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 StringHelper::titleize( |
47
|
|
|
(new \ReflectionClass(static::class)) |
48
|
|
|
->getShortName() |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
|
|
public function getVersion(): string |
56
|
|
|
{ |
57
|
|
|
return $this->version; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritdoc |
62
|
|
|
*/ |
63
|
|
|
public function getWeight(): float |
64
|
|
|
{ |
65
|
|
|
return $this->weight; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return float |
70
|
|
|
*/ |
71
|
|
|
public function getScore(): float |
72
|
|
|
{ |
73
|
|
|
if ($this->score === null) { |
74
|
|
|
$this->score = $this->calculateScore(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->score * $this->getWeight(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function resetScore() |
84
|
|
|
{ |
85
|
|
|
$this->score = null; |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param float|null $score |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
|
|
public function setScore(float $score = null) |
94
|
|
|
{ |
95
|
|
|
$this->score = $score; |
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @inheritdoc |
101
|
|
|
*/ |
102
|
|
|
public function toConfig(): array |
103
|
|
|
{ |
104
|
|
|
return [ |
105
|
|
|
'class' => static::class, |
106
|
|
|
'weight' => $this->getWeight(), |
107
|
|
|
'version' => $this->getVersion(), |
108
|
|
|
'score' => $this->getScore() |
109
|
|
|
]; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|