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\queue; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\helpers\Json; |
13
|
|
|
use craft\queue\BaseJob; |
14
|
|
|
use craft\queue\JobInterface; |
15
|
|
|
use flipbox\craft\scorecard\metrics\SavableMetricInterface; |
16
|
|
|
use flipbox\craft\scorecard\Scorecard; |
17
|
|
|
use Serializable; |
18
|
|
|
use yii\di\Instance; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Flipbox Factory <[email protected]> |
22
|
|
|
* @since 1.0.0 |
23
|
|
|
* |
24
|
|
|
* @property int $parentId |
25
|
|
|
* @property string $class |
26
|
|
|
* @property float $score |
27
|
|
|
* @property float $weight |
28
|
|
|
* @property string $version |
29
|
|
|
* @property array|null $settings |
30
|
|
|
* @property \DateTime $dateCalculated |
31
|
|
|
*/ |
32
|
|
|
class CalculateAndSaveMetricJob extends BaseJob implements JobInterface, Serializable |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var |
36
|
|
|
*/ |
37
|
|
|
public $metric; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
* @return bool |
42
|
|
|
* @throws \yii\base\InvalidConfigException |
43
|
|
|
*/ |
44
|
|
|
public function execute($queue) |
45
|
|
|
{ |
46
|
|
|
if (null === ($metric = $this->resolveMetric())) { |
47
|
|
|
Scorecard::error("Unable to resolve metric."); |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Calculate score |
52
|
|
|
$metric->getScore(); |
53
|
|
|
|
54
|
|
|
if (!$metric->save()) { |
55
|
|
|
Scorecard::warning(sprintf( |
56
|
|
|
"Unable to save metric: %s", |
57
|
|
|
Json::encode($metric->toConfig()) |
58
|
|
|
)); |
59
|
|
|
|
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
Scorecard::info("Successfully saved metric."); |
64
|
|
|
|
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return SavableMetricInterface|object |
70
|
|
|
* @throws \yii\base\InvalidConfigException |
71
|
|
|
*/ |
72
|
|
|
protected function resolveMetric() |
73
|
|
|
{ |
74
|
|
|
if ($this->metric instanceof SavableMetricInterface) { |
75
|
|
|
return $this->metric; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->metric = Instance::ensure( |
79
|
|
|
$this->metric, |
80
|
|
|
SavableMetricInterface::class |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
return $this->metric; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritdoc |
88
|
|
|
*/ |
89
|
|
|
protected function defaultDescription(): string |
90
|
|
|
{ |
91
|
|
|
return Craft::t('scorecard', 'Calculate Metric'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string |
96
|
|
|
* @throws \yii\base\InvalidConfigException |
97
|
|
|
*/ |
98
|
|
|
public function serialize() |
99
|
|
|
{ |
100
|
|
|
return serialize( |
101
|
|
|
[ |
102
|
|
|
$this->resolveMetric()->toConfig() |
103
|
|
|
] |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $data |
109
|
|
|
*/ |
110
|
|
|
public function unserialize($data) |
111
|
|
|
{ |
112
|
|
|
list($this->metric) = unserialize($data); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|