AbstractMetricCollection   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A calculateScore() 0 12 2
A resetScore() 0 8 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 resetScore()
56
    {
57
        foreach ($this->metrics as $metric) {
58
            $metric->resetScore();
59
        }
60
61
        return parent::resetScore();
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67
    public function toConfig(): array
68
    {
69
        $children = [];
70
71
        foreach ($this->metrics as $metric) {
72
            $children[] = $metric->toConfig();
73
        }
74
75
        return array_merge(
76
            parent::toConfig(),
77
            [
78
                'metrics' => $children
79
            ]
80
        );
81
    }
82
83
    /**
84
     * @param array $metrics
85
     * @return $this
86
     */
87
    public function setMetrics(array $metrics = [])
88
    {
89
        $this->metrics = [];
90
91
        foreach ($metrics as $metric) {
92
            if (!$metric instanceof MetricInterface) {
93
                $metric = $this->createMetric($metric);
94
            }
95
96
            $this->metrics[] = $metric;
97
        }
98
99
        return $this;
100
    }
101
102
    /**
103
     * @noinspection PhpDocMissingThrowsInspection
104
     *
105
     * @param $metric
106
     * @return MetricInterface
107
     */
108
    protected function createMetric($metric): MetricInterface
109
    {
110
        /** @noinspection PhpUnhandledExceptionInspection */
111
        /** @noinspection PhpIncompatibleReturnTypeInspection */
112
        return MetricHelper::create($metric);
113
    }
114
}
115