[TASK] Remove leading slash from use statements
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Processing / AbstractTask.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Processing;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Resource;
18 use TYPO3\CMS\Core\Utility;
19
20 /**
21 * Abstract base implementation of a task.
22 *
23 * If you extend this class, make sure that you redefine the member variables $type and $name
24 * or set them in the constructor. Otherwise your task won't be recognized by the system and several
25 * things will fail.
26 */
27 abstract class AbstractTask implements TaskInterface {
28
29 /**
30 * @var array
31 */
32 protected $checksumData = array();
33
34 /**
35 * @var Resource\ProcessedFile
36 */
37 protected $targetFile;
38
39 /**
40 * @var Resource\File
41 */
42 protected $sourceFile;
43
44 /**
45 * @var array
46 */
47 protected $configuration;
48
49 /**
50 * @var string
51 */
52 protected $type;
53
54 /**
55 * @var string
56 */
57 protected $name;
58
59 /**
60 * @var bool
61 */
62 protected $executed = FALSE;
63
64 /**
65 * @var bool
66 */
67 protected $successful;
68
69 /**
70 * @param Resource\ProcessedFile $targetFile
71 * @param array $configuration
72 */
73 public function __construct(Resource\ProcessedFile $targetFile, array $configuration) {
74 $this->targetFile = $targetFile;
75 $this->sourceFile = $targetFile->getOriginalFile();
76 $this->configuration = $configuration;
77 }
78
79 /**
80 * Sets parameters needed in the checksum. Can be overridden to add additional parameters to the checksum.
81 * This should include all parameters that could possibly vary between different task instances, e.g. the
82 * TYPO3 image configuration in TYPO3_CONF_VARS[GFX] for graphic processing tasks.
83 *
84 * @return array
85 */
86 protected function getChecksumData() {
87 return array(
88 $this->getSourceFile()->getUid(),
89 $this->getType() . '.' . $this->getName(),
90 serialize($this->configuration)
91 );
92 }
93
94 /**
95 * Returns the checksum for this task's configuration, also taking the file and task type into account.
96 *
97 * @return string
98 */
99 public function getConfigurationChecksum() {
100 return \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5(implode('|', $this->getChecksumData()));
101 }
102
103 /**
104 * Returns the filename
105 *
106 * @return string
107 */
108 public function getTargetFilename() {
109 return $this->targetFile->getNameWithoutExtension()
110 . '_' . $this->getConfigurationChecksum()
111 . '.' . $this->getTargetFileExtension();
112 }
113
114 /**
115 * Gets the file extension the processed file should
116 * have in the filesystem.
117 *
118 * @return string
119 */
120 public function getTargetFileExtension() {
121 return $this->targetFile->getExtension();
122 }
123
124 /**
125 * Returns the name of this task
126 *
127 * @return string
128 */
129 public function getName() {
130 return $this->name;
131 }
132
133 /**
134 * Returns the type of this task
135 *
136 * @return string
137 */
138 public function getType() {
139 return $this->type;
140 }
141
142 /**
143 * @return Resource\ProcessedFile
144 */
145 public function getTargetFile() {
146 return $this->targetFile;
147 }
148
149 /**
150 * @param Resource\ProcessedFile $targetFile
151 */
152 public function setTargetFile(Resource\ProcessedFile $targetFile) {
153 $this->targetFile = $targetFile;
154 }
155
156 /**
157 * @return Resource\File
158 */
159 public function getSourceFile() {
160 return $this->sourceFile;
161 }
162
163 /**
164 * @param Resource\File $sourceFile
165 */
166 public function setSourceFile(Resource\File $sourceFile) {
167 $this->sourceFile = $sourceFile;
168 }
169
170 /**
171 * @return array
172 */
173 public function getConfiguration() {
174 return $this->configuration;
175 }
176
177 /**
178 * Checks if the given configuration is sensible for this task, i.e. if all required parameters
179 * are given, within the boundaries and don't conflict with each other.
180 *
181 * @param array $configuration
182 * @return bool
183 */
184 abstract protected function isValidConfiguration(array $configuration);
185
186 /**
187 * Returns TRUE if this task has been executed, no matter if the execution was successful.
188 *
189 * @return bool
190 */
191 public function isExecuted() {
192 return $this->executed;
193 }
194
195 /**
196 * Set this task executed. This is used by the Processors in order to transfer the state of this task to
197 * the file processing service.
198 *
199 * @param bool $successful Set this to FALSE if executing the task failed
200 * @return void
201 */
202 public function setExecuted($successful) {
203 $this->executed = TRUE;
204 $this->successful = $successful;
205 }
206
207 /**
208 * Returns TRUE if this task has been successfully executed. Only call this method if the task has been processed
209 * at all.
210 * @return bool
211 * @throws \LogicException If the task has not been executed already
212 */
213 public function isSuccessful() {
214 if (!$this->executed) {
215 throw new \LogicException('Task has not been executed; cannot determine success.', 1352549235);
216 }
217
218 return $this->successful;
219 }
220
221 }