Completed
Push — master ( a7fed8...afc97a )
by Nate
13:05
created

ElementMetric   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 0
loc 180
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
calculateScore() 0 1 ?
A init() 0 20 4
A find() 0 6 1
A instantiate() 0 5 1
A rules() 0 51 1
A displayName() 0 7 1
A getWeight() 0 4 1
A getVersion() 0 4 1
A getScore() 0 8 2
A toConfig() 0 4 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\scorecard\records;
10
11
use Craft;
12
use craft\helpers\StringHelper;
13
use flipbox\ember\helpers\ModelHelper;
14
use flipbox\ember\records\ActiveRecordWithId;
15
use flipbox\ember\records\traits\ElementAttribute;
16
use flipbox\scorecard\db\ElementMetricQuery;
17
use flipbox\scorecard\helpers\MetricHelper;
18
use flipbox\scorecard\metrics\MetricInterface;
19
use flipbox\scorecard\validators\ElementMetricValidator;
20
21
/**
22
 * @author Flipbox Factory <[email protected]>
23
 * @since 1.0.0
24
 *
25
 * @property int $parentId
26
 * @property string $class
27
 * @property float $score
28
 * @property float $weight
29
 * @property string $version
30
 * @property array|null $settings
31
 */
32
abstract class ElementMetric extends ActiveRecordWithId implements MetricInterface
33
{
34
    use ElementAttribute;
35
36
    /**
37
     * The default score weight
38
     */
39
    const WEIGHT = 1;
40
41
    /**
42
     * The default metric version
43
     */
44
    const VERSION = '1.0';
45
46
    /**
47
     * The table alias
48
     */
49
    const TABLE_ALIAS = 'scorecard_element_metrics';
50
51
    /**
52
     * @inheritdoc
53
     */
54
    protected $getterPriorityAttributes = ['elementId', 'score'];
55
56
    /**
57
     * @return float
58
     */
59
    abstract protected function calculateScore(): float;
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function init()
65
    {
66
        parent::init();
67
68
        $this->setAttribute(
69
            'settings',
70
            MetricHelper::resolveSettings(
71
                $this->getAttribute('settings')
72
            )
73
        );
74
75
        // Always this class
76
        $this->class = static::class;
77
78
        // Defaults
79
        if ($this->getIsNewRecord()) {
80
            $this->weight = $this->weight ?: static::WEIGHT;
81
            $this->version = $this->version ?: static::VERSION;
82
        }
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88
    public static function find()
89
    {
90
        /** @noinspection PhpUnhandledExceptionInspection */
91
        /** @noinspection PhpIncompatibleReturnTypeInspection */
92
        return Craft::createObject(ElementMetricQuery::class, [get_called_class()]);
93
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98
    public static function instantiate($row)
99
    {
100
        $class = $row['class'] ?? static::class;
101
        return new $class;
102
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107
    public function rules()
108
    {
109
        return array_merge(
110
            parent::rules(),
111
            $this->elementRules(),
112
            [
113
                [
114
                    [
115
                        'class'
116
                    ],
117
                    ElementMetricValidator::class
118
                ],
119
                [
120
                    [
121
                        'parentId',
122
                    ],
123
                    'number',
124
                    'integerOnly' => true
125
                ],
126
                [
127
                    [
128
                        'score',
129
                        'weight',
130
                    ],
131
                    'number'
132
                ],
133
                [
134
                    [
135
                        'elementId',
136
                        'class',
137
                        'weight',
138
                        'version',
139
                    ],
140
                    'required'
141
                ],
142
                [
143
                    [
144
                        'class',
145
                        'settings',
146
                        'score',
147
                        'weight',
148
                        'version'
149
                    ],
150
                    'safe',
151
                    'on' => [
152
                        ModelHelper::SCENARIO_DEFAULT
153
                    ]
154
                ]
155
            ]
156
        );
157
    }
158
159
160
    /*******************************************
161
     * METRIC INTERFACE
162
     *******************************************/
163
164
    /**
165
     * @inheritdoc
166
     * @throws \ReflectionException
167
     */
168
    public static function displayName(): string
169
    {
170
        return StringHelper::titleize(
171
            (new \ReflectionClass(static::class))
172
                ->getShortName()
173
        );
174
    }
175
176
    /**
177
     * @inheritdoc
178
     */
179
    public function getWeight(): float
180
    {
181
        return (float)$this->getAttribute('weight');
182
    }
183
184
    /**
185
     * @inheritdoc
186
     */
187
    public function getVersion(): string
188
    {
189
        return (string)$this->getAttribute('version');
190
    }
191
192
    /**
193
     * @inheritdoc
194
     */
195
    public function getScore(): float
196
    {
197
        if ($this->getAttribute('score') === null) {
198
            $this->setAttribute('score', $this->calculateScore() * $this->getWeight());
199
        }
200
201
        return (float)$this->getAttribute('score');
202
    }
203
204
    /**
205
     * @return array
206
     */
207
    public function toConfig(): array
208
    {
209
        return parent::toArray();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (toArray() instead of toConfig()). Are you sure this is correct? If so, you might want to change this to $this->toArray().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
210
    }
211
}
212