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

InMemoryRepository::flush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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