AbstractDoctrineStorage::findMetric()   A
last analyzed

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 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Lamoda\Metric\Adapters\Doctrine;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Lamoda\Metric\Common\MetricSourceInterface;
7
use Lamoda\Metric\Storage\Exception\ReceiverException;
8
use Lamoda\Metric\Storage\MetricStorageInterface;
9
use Lamoda\Metric\Storage\MutableMetricInterface;
10
11
abstract class AbstractDoctrineStorage implements \IteratorAggregate, MetricStorageInterface
12
{
13
    /** @var EntityManagerInterface */
14
    protected $entityManager;
15
16 2
    public function __construct(EntityManagerInterface $entityManager)
17
    {
18 2
        $this->entityManager = $entityManager;
19 2
    }
20
21 1
    final public function getIterator(): \Traversable
22
    {
23 1
        return $this->getMetrics();
24
    }
25
26
    /** {@inheritdoc} */
27 2
    final public function receive(MetricSourceInterface $source): void
28
    {
29 2
        $this->entityManager->beginTransaction();
30
        try {
31 2
            $this->doReceive($source);
32 1
            $this->entityManager->flush();
33 1
            $this->entityManager->commit();
34 1
        } catch (\Exception $exception) {
35 1
            $this->entityManager->rollback();
36 1
            throw ReceiverException::becauseOfStorageFailure($exception);
37
        }
38 1
    }
39
40
    /** {@inheritdoc} */
41 2
    final public function findMetric(string $name, array $tags = []): ?MutableMetricInterface
42
    {
43 2
        $metric = $this->doFindMetric($name, $tags);
44 1
        if (!$metric) {
45 1
            return null;
46
        }
47
48 1
        return new AtomicMutableWrapper($this->entityManager, $metric);
49
    }
50
51
    /** {@inheritdoc} */
52 1
    final public function createMetric(string $name, float $value, array $tags = []): MutableMetricInterface
53
    {
54 1
        $metric = $this->doCreateMetric($name, $value, $tags);
55 1
        $this->entityManager->persist($metric);
56
57 1
        return new AtomicMutableWrapper($this->entityManager, $metric);
58
    }
59
60
    abstract protected function doFindMetric(string $name, array $tags = []): ?MutableMetricInterface;
61
62
    abstract protected function doCreateMetric(string $name, float $value, array $tags = []): MutableMetricInterface;
63
64
    /**
65
     * @param MetricSourceInterface $source
66
     */
67 2
    protected function doReceive(MetricSourceInterface $source): void
68
    {
69 2
        foreach ($source->getMetrics() as $metric) {
70 2
            $tags = $metric->getTags();
71 2
            $name = $metric->getName();
72 2
            $value = $metric->resolve();
73 2
            $resolved = $this->findMetric($name, $tags);
74 1
            if (!$resolved) {
75 1
                $resolved = $this->createMetric($name, 0, $tags);
76
            }
77 1
            $resolved->setValue($value);
78
        }
79 1
    }
80
}
81