Completed
Push — develop ( 3b7672...d09c9d )
by Stan
02:23
created

ApcuRepository::flush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 ApcuRepository implements Repository
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14 6
    public function get(string $key): Collection
15
    {
16 6
        return new Collection(apcu_fetch($key) ?: []);
17
    }
18
19
    /**
20
     * {@inheritdoc}
21
     */
22 4
    public function increment(string $key, string $field, float $value): void
23
    {
24 4
        $collection = $this->get($key);
25
26 4
        $stored = apcu_store($key, $collection->put(
27 4
            $field,
28 4
            $collection->get($field, 0) + $value
29
        ));
30
31 4
        if (!$stored) {
32
            throw new RuntimeException('The store method returned false.');
33
        }
34 4
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 1
    public function decrement(string $key, string $field, float $value): void
40
    {
41 1
        $this->increment($key, $field, -abs($value));
42 1
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 2
    public function set(string $key, string $field, $value, $override = true): void
48
    {
49 2
        $collection = $this->get($key);
50
51 2
        if (!$override && $collection->get($field) !== null) {
52 1
            return;
53
        }
54
55 2
        $stored = apcu_store($key, $collection->put($field, $value));
56
57 2
        if (!$stored) {
58
            throw new RuntimeException('The store method returned false.');
59
        }
60 2
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 1
    public function push(string $key, float $value): void
66
    {
67 1
        $collection = $this->get($key);
68
69 1
        $stored = apcu_store($key, $collection->push($value));
70
71 1
        if (!$stored) {
72
            throw new RuntimeException('The store method returned false.');
73
        }
74 1
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 6
    public function flush(): bool
80
    {
81 6
        return apcu_clear_cache();
82
    }
83
}
84