Completed
Push — develop ( cb76c7...f2525d )
by Nate
11:30
created

CalculateAndSaveMetric::unserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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