Completed
Pull Request — v3-ssp (#169)
by Garion
01:33
created

BuildAddonJob::getTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * A background job which builds a single add-on.
4
 */
5
class BuildAddonJob extends AbstractQueuedJob implements QueuedJob
6
{
7
    private $packageID;
8
9
    /**
10
     * @var array params
11
     * @throws Exception
12
     */
13
    public function __construct($params = array()) {
14
        if (!empty($params['package'])) {
15
            $this->setObject(Addon::get()->byID($params['package']));
0 ignored issues
show
Bug introduced by
It seems like \Addon::get()->byID($params['package']) can be null; however, setObject() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
16
        }
17
    }
18
19
    public function setUp()
20
    {
21
        global $databaseConfig;
22
23
        if (!DB::isActive()) {
0 ignored issues
show
Deprecated Code introduced by
The method DB::isActive() has been deprecated with message: since version 4.0 Use DB::is_active instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
24
            DB::connect($databaseConfig);
25
        }
26
    }
27
28
    public function perform()
29
    {
30
        $package = $this->getObject();
31
        if (!$package->ID) throw new Exception('Package not specified');
32
33
        $builder = Injector::inst()->get('AddonBuilder');
34
35
        $builder->build($package);
36
37
        $package->BuildQueued = false;
38
        $package->write();
39
40
        $this->isComplete = true;
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    public function getTitle()
47
    {
48
        return 'Build Extended Addon Information';
49
    }
50
    /**
51
     * Return a signature for this queued job
52
     *
53
     * @return string
54
     */
55
    public function getSignature() {
56
        return md5(get_class($this) . serialize($this->jobData) . $this->packageID);
57
    }
58
59
    /**
60
     * @return stdClass
61
     */
62
    public function getJobData() {
63
        $data = new stdClass();
64
        $data->totalSteps = $this->totalSteps;
65
        $data->currentStep = $this->currentStep;
66
        $data->isComplete = $this->isComplete;
67
        $data->jobData = $this->jobData;
68
        $data->messages = $this->messages;
69
        $data->packageID = $this->packageID;
70
71
        return $data;
72
    }
73
74
    /**
75
     * Do some processing yourself!
76
     */
77
    public function process()
78
    {
79
        $this->setUp();
80
        $this->perform();
81
    }
82
}
83