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

MaterializeHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 42
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A materializeSource() 0 8 2
A materialize() 0 6 1
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