RedisWallet::deposit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 1
dl 0
loc 3
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\ChargeableApi\Wallet;
6
7
use Damax\ChargeableApi\Credit;
8
use Predis\ClientInterface;
9
10
final class RedisWallet implements Wallet
11
{
12
    private $client;
13
    private $walletKey;
14
    private $identity;
15
16
    public function __construct(ClientInterface $client, string $walletKey, string $identity)
17
    {
18
        $this->client = $client;
19
        $this->walletKey = $walletKey;
20
        $this->identity = $identity;
21
    }
22
23
    public function balance(): Credit
24
    {
25
        $balance = $this->client->hget($this->walletKey, $this->identity) ?? 0;
26
27
        return Credit::fromInteger((int) $balance);
28
    }
29
30
    public function deposit(Credit $credit): void
31
    {
32
        $this->client->hincrby($this->walletKey, $this->identity, $credit->toInteger());
33
    }
34
35
    public function withdraw(Credit $credit): void
36
    {
37
        $balance = $this->balance()->subtract($credit)->toInteger();
38
39
        $this->client->hset($this->walletKey, $this->identity, $balance);
40
    }
41
}
42