Completed
Push — develop ( 66c29c...10a2c8 )
by Stan
02:47
created

ApcRepository::increment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 3
dl 0
loc 11
ccs 0
cts 7
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Krenor\Prometheus\Storage\Repositories;
4
5
use RuntimeException;
6
use Tightenco\Collect\Support\Collection;
7
use Krenor\Prometheus\Contracts\Repository;
8
9
class ApcRepository implements Repository
10
{
11
    /**
12
     * @var ApcWrapper
13
     */
14
    protected $apc;
15
16
    /**
17
     * ApcRepository constructor.
18
     *
19
     * @param ApcWrapper $apc
20
     */
21
    public function __construct(ApcWrapper $apc)
22
    {
23
        $this->apc = $apc;
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function get(string $key): Collection
30
    {
31
        return new Collection($this->apc->get($key) ?: []);
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function increment(string $key, string $field, float $value): void
38
    {
39
        $collection = $this->get($key);
40
41
        $stored = $this->apc->store($key, $collection->put(
42
            $field,
43
            $collection->get($field, 0) + $value
44
        ));
45
46
        if (!$stored) {
47
            throw new RuntimeException('The store method returned false.');
48
        }
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function decrement(string $key, string $field, float $value): void
55
    {
56
        $this->increment($key, $field, -abs($value));
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function set(string $key, string $field, $value, $override = true): void
63
    {
64
        $collection = $this->get($key);
65
66
        if (!$override && $collection->get($field) !== null) {
67
            return;
68
        }
69
70
        $stored = $this->apc->store($key, $collection->put($field, $value));
71
72
        if (!$stored) {
73
            throw new RuntimeException('The store method returned false.');
74
        }
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function push(string $key, float $value): void
81
    {
82
        $collection = $this->get($key);
83
84
        $stored = $this->apc->store($key, $collection->push($value));
85
86
        if (!$stored) {
87
            throw new RuntimeException('The store method returned false.');
88
        }
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function flush(): bool
95
    {
96
        return $this->apc->flush();
97
    }
98
}
99