AtomicMutableWrapper   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 20
dl 0
loc 62
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 3 1
A getTags() 0 3 1
A getName() 0 3 1
A __construct() 0 4 1
A setValue() 0 5 1
A executeWithLock() 0 11 1
A adjust() 0 5 1
1
<?php
2
3
namespace Lamoda\Metric\Adapters\Doctrine;
4
5
use Doctrine\DBAL\LockMode;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Lamoda\Metric\Storage\MutableMetricInterface;
8
9
/** @internal */
10
final class AtomicMutableWrapper implements MutableMetricInterface
11
{
12
    /** @var EntityManagerInterface */
13
    private $manager;
14
    /** @var MutableMetricInterface */
15
    private $metric;
16
17 3
    public function __construct(EntityManagerInterface $manager, MutableMetricInterface $metric)
18
    {
19 3
        $this->manager = $manager;
20 3
        $this->metric = $metric;
21 3
    }
22
23
    /** {@inheritdoc} */
24 1
    public function adjust(float $delta): void
25
    {
26 1
        $this->executeWithLock(
27
            function () use ($delta) {
28 1
                $this->metric->adjust($delta);
29 1
            }
30
        );
31 1
    }
32
33
    /** {@inheritdoc} */
34 1
    public function setValue(float $value): void
35
    {
36 1
        $this->executeWithLock(
37
            function () use ($value) {
38 1
                $this->metric->setValue($value);
39 1
            }
40
        );
41 1
    }
42
43
    /** {@inheritdoc} */
44 1
    public function getName(): string
45
    {
46 1
        return $this->metric->getName();
47
    }
48
49
    /** {@inheritdoc} */
50 1
    public function resolve(): float
51
    {
52 1
        return $this->metric->resolve();
53
    }
54
55
    /** {@inheritdoc} */
56 1
    public function getTags(): array
57
    {
58 1
        return $this->metric->getTags();
59
    }
60
61 2
    private function executeWithLock(callable $fn): void
62
    {
63 2
        $this->manager->transactional(
64
            function () use ($fn) {
65 2
                $this->manager->lock($this->metric, LockMode::PESSIMISTIC_WRITE);
66
67 2
                $this->manager->refresh($this->metric);
68
69 2
                $fn();
70
71 2
                $this->manager->flush();
72 2
            }
73
        );
74 2
    }
75
}
76