Completed
Push — master ( c5ed18...ed0fce )
by Nathan
07:36
created

src/Jobs/DoormanQueuedJobTask.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Symbiote\QueuedJobs\Jobs;
4
5
use AsyncPHP\Doorman\Cancellable;
6
use AsyncPHP\Doorman\Expires;
7
use AsyncPHP\Doorman\Process;
8
use AsyncPHP\Doorman\Task;
9
use InvalidArgumentException;
10
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
11
use Symbiote\QueuedJobs\Services\QueuedJob;
12
13
class DoormanQueuedJobTask implements Task, Expires, Process, Cancellable
14
{
15
    /**
16
     * @var int
17
     */
18
    protected $id;
19
20
    /**
21
     * @var QueuedJobDescriptor
22
     */
23
    protected $descriptor;
24
25
    /**
26
     * Reload descriptor from DB
27
     */
28
    protected function refreshDescriptor()
29
    {
30
        if ($this->descriptor) {
31
            $this->descriptor = QueuedJobDescriptor::get()->byID($this->descriptor->ID);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Symbiote\QueuedJobs\Dat...($this->descriptor->ID) can also be of type object<SilverStripe\ORM\DataObject>. However, the property $descriptor is declared as type object<Symbiote\QueuedJo...ts\QueuedJobDescriptor>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
32
        }
33
    }
34
35
    /**
36
     * @inheritdoc
37
     *
38
     * @return null|int
39
     */
40
    public function getId()
41
    {
42
        return $this->id;
43
    }
44
45
    /**
46
     * @inheritdoc
47
     *
48
     * @param int $id
49
     *
50
     * @return $this
51
     */
52
    public function setId($id)
53
    {
54
        $this->id = $id;
55
56
        return $this;
57
    }
58
59
    /**
60
     * @return QueuedJobDescriptor
61
     */
62
    public function getDescriptor()
63
    {
64
        return $this->descriptor;
65
    }
66
67
    /**
68
     * @param QueuedJobDescriptor $descriptor
69
     */
70
    public function __construct(QueuedJobDescriptor $descriptor)
71
    {
72
        $this->descriptor = $descriptor;
73
    }
74
75
    /**
76
     * @inheritdoc
77
     *
78
     * @return string
79
     */
80
    public function serialize()
81
    {
82
        return serialize(array(
83
            'descriptor' => $this->descriptor->ID,
84
        ));
85
    }
86
87
    /**
88
     * @inheritdoc
89
     *
90
     * @throws InvalidArgumentException
91
     * @param string
92
     */
93
    public function unserialize($serialized)
94
    {
95
        $data = unserialize($serialized);
96
97
        if (!isset($data['descriptor'])) {
98
            throw new InvalidArgumentException('Malformed data');
99
        }
100
101
        $descriptor = QueuedJobDescriptor::get()
102
            ->filter('ID', $data['descriptor'])
103
            ->first();
104
105
        if (!$descriptor) {
106
            throw new InvalidArgumentException('Descriptor not found');
107
        }
108
109
        $this->descriptor = $descriptor;
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public function getHandler()
116
    {
117
        return 'DoormanQueuedJobHandler';
118
    }
119
120
    /**
121
     * @return array
122
     */
123
    public function getData()
124
    {
125
        return array(
126
            'descriptor' => $this->descriptor,
127
        );
128
    }
129
130
    /**
131
     * @return bool
132
     */
133
    public function ignoresRules()
134
    {
135
        if ($this->descriptor->hasMethod('ignoreRules')) {
136
            return $this->descriptor->ignoreRules();
0 ignored issues
show
Documentation Bug introduced by
The method ignoreRules does not exist on object<Symbiote\QueuedJo...ts\QueuedJobDescriptor>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
137
        }
138
139
        return false;
140
    }
141
142
    /**
143
     * @return bool
144
     */
145
    public function stopsSiblings()
146
    {
147
        if ($this->descriptor->hasMethod('stopsSiblings')) {
148
            return $this->descriptor->stopsSiblings();
0 ignored issues
show
Documentation Bug introduced by
The method stopsSiblings does not exist on object<Symbiote\QueuedJo...ts\QueuedJobDescriptor>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
149
        }
150
151
        return false;
152
    }
153
154
    /**
155
     * @inheritdoc
156
     *
157
     * @return int
158
     */
159
    public function getExpiresIn()
160
    {
161
        if ($this->descriptor->hasMethod('getExpiresIn')) {
162
            return $this->descriptor->getExpiresIn();
0 ignored issues
show
Documentation Bug introduced by
The method getExpiresIn does not exist on object<Symbiote\QueuedJo...ts\QueuedJobDescriptor>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
163
        }
164
165
        return -1;
166
    }
167
168
    /**
169
     * @inheritdoc
170
     *
171
     * @param int $startedAt
172
     * @return bool
173
     */
174
    public function shouldExpire($startedAt)
175
    {
176
        if ($this->descriptor->hasMethod('shouldExpire')) {
177
            return $this->descriptor->shouldExpire($startedAt);
0 ignored issues
show
Documentation Bug introduced by
The method shouldExpire does not exist on object<Symbiote\QueuedJo...ts\QueuedJobDescriptor>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
178
        }
179
180
        return true;
181
    }
182
183
    /**
184
     * @inheritdoc
185
     *
186
     * @return bool
187
     */
188
    public function canRunTask()
189
    {
190
        $this->refreshDescriptor();
191
        return in_array(
192
            $this->descriptor->JobStatus,
193
            array(
194
                QueuedJob::STATUS_NEW,
195
                QueuedJob::STATUS_INIT,
196
                QueuedJob::STATUS_WAIT
197
            )
198
        );
199
    }
200
201
    /**
202
     * @inheritdoc
203
     *
204
     * @return bool
205
     */
206
    public function isCancelled()
207
    {
208
        $this->refreshDescriptor();
209
        return $this->descriptor->JobStatus === QueuedJob::STATUS_CANCELLED;
210
    }
211
}
212