RedisWallet   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 30
c 0
b 0
f 0
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A deposit() 0 3 1
A __construct() 0 5 1
A withdraw() 0 5 1
A balance() 0 5 1
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