-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
407 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tbbc\MoneyBundle\Document; | ||
|
||
use DateTime; | ||
use DateTimeInterface; | ||
|
||
class DocumentRatioHistory | ||
{ | ||
protected ?string $id = null; | ||
protected string $referenceCurrencyCode = ''; | ||
protected string $currencyCode = ''; | ||
protected float $ratio = 0; | ||
protected DateTimeInterface $savedAt; | ||
|
||
public function __construct() | ||
{ | ||
$this->savedAt = new DateTime(); | ||
} | ||
|
||
public function setId(string $id): self | ||
{ | ||
$this->id = $id; | ||
|
||
return $this; | ||
} | ||
|
||
public function getId(): ?string | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setCurrencyCode(string $currencyCode): self | ||
{ | ||
$this->currencyCode = $currencyCode; | ||
|
||
return $this; | ||
} | ||
|
||
public function getCurrencyCode(): string | ||
{ | ||
return $this->currencyCode; | ||
} | ||
|
||
public function setRatio(float $ratio): self | ||
{ | ||
$this->ratio = $ratio; | ||
|
||
return $this; | ||
} | ||
|
||
public function getRatio(): float | ||
{ | ||
return $this->ratio; | ||
} | ||
|
||
public function setReferenceCurrencyCode(string $referenceCurrencyCode): self | ||
{ | ||
$this->referenceCurrencyCode = $referenceCurrencyCode; | ||
|
||
return $this; | ||
} | ||
|
||
public function getReferenceCurrencyCode(): string | ||
{ | ||
return $this->referenceCurrencyCode; | ||
} | ||
|
||
public function setSavedAt(DateTimeInterface $savedAt): self | ||
{ | ||
$this->savedAt = $savedAt; | ||
|
||
return $this; | ||
} | ||
|
||
public function getSavedAt(): DateTimeInterface | ||
{ | ||
return $this->savedAt; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tbbc\MoneyBundle\Document; | ||
|
||
class DocumentStorageRatio | ||
{ | ||
private ?string $id = null; | ||
|
||
public function __construct( | ||
private ?string $currencyCode = null, | ||
private ?float $ratio = null, | ||
) { | ||
} | ||
|
||
public function getId(): ?string | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setCurrencyCode(string $currencyCode): self | ||
{ | ||
$this->currencyCode = $currencyCode; | ||
|
||
return $this; | ||
} | ||
|
||
public function getCurrencyCode(): ?string | ||
{ | ||
return $this->currencyCode; | ||
} | ||
|
||
public function setRatio(float $ratio): self | ||
{ | ||
$this->ratio = $ratio; | ||
|
||
return $this; | ||
} | ||
|
||
public function getRatio(): ?float | ||
{ | ||
return $this->ratio; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tbbc\MoneyBundle\Pair\Storage; | ||
|
||
use Doctrine\ODM\MongoDB\DocumentManager; | ||
use Tbbc\MoneyBundle\Document\DocumentStorageRatio; | ||
use Tbbc\MoneyBundle\Pair\StorageInterface; | ||
|
||
/** | ||
* Class DocumentStorage. | ||
* | ||
* @author Philippe Le Van. | ||
*/ | ||
class DocumentStorage implements StorageInterface | ||
{ | ||
protected array $ratioList = []; | ||
|
||
public function __construct(protected DocumentManager $documentManager, protected string $referenceCurrencyCode) | ||
{ | ||
} | ||
|
||
public function loadRatioList(bool $force = false): array | ||
{ | ||
if ((false === $force) && (count($this->ratioList) > 0)) { | ||
return $this->ratioList; | ||
} | ||
|
||
$repository = $this->documentManager->getRepository(DocumentStorageRatio::class); | ||
$documentStorageRatios = $repository->findAll(); | ||
|
||
if (0 === count($documentStorageRatios)) { | ||
$this->ratioList = [$this->referenceCurrencyCode => 1.0]; | ||
$this->saveRatioList($this->ratioList); | ||
|
||
return $this->ratioList; | ||
} | ||
|
||
$this->ratioList = []; | ||
|
||
foreach ($documentStorageRatios as $documentStorageRatio) { | ||
if ( | ||
null !== ($code = $documentStorageRatio->getCurrencyCode()) | ||
&& null !== ($ratio = $documentStorageRatio->getRatio()) | ||
) { | ||
$this->ratioList[$code] = $ratio; | ||
} | ||
} | ||
|
||
return $this->ratioList; | ||
} | ||
|
||
/** | ||
* @psalm-param array<string, null|float> $ratioList | ||
*/ | ||
public function saveRatioList(array $ratioList): void | ||
{ | ||
/** @var DocumentStorageRatio[] $documentStorageRatios */ | ||
$documentStorageRatios = $this->documentManager->getRepository(DocumentStorageRatio::class)->findAll(); | ||
|
||
// index them in an associative array | ||
$existingStorageRatios = []; | ||
foreach ($documentStorageRatios as $documentStorageRatio) { | ||
$existingStorageRatios[$documentStorageRatio->getCurrencyCode()] = $documentStorageRatio; | ||
} | ||
|
||
foreach ($ratioList as $currencyCode => $ratio) { | ||
// load from existing, or create a new | ||
$existingStorageRatio = $existingStorageRatios[$currencyCode] ?? new DocumentStorageRatio($currencyCode, $ratio); | ||
$existingStorageRatio->setRatio($ratio); | ||
$this->documentManager->persist($existingStorageRatio); | ||
|
||
// remove from the array, as we do not want to remove this one | ||
unset($existingStorageRatios[$currencyCode]); | ||
} | ||
|
||
// remove the remaining ones | ||
foreach ($existingStorageRatios as $documentStorageRatio) { | ||
$this->documentManager->remove($documentStorageRatio); | ||
} | ||
|
||
// flush to database | ||
$this->documentManager->flush(); | ||
$this->documentManager->clear(); | ||
|
||
$this->ratioList = $ratioList; | ||
} | ||
} |
Oops, something went wrong.