Manager::extend()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JildertMiedema\SystemMonitor;
6
7
use Illuminate\Contracts\Container\Container;
8
use JildertMiedema\SystemMonitor\Measurements\Measurement;
9
10
final class Manager
11
{
12
    /**
13
     * @var Measurement[]
14
     */
15
    private $types = [];
16
17
    /**
18
     * @var Container
19
     */
20
    private $container;
21
22
    public function __construct(Container $container)
23
    {
24
        $this->container = $container;
25
    }
26
27
    public function extend(Measurement $class)
28
    {
29
        $this->types[$class->name()] = $class;
30
    }
31
32
    public function run()
33
    {
34
        $measurements = $this->container['config']->get('measurement.measurements', []);
35
        $store = $this->container['measurement.store'];
36
37
        foreach ($measurements as $measurement) {
38
            $type = array_get($measurement, 'type');
39
            $key = array_get($measurement, 'key');
40
41
            if (!$type || !$key) {
42
                $message = 'The measurement configuration needs at least a type and a key';
43
                throw new MeasurementConfigurationError($message);
44
            }
45
46
            $runner = $this->types[$type];
47
48
            $runner->run($store, $measurement);
49
        }
50
    }
51
}
52