Completed
Push — develop ( 3b8b48...76311f )
by Nate
27:41 queued 12:43
created

AbstractMetricCollection   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 83
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A calculateScore() 0 12 2
A toConfig() 0 15 2
A setMetrics() 0 14 3
A createMetric() 0 6 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/scorecard/license
6
 * @link       https://www.flipboxfactory.com/software/scorecard/
7
 */
8
9
namespace flipbox\craft\scorecard\metrics;
10
11
use flipbox\craft\scorecard\helpers\MetricHelper;
12
13
/**
14
 * A Metric which utilized child Metrics to calculate a score.  This is useful when preforming complex calculations
15
 * or further details/breakdown are needed on a particular Metric.
16
 *
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
abstract class AbstractMetricCollection extends AbstractMetric
21
{
22
    /**
23
     * @var MetricInterface[]
24
     */
25
    protected $metrics = [];
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function init()
31
    {
32
        parent::init();
33
        $this->setMetrics($this->metrics);
34
    }
35
36
    /**
37
     * @return float
38
     */
39
    protected function calculateScore(): float
40
    {
41
        // Sum of total/weight
42
        $total = $weights = 0;
43
44
        foreach ($this->metrics as $metric) {
45
            $total += $metric->getScore();
46
            $weights += $metric->getWeight();
47
        }
48
49
        return (float)($total / $weights);
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function toConfig(): array
56
    {
57
        $children = [];
58
59
        foreach ($this->metrics as $metric) {
60
            $children[] = $metric->toConfig();
61
        }
62
63
        return array_merge(
64
            parent::toConfig(),
65
            [
66
                'metrics' => $children
67
            ]
68
        );
69
    }
70
71
    /**
72
     * @param array $metrics
73
     * @return $this
74
     */
75
    public function setMetrics(array $metrics = [])
76
    {
77
        $this->metrics = [];
78
79
        foreach ($metrics as $metric) {
80
            if (!$metric instanceof MetricInterface) {
81
                $metric = $this->createMetric($metric);
82
            }
83
84
            $this->metrics[] = $metric;
85
        }
86
87
        return $this;
88
    }
89
90
    /**
91
     * @noinspection PhpDocMissingThrowsInspection
92
     *
93
     * @param $metric
94
     * @return MetricInterface
95
     */
96
    protected function createMetric($metric): MetricInterface
97
    {
98
        /** @noinspection PhpUnhandledExceptionInspection */
99
        /** @noinspection PhpIncompatibleReturnTypeInspection */
100
        return MetricHelper::create($metric);
101
    }
102
}
103