ProviderInvoker::onTerminate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Flagbit\Bundle\MetricsBundle\Provider;
4
5
use Beberlei\Metrics\Collector\Collector;
6
use Flagbit\Bundle\MetricsBundle\Collector\Factory\CollectorCollectionFactory;
7
8
class ProviderInvoker
9
{
10
    /**
11
     * @var CollectorCollectionFactory
12
     */
13
    private $factory;
14
15
    /**
16
     * @var ProviderInterface[]
17
     */
18
    private $providers = [];
19
20
    /**
21
     * @var \Flagbit\Bundle\MetricsBundle\Collector\CollectorCollection[]
22
     */
23
    private $collectorCollections = [];
24
25
    /**
26
     * @param CollectorCollectionFactory $factory
27
     */
28 3
    public function __construct(CollectorCollectionFactory $factory)
29
    {
30 3
        $this->factory = $factory;
31 3
    }
32
33 2
    public function collectMetrics()
34
    {
35 2
        foreach ($this->providers as $key => $provider) {
36 2
            $provider->collectMetrics($this->collectorCollections[$key]);
37
        }
38 2
    }
39
40
    public function onTerminate()
41
    {
42
        foreach ($this->collectorCollections as $collectorCollection) {
43
            $collectorCollection->flush();
44
        }
45
    }
46
47
    /**
48
     * @param ProviderInterface $provider
49
     * @param Collector[]       $collectors
50
     */
51 3
    public function addMetricsProvider(ProviderInterface $provider, array $collectors)
52
    {
53 3
        $collectorsCollection = $this->factory->create();
54 3
        foreach ($collectors as $collector) {
55 3
            $collectorsCollection->addCollector($collector);
56
        }
57
58 3
        $key = spl_object_hash($provider);
59 3
        $this->providers[$key] = $provider;
60 3
        $this->collectorCollections[$key] = $collectorsCollection;
61 3
    }
62
}
63