| Total Complexity | 5 |
| Total Lines | 35 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 11 | final class MongoWallet implements Wallet |
||
| 12 | { |
||
| 13 | private const FIELD_BALANCE = 'balance'; |
||
| 14 | |||
| 15 | private $collection; |
||
| 16 | private $filters; |
||
| 17 | |||
| 18 | public function __construct(Collection $collection, string $identity) |
||
| 19 | { |
||
| 20 | $this->collection = $collection; |
||
| 21 | $this->filters = ['_id' => $identity]; |
||
| 22 | } |
||
| 23 | |||
| 24 | public function balance(): Credit |
||
| 25 | { |
||
| 26 | $options = ['projection' => [self::FIELD_BALANCE => 1]]; |
||
| 27 | |||
| 28 | /** @var BSONDocument $doc */ |
||
| 29 | if (null === $doc = $this->collection->findOne($this->filters, $options)) { |
||
| 30 | return Credit::blank(); |
||
| 31 | } |
||
| 32 | |||
| 33 | return Credit::fromInteger($doc[self::FIELD_BALANCE]); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function deposit(Credit $credit): void |
||
| 37 | { |
||
| 38 | $this->collection->updateOne($this->filters, ['$inc' => [self::FIELD_BALANCE => $credit->toInteger()]], ['upsert' => true]); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function withdraw(Credit $credit): void |
||
| 46 | } |
||
| 47 | } |
||
| 48 |