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

ApcRepository   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 88
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A flush() 0 3 1
A decrement() 0 3 1
A set() 0 12 4
A push() 0 8 2
A get() 0 3 2
A increment() 0 11 2
A __construct() 0 3 1
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