AbstractMetric::toConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 craft\helpers\StringHelper;
12
use yii\base\BaseObject;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 1.0.0
17
 */
18
abstract class AbstractMetric extends BaseObject implements MetricInterface
19
{
20
    /**
21
     * @var float
22
     */
23
    public $weight = 1;
24
25
    /**
26
     * @var string
27
     */
28
    public $version = '1.0.0';
29
30
    /**
31
     * @var float
32
     */
33
    private $score;
34
35
    /**
36
     * @return float
37
     */
38
    abstract protected function calculateScore(): float;
39
40
    /**
41
     * @inheritdoc
42
     * @throws \ReflectionException
43
     */
44
    public static function displayName(): string
45
    {
46
        return preg_replace(
47
            '/(?<!^)([A-Z])/',
48
            ' $0',
49
            (new \ReflectionClass(static::class))
50
            ->getShortName()
51
        );
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function getVersion(): string
58
    {
59
        return $this->version;
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function getWeight(): float
66
    {
67
        return $this->weight;
68
    }
69
70
    /**
71
     * @return float
72
     */
73
    public function getScore(): float
74
    {
75
        if ($this->score === null) {
76
            $this->score = $this->calculateScore();
77
        }
78
79
        return $this->score * $this->getWeight();
80
    }
81
82
    /**
83
     * @return $this
84
     */
85
    public function resetScore()
86
    {
87
        $this->score = null;
88
        return $this;
89
    }
90
91
    /**
92
     * @param float|null $score
93
     * @return $this
94
     */
95
    public function setScore(float $score = null)
96
    {
97
        $this->score = $score;
98
        return $this;
99
    }
100
101
    /**
102
     * @inheritdoc
103
     */
104
    public function toConfig(): array
105
    {
106
        return [
107
            'class' => static::class,
108
            'weight' => $this->getWeight(),
109
            'version' => $this->getVersion(),
110
            'score' => $this->getScore()
111
        ];
112
    }
113
}
114