Passed
Pull Request — master (#5)
by Pavel
05:52
created

MaterializeHelper::materialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Lamoda\Metric\Storage;
4
5
use Lamoda\Metric\Collector\CollectorRegistry;
6
use Lamoda\Metric\Common\Metric;
7
use Lamoda\Metric\Common\MetricSourceInterface;
8
use Lamoda\Metric\Common\Source\IterableMetricSource;
9
use Lamoda\Metric\Storage\Exception\ReceiverException;
10
11
final class MaterializeHelper
12
{
13
    /** @var CollectorRegistry */
14
    private $collectorRegistry;
15
    /** @var StorageRegistry */
16
    private $storageRegistry;
17
18
    /**
19
     * @param CollectorRegistry $collectorRegistry
20
     * @param StorageRegistry   $storageRegistry
21
     */
22
    public function __construct(CollectorRegistry $collectorRegistry, StorageRegistry $storageRegistry)
23
    {
24
        $this->collectorRegistry = $collectorRegistry;
25
        $this->storageRegistry = $storageRegistry;
26
    }
27
28
    /**
29
     * Receives metrics from named collector to named storage.
30
     *
31
     * @param string $collectorName
32
     * @param string $storageName
33
     *
34
     * @throws ReceiverException
35
     * @throws \OutOfBoundsException
36
     */
37
    public function materialize(string $collectorName, string $storageName): void
38
    {
39
        $collector = $this->collectorRegistry->getCollector($collectorName);
40
        $storage = $this->storageRegistry->getStorage($storageName);
41
42
        $storage->receive($this->materializeSource($collector->collect()));
43
    }
44
45
    private function materializeSource(MetricSourceInterface $source): MetricSourceInterface
46
    {
47
        $metrics = [];
48
        foreach ($source->getMetrics() as $metric) {
49
            $metrics[] = new Metric($metric->getName(), $metric->resolve(), $metric->getTags());
50
        }
51
52
        return new IterableMetricSource($metrics);
53
    }
54
}
55