Completed
Branch master (44133e)
by Alessandro
02:07
created

PointsCollectionStorage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 11 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
declare(strict_types=1);
3
namespace Algatux\InfluxDbBundle\Services;
4
5
use Algatux\InfluxDbBundle\Model\PointsCollection;
6
7
/**
8
 * Class PointsCollectionStorage
9
 * @package Algatux\InfluxDbBundle\Services
10
 */
11
class PointsCollectionStorage implements CollectionStorageInterface
12
{
13
14
    /** @var array  */
15
    protected $storage;
16
17
    /**
18
     * PointsCollectionStorage constructor.
19
     */
20 3
    public function __construct()
21
    {
22 3
        $this->storage = [];
23 3
    }
24
25
    /**
26
     * @param PointsCollection $collection
27
     * @param string $writeMode
28
     */
29 2
    public function storeCollection(PointsCollection $collection, $writeMode)
30
    {
31 2
        $this->checkStorageInitialization($writeMode, $collection->getPrecision());
32 2
        $this->mergeCollections($writeMode, $collection);
33 2
    }
34
35
    /**
36
     * @return array
37
     */
38 2
    public function getStoredCollections(): array
39
    {
40 2
        return $this->storage;
41
    }
42
43
    /**
44
     * @param string $getWriteMode
45
     * @param string $getPrecision
46
     */
47 2
    private function checkStorageInitialization(string $getWriteMode, string $getPrecision)
48
    {
49 2
        if (! isset($this->storage[$getWriteMode])) {
50 2
            $this->storage[$getWriteMode] = null;
51
        }
52
53 2
        if (!isset($this->storage[$getWriteMode][$getPrecision])) {
54 2
            $this->storage[$getWriteMode][$getPrecision] = new PointsCollection([], $getPrecision);
55
        }
56 2
    }
57
58
    /**
59
     * @param string $writemode
60
     * @param PointsCollection $points
61
     * @internal param InfluxDbEvent $event
62
     */
63 2
    private function mergeCollections(string $writemode, PointsCollection $points)
64
    {
65
        /** @var PointsCollection $actualCollection */
66 2
        $actualCollection = $this->storage[$writemode][$points->getPrecision()];
67
68 2
        $this->storage[$writemode][$points->getPrecision()] = new PointsCollection(array_merge(
69 2
            $actualCollection->toArray(),
70 2
            $points->toArray()
71 2
        ), $points->getPrecision());
72
73 2
    }
74
    
75
}
76