Completed
Push — master ( 8f0039...f6442f )
by Damian
8s
created

AbstractQueuedJob::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * A base implementation of a queued job that provides some convenience for implementations
5
 *
6
 * This implementation assumes that when you created your job class, you initialised the
7
 * jobData with relevant variables needed to process() your job later on in execution. If you do not,
8
 * please ensure you do before you queueJob() the job, to ensure the signature that is generated is 'correct'.
9
 *
10
 * @author Marcus Nyeholt <[email protected]>
11
 * @license BSD http://silverstripe.org/bsd-license/
12
 */
13
abstract class AbstractQueuedJob implements QueuedJob {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
14
	/**
15
	 * @var stdClass
16
	 */
17
	protected $jobData;
18
19
	/**
20
	 * @var array
21
	 */
22
	protected $messages = array();
23
24
	/**
25
	 * @var int
26
	 */
27
	protected $totalSteps = 0;
28
29
	/**
30
	 * @var int
31
	 */
32
	protected $currentStep = 0;
33
34
	/**
35
	 * @var boolean
36
	 */
37
	protected $isComplete = false;
38
	
39
	/**
40
	 * Extensions can have a construct but don't have too.
41
	 * Without a construct, it's impossible to create a job in the CMS
42
	 * @var array params
43
	 */
44
	public function __construct($params = array()) {
45
	    
46
	}
47
48
	/**
49
	 * @return string
50
	 */
51
	public abstract function getTitle();
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
52
53
	/**
54
	 * Sets a data object for persisting by adding its id and type to the serialised vars
55
	 *
56
	 * @param DataObject $object
57
	 * @param string $name A name to give it, if you want to store more than one
58
	 */
59
	protected function setObject(DataObject $object, $name = 'Object') {
60
		$this->{$name . 'ID'} = $object->ID;
61
		$this->{$name . 'Type'} = $object->ClassName;
62
	}
63
64
	/**
65
	 * @param string $name
66
	 * @return DataObject|void
67
	 */
68
	protected function getObject($name = 'Object') {
69
		$id = $this->{$name . 'ID'};
70
		$type = $this->{$name . 'Type'};
71
		if ($id) {
72
			return DataObject::get_by_id($type, $id);
73
		}
74
	}
75
76
	/**
77
	 * Return a signature for this queued job
78
	 *
79
	 * @return string
80
	 */
81
	public function getSignature() {
82
		return md5(get_class($this) . serialize($this->jobData));
83
	}
84
85
	/**
86
	 * Generate a somewhat random signature
87
	 *
88
	 * useful if you're want to make sure something is always added
89
	 *
90
	 * @return string
91
	 */
92
	protected function randomSignature() {
93
		return md5(get_class($this) . time() . mt_rand(0, 100000));
94
	}
95
96
	/**
97
	 * By default jobs should just go into the default processing queue
98
	 *
99
	 * @return string
100
	 */
101
	public function getJobType() {
102
		return QueuedJob::QUEUED;
103
	}
104
105
	/**
106
	 * Performs setup tasks the first time this job is run.
107
	 *
108
	 * This is only executed once for every job. If you want to run something on every job restart, use the
109
	 * {@link prepareForRestart} method.
110
	 */
111
	public function setup() {
112
		$this->loadCustomConfig();
113
	}
114
115
	/**
116
	 * Run when an already setup job is being restarted.
117
	 */
118
	public function prepareForRestart() {
119
		$this->loadCustomConfig();
120
	}
121
122
	/**
123
	 * Do some processing yourself!
124
	 */
125
	public abstract function process();
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
126
127
	/**
128
	 * Method for determining whether the job is finished - you may override it if there's
129
	 * more to it than just this
130
	 */
131
	public function jobFinished() {
132
		return $this->isComplete;
133
	}
134
135
	/**
136
	 * Called when the job is determined to be 'complete'
137
	 */
138
	public function afterComplete() {
139
140
	}
141
142
	/**
143
	 * @return stdClass
144
	 */
145
	public function getJobData() {
146
		// okay, we NEED to store the subsite ID if there's one available
147
		if (!$this->SubsiteID && class_exists('Subsite')) {
0 ignored issues
show
Documentation introduced by
The property SubsiteID does not exist on object<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...
148
			$this->SubsiteID = Subsite::currentSubsiteID();
0 ignored issues
show
Documentation introduced by
The property SubsiteID does not exist on object<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...
149
		}
150
151
		$data = new stdClass();
152
		$data->totalSteps = $this->totalSteps;
153
		$data->currentStep = $this->currentStep;
154
		$data->isComplete = $this->isComplete;
155
		$data->jobData = $this->jobData;
156
		$data->messages = $this->messages;
157
158
		return $data;
159
	}
160
161
	/**
162
	 * @param int $totalSteps
163
	 * @param int $currentStep
164
	 * @param boolean $isComplete
165
	 * @param stdClass $jobData
166
	 * @param array $messages
167
	 */
168
	public function setJobData($totalSteps, $currentStep, $isComplete, $jobData, $messages) {
169
		$this->totalSteps = $totalSteps;
170
		$this->currentStep = $currentStep;
171
		$this->isComplete = $isComplete;
172
		$this->jobData = $jobData;
173
		$this->messages = $messages;
174
	}
175
176
	/**
177
	 * Gets custom config settings to use when running the job.
178
	 *
179
	 * @return array|null
180
	 */
181
	public function getCustomConfig() {
182
		return $this->CustomConfig;
0 ignored issues
show
Documentation introduced by
The property CustomConfig does not exist on object<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...
183
	}
184
185
	/**
186
	 * Sets custom config settings to use when the job is run.
187
	 *
188
	 * @param array $config
189
	 */
190
	public function setCustomConfig(array $config) {
191
		$this->CustomConfig = $config;
0 ignored issues
show
Documentation introduced by
The property CustomConfig does not exist on object<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...
192
	}
193
194
	/**
195
	 * Sets custom configuration settings from the job data.
196
	 */
197
	private function loadCustomConfig() {
198
		$custom = $this->getCustomConfig();
199
200
		if (!is_array($custom)) {
201
			return;
202
		}
203
204
		foreach ($custom as $class => $settings) {
205
			foreach ($settings as $setting => $value) Config::inst()->update($class, $setting, $value);
206
		}
207
	}
208
209
	/**
210
	 * @param string $message
211
	 * @param string $severity
212
	 */
213
	public function addMessage($message, $severity = 'INFO') {
214
		$severity = strtoupper($severity);
215
		$this->messages[] = '[' . date('Y-m-d H:i:s') . "][$severity] $message";
216
	}
217
218
	/**
219
	 * Convenience methods for setting and getting job data
220
	 *
221
	 * @param mixed $name
222
	 * @param mixed $value
223
	 */
224
	public function __set($name, $value) {
225
		if (!$this->jobData) {
226
			$this->jobData = new stdClass();
227
		}
228
		$this->jobData->$name = $value;
229
	}
230
231
	/**
232
	 * Retrieve some job data
233
	 *
234
	 * @param mixed $name
235
	 * @return mixed
236
	 */
237
	public function __get($name) {
238
		return isset($this->jobData->$name) ? $this->jobData->$name : null;
239
	}
240
}
241