‪TYPO3CMS  ‪main
AbstractTask.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
24 
28 abstract class ‪AbstractTask implements ‪TaskInterface
29 {
31  protected bool ‪$executed = false;
32  protected bool ‪$successful;
33 
34  public function ‪__construct(
35  protected ‪ProcessedFile $targetFile,
36  protected array $configuration
37  ) {
38  $this->sourceFile = $targetFile->‪getOriginalFile();
39  }
40 
46  protected function ‪getChecksumData(): array
47  {
48  return [
49  $this->‪getSourceFile()->getUid(),
50  $this->‪getType() . '.' . $this->‪getName() . $this->‪getSourceFile()->getModificationTime(),
51  (new ‪ConfigurationService())->serialize($this->configuration),
52  ];
53  }
54 
58  public function ‪getConfigurationChecksum(): string
59  {
60  return substr((string)md5(implode('|', $this->‪getChecksumData())), 0, 10);
61  }
62 
66  public function ‪getTargetFilename(): string
67  {
68  return $this->targetFile->getNameWithoutExtension()
69  . '_' . $this->‪getConfigurationChecksum()
70  . '.' . $this->‪getTargetFileExtension();
71  }
72 
77  public function ‪getTargetFileExtension(): string
78  {
79  return $this->targetFile->getExtension();
80  }
81 
85  abstract public function ‪getName(): string;
86 
90  abstract public function ‪getType(): string;
91 
92  public function ‪getTargetFile(): Resource\‪ProcessedFile
93  {
94  return $this->targetFile;
95  }
96 
97  public function ‪getSourceFile(): Resource\‪File
98  {
99  return ‪$this->sourceFile;
100  }
101 
102  public function ‪getConfiguration(): array
103  {
104  return $this->configuration;
105  }
106 
110  public function ‪isExecuted(): bool
111  {
112  return ‪$this->executed;
113  }
114 
121  public function ‪setExecuted(bool ‪$successful): void
122  {
123  $this->executed = true;
124  $this->successful = ‪$successful;
125  }
126 
133  public function ‪isSuccessful(): bool
134  {
135  if (!$this->executed) {
136  throw new \LogicException('Task has not been executed; cannot determine success.', 1352549235);
137  }
138  return ‪$this->successful;
139  }
140 
146  public function ‪fileNeedsProcessing(): bool
147  {
148  $processedFile = $this->‪getTargetFile();
149  if (!$processedFile->isProcessed()) {
150  return true;
151  }
152  return $processedFile->isNew() || (!$processedFile->usesOriginalFile() && !$processedFile->exists()) || $processedFile->isOutdated();
153  }
154 
162  public function ‪sanitizeConfiguration(): void
163  {
164  foreach ($this->configuration as &$value) {
166  $value = (int)$value;
167  }
168  }
169  // @todo: ideally we would do a sort() on the array to really structure this, but then the checksums would change
170  // @todo: and we would need to re-create all processed files again, but this would be something we should tackle at some point
171  }
172 }
‪TYPO3\CMS\Core\Resource\Processing\AbstractTask\getConfigurationChecksum
‪getConfigurationChecksum()
Definition: AbstractTask.php:58
‪TYPO3\CMS\Core\Resource\Processing\AbstractTask\isExecuted
‪isExecuted()
Definition: AbstractTask.php:110
‪TYPO3\CMS\Core\Resource\Processing\AbstractTask\getSourceFile
‪getSourceFile()
Definition: AbstractTask.php:97
‪TYPO3\CMS\Core\Resource\Processing\AbstractTask\getName
‪getName()
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface
Definition: TaskInterface.php:34
‪TYPO3\CMS\Core\Resource\Processing\AbstractTask\fileNeedsProcessing
‪fileNeedsProcessing()
Definition: AbstractTask.php:146
‪TYPO3\CMS\Core\Resource\Processing\AbstractTask\__construct
‪__construct(protected ProcessedFile $targetFile, protected array $configuration)
Definition: AbstractTask.php:34
‪TYPO3\CMS\Core\Resource\Processing\AbstractTask\$successful
‪bool $successful
Definition: AbstractTask.php:32
‪TYPO3\CMS\Core\Resource\Processing
Definition: AbstractTask.php:18
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Core\Resource\Processing\AbstractTask\getTargetFileExtension
‪getTargetFileExtension()
Definition: AbstractTask.php:77
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Resource\Processing\AbstractTask\isSuccessful
‪isSuccessful()
Definition: AbstractTask.php:133
‪TYPO3\CMS\Core\Resource\Processing\AbstractTask\$sourceFile
‪Resource File $sourceFile
Definition: AbstractTask.php:30
‪TYPO3\CMS\Core\Resource\Processing\AbstractTask\getType
‪getType()
‪TYPO3\CMS\Core\Resource\Processing\AbstractTask\setExecuted
‪setExecuted(bool $successful)
Definition: AbstractTask.php:121
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:47
‪TYPO3\CMS\Core\Resource\Processing\AbstractTask\sanitizeConfiguration
‪sanitizeConfiguration()
Definition: AbstractTask.php:162
‪TYPO3\CMS\Core\Resource\Service\ConfigurationService
Definition: ConfigurationService.php:34
‪TYPO3\CMS\Core\Resource\Processing\AbstractTask
Definition: AbstractTask.php:29
‪TYPO3\CMS\Core\Resource\ProcessedFile\getOriginalFile
‪getOriginalFile()
Definition: ProcessedFile.php:273
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Resource\Processing\AbstractTask\$executed
‪bool $executed
Definition: AbstractTask.php:31
‪TYPO3\CMS\Core\Resource\Processing\AbstractTask\getConfiguration
‪getConfiguration()
Definition: AbstractTask.php:102
‪TYPO3\CMS\Core\Resource\Processing\AbstractTask\getTargetFile
‪getTargetFile()
Definition: AbstractTask.php:92
‪TYPO3\CMS\Core\Resource\Processing\AbstractTask\getTargetFilename
‪getTargetFilename()
Definition: AbstractTask.php:66
‪TYPO3\CMS\Core\Resource\Processing\AbstractTask\getChecksumData
‪getChecksumData()
Definition: AbstractTask.php:46