GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#247)
by
unknown
01:27
created

AbstractQueuedJob::getRunAsMember()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Symbiote\QueuedJobs\Services;
4
5
use SilverStripe\ORM\FieldType\DBDatetime;
6
use SilverStripe\Subsites\State\SubsiteState;
7
use stdClass;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\ORM\DataObject;
10
11
/**
12
 * A base implementation of a queued job that provides some convenience for implementations
13
 *
14
 * This implementation assumes that when you created your job class, you initialised the
15
 * jobData with relevant variables needed to process() your job later on in execution. If you do not,
16
 * please ensure you do before you queueJob() the job, to ensure the signature that is generated is 'correct'.
17
 *
18
 * @author Marcus Nyeholt <[email protected]>
19
 * @license BSD http://silverstripe.org/bsd-license/
20
 * @skipUpgrade
21
 */
22
abstract class AbstractQueuedJob implements QueuedJob
23
{
24
    /**
25
     * @var stdClass
26
     */
27
    protected $jobData;
28
29
    /**
30
     * @var array
31
     */
32
    protected $messages = array();
33
34
    /**
35
     * @var int
36
     */
37
    protected $totalSteps = 0;
38
39
    /**
40
     * @var int
41
     */
42
    protected $currentStep = 0;
43
44
    /**
45
     * @var boolean
46
     */
47
    protected $isComplete = false;
48
49
    /**
50
     * Extensions can have a construct but don't have too.
51
     * Without a construct, it's impossible to create a job in the CMS
52
     * @var array params
53
     */
54
    public function __construct($params = array())
55
    {
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    abstract public function getTitle();
62
63
    /**
64
     * Sets a data object for persisting by adding its id and type to the serialised vars
65
     *
66
     * @param DataObject $object
67
     * @param string $name A name to give it, if you want to store more than one
68
     */
69
    protected function setObject(DataObject $object, $name = 'Object')
70
    {
71
        $this->{$name . 'ID'} = $object->ID;
72
        $this->{$name . 'Type'} = $object->ClassName;
73
    }
74
75
    /**
76
     * @param string $name
77
     * @return DataObject|null
78
     */
79
    protected function getObject($name = 'Object')
80
    {
81
        $id = $this->{$name . 'ID'};
82
        $type = $this->{$name . 'Type'};
83
        if ($id) {
84
            return DataObject::get_by_id($type, $id);
85
        }
86
    }
87
88
    /**
89
     * Return a signature for this queued job
90
     *
91
     * @return string
92
     */
93
    public function getSignature()
94
    {
95
        return md5(get_class($this) . serialize($this->jobData));
96
    }
97
98
    /**
99
     * Generate a somewhat random signature
100
     *
101
     * useful if you're want to make sure something is always added
102
     *
103
     * @return string
104
     */
105
    protected function randomSignature()
106
    {
107
        return md5(get_class($this) . DBDatetime::now()->getTimestamp() . mt_rand(0, 100000));
108
    }
109
110
    /**
111
     * By default jobs should just go into the default processing queue
112
     *
113
     * @return string
114
     */
115
    public function getJobType()
116
    {
117
        return QueuedJob::QUEUED;
118
    }
119
120
    /**
121
     * @return int|null
122
     */
123
    public function getRunAsMember()
124
    {
125
        return null;
126
    }
127
128
    /**
129
     * Performs setup tasks the first time this job is run.
130
     *
131
     * This is only executed once for every job. If you want to run something on every job restart, use the
132
     * {@link prepareForRestart} method.
133
     */
134
    public function setup()
135
    {
136
        $this->loadCustomConfig();
137
    }
138
139
    /**
140
     * Run when an already setup job is being restarted.
141
     */
142
    public function prepareForRestart()
143
    {
144
        $this->loadCustomConfig();
145
    }
146
147
    /**
148
     * Do some processing yourself!
149
     */
150
    abstract public function process();
151
152
    /**
153
     * Method for determining whether the job is finished - you may override it if there's
154
     * more to it than just this
155
     */
156
    public function jobFinished()
157
    {
158
        return $this->isComplete;
159
    }
160
161
    /**
162
     * Called when the job is determined to be 'complete'
163
     */
164
    public function afterComplete()
165
    {
166
    }
167
168
    /**
169
     * @return stdClass
170
     */
171
    public function getJobData()
172
    {
173
        // okay, we NEED to store the subsite ID if there's one available
174
        if (!$this->SubsiteID && class_exists(SubsiteState::class)) {
0 ignored issues
show
Documentation introduced by
The property SubsiteID does not exist on object<Symbiote\QueuedJo...ices\AbstractQueuedJob>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
175
            $this->SubsiteID = SubsiteState::singleton()->getSubsiteId();
0 ignored issues
show
Documentation introduced by
The property SubsiteID does not exist on object<Symbiote\QueuedJo...ices\AbstractQueuedJob>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
176
        }
177
178
        $data = new stdClass();
179
        $data->totalSteps = $this->totalSteps;
180
        $data->currentStep = $this->currentStep;
181
        $data->isComplete = $this->isComplete;
182
        $data->jobData = $this->jobData;
183
        $data->messages = $this->messages;
184
185
        return $data;
186
    }
187
188
    /**
189
     * @param int $totalSteps
190
     * @param int $currentStep
191
     * @param boolean $isComplete
192
     * @param stdClass $jobData
193
     * @param array $messages
194
     */
195
    public function setJobData($totalSteps, $currentStep, $isComplete, $jobData, $messages)
196
    {
197
        $this->totalSteps = $totalSteps;
198
        $this->currentStep = $currentStep;
199
        $this->isComplete = $isComplete;
200
        $this->jobData = $jobData;
201
        $this->messages = $messages;
202
    }
203
204
    /**
205
     * Gets custom config settings to use when running the job.
206
     *
207
     * @return array|null
208
     */
209
    public function getCustomConfig()
210
    {
211
        return $this->CustomConfig;
0 ignored issues
show
Documentation introduced by
The property CustomConfig does not exist on object<Symbiote\QueuedJo...ices\AbstractQueuedJob>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
212
    }
213
214
    /**
215
     * Sets custom config settings to use when the job is run.
216
     *
217
     * @param array $config
218
     */
219
    public function setCustomConfig(array $config)
220
    {
221
        $this->CustomConfig = $config;
0 ignored issues
show
Documentation introduced by
The property CustomConfig does not exist on object<Symbiote\QueuedJo...ices\AbstractQueuedJob>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
222
    }
223
224
    /**
225
     * Sets custom configuration settings from the job data.
226
     */
227
    private function loadCustomConfig()
228
    {
229
        $custom = $this->getCustomConfig();
230
231
        if (!is_array($custom)) {
232
            return;
233
        }
234
235
        foreach ($custom as $class => $settings) {
236
            foreach ($settings as $setting => $value) {
237
                Config::modify()->set($class, $setting, $value);
238
            }
239
        }
240
    }
241
242
    /**
243
     * @param string $message
244
     * @param string $severity
245
     */
246
    public function addMessage($message, $severity = 'INFO')
247
    {
248
        $severity = strtoupper($severity);
249
        $this->messages[] = '[' . DBDatetime::now()->Rfc2822() . "][$severity] $message";
250
    }
251
252
    /**
253
     * Convenience methods for setting and getting job data
254
     *
255
     * @param mixed $name
256
     * @param mixed $value
257
     */
258
    public function __set($name, $value)
259
    {
260
        if (!$this->jobData) {
261
            $this->jobData = new stdClass();
262
        }
263
        $this->jobData->$name = $value;
264
    }
265
266
    /**
267
     * Retrieve some job data
268
     *
269
     * @param mixed $name
270
     * @return mixed
271
     */
272
    public function __get($name)
273
    {
274
        return isset($this->jobData->$name) ? $this->jobData->$name : null;
275
    }
276
}
277