Completed
Pull Request — master (#5)
by Pavel
11:51
created

MaterializeHelper::materializeSource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
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 1
    public function __construct(CollectorRegistry $collectorRegistry, StorageRegistry $storageRegistry)
23
    {
24 1
        $this->collectorRegistry = $collectorRegistry;
25 1
        $this->storageRegistry = $storageRegistry;
26 1
    }
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 1
    public function materialize(string $collectorName, string $storageName): void
38
    {
39 1
        $collector = $this->collectorRegistry->getCollector($collectorName);
40 1
        $storage = $this->storageRegistry->getStorage($storageName);
41
42 1
        $storage->receive($this->materializeSource($collector->collect()));
43 1
    }
44
45 1
    private function materializeSource(MetricSourceInterface $source): MetricSourceInterface
46
    {
47 1
        $metrics = [];
48 1
        foreach ($source->getMetrics() as $metric) {
49 1
            $metrics[] = new Metric($metric->getName(), $metric->resolve(), $metric->getTags());
50
        }
51
52 1
        return new IterableMetricSource($metrics);
53
    }
54
}
55