SimpleRepository::get()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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