| Total Complexity | 45 |
| Total Lines | 382 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ExtensionManagementService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ExtensionManagementService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class ExtensionManagementService implements SingletonInterface |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var DownloadQueue |
||
| 39 | */ |
||
| 40 | protected $downloadQueue; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var DependencyUtility |
||
| 44 | */ |
||
| 45 | protected $dependencyUtility; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var InstallUtility |
||
| 49 | */ |
||
| 50 | protected $installUtility; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | protected $automaticInstallationEnabled = true; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var bool |
||
| 59 | */ |
||
| 60 | protected $skipDependencyCheck = false; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var EventDispatcherInterface |
||
| 64 | */ |
||
| 65 | protected $eventDispatcher; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var FileHandlingUtility |
||
| 69 | */ |
||
| 70 | protected $fileHandlingUtility; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var RemoteRegistry |
||
| 74 | */ |
||
| 75 | protected $remoteRegistry; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected $downloadPath = 'Local'; |
||
| 81 | |||
| 82 | public function __construct(RemoteRegistry $remoteRegistry, FileHandlingUtility $fileHandlingUtility) |
||
| 83 | { |
||
| 84 | $this->remoteRegistry = $remoteRegistry; |
||
| 85 | $this->fileHandlingUtility = $fileHandlingUtility; |
||
| 86 | } |
||
| 87 | |||
| 88 | public function injectEventDispatcher(EventDispatcherInterface $eventDispatcher) |
||
| 89 | { |
||
| 90 | $this->eventDispatcher = $eventDispatcher; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param DownloadQueue $downloadQueue |
||
| 95 | */ |
||
| 96 | public function injectDownloadQueue(DownloadQueue $downloadQueue) |
||
| 97 | { |
||
| 98 | $this->downloadQueue = $downloadQueue; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param DependencyUtility $dependencyUtility |
||
| 103 | */ |
||
| 104 | public function injectDependencyUtility(DependencyUtility $dependencyUtility) |
||
| 105 | { |
||
| 106 | $this->dependencyUtility = $dependencyUtility; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param InstallUtility $installUtility |
||
| 111 | */ |
||
| 112 | public function injectInstallUtility(InstallUtility $installUtility) |
||
| 113 | { |
||
| 114 | $this->installUtility = $installUtility; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param string $extensionKey |
||
| 119 | */ |
||
| 120 | public function markExtensionForInstallation($extensionKey) |
||
| 121 | { |
||
| 122 | // We have to check for dependencies of the extension first, before marking it for installation |
||
| 123 | // because this extension might have dependencies, which need to be installed first |
||
| 124 | $this->installUtility->reloadAvailableExtensions(); |
||
| 125 | $extension = $this->getExtension($extensionKey); |
||
| 126 | $this->dependencyUtility->checkDependencies($extension); |
||
| 127 | $this->downloadQueue->addExtensionToInstallQueue($extension); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Mark an extension for download |
||
| 132 | * |
||
| 133 | * @param Extension $extension |
||
| 134 | */ |
||
| 135 | public function markExtensionForDownload(Extension $extension) |
||
| 136 | { |
||
| 137 | // We have to check for dependencies of the extension first, before marking it for download |
||
| 138 | // because this extension might have dependencies, which need to be downloaded and installed first |
||
| 139 | $this->dependencyUtility->checkDependencies($extension); |
||
| 140 | if (!$this->dependencyUtility->hasDependencyErrors()) { |
||
| 141 | $this->downloadQueue->addExtensionToQueue($extension); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param Extension $extension |
||
| 147 | */ |
||
| 148 | public function markExtensionForUpdate(Extension $extension) |
||
| 149 | { |
||
| 150 | // We have to check for dependencies of the extension first, before marking it for download |
||
| 151 | // because this extension might have dependencies, which need to be downloaded and installed first |
||
| 152 | $this->dependencyUtility->checkDependencies($extension); |
||
| 153 | $this->downloadQueue->addExtensionToQueue($extension, 'update'); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Enables or disables the dependency check for system environment (PHP, TYPO3) before extension installation |
||
| 158 | * |
||
| 159 | * @param bool $skipDependencyCheck |
||
| 160 | */ |
||
| 161 | public function setSkipDependencyCheck($skipDependencyCheck) |
||
| 162 | { |
||
| 163 | $this->skipDependencyCheck = $skipDependencyCheck; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param bool $automaticInstallationEnabled |
||
| 168 | */ |
||
| 169 | public function setAutomaticInstallationEnabled($automaticInstallationEnabled) |
||
| 170 | { |
||
| 171 | $this->automaticInstallationEnabled = (bool)$automaticInstallationEnabled; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Install the extension |
||
| 176 | * |
||
| 177 | * @param Extension $extension |
||
| 178 | * @return bool|array Returns FALSE if dependencies cannot be resolved, otherwise array with installation information |
||
| 179 | */ |
||
| 180 | public function installExtension(Extension $extension) |
||
| 181 | { |
||
| 182 | $this->downloadMainExtension($extension); |
||
| 183 | if (!$this->checkDependencies($extension)) { |
||
| 184 | return false; |
||
| 185 | } |
||
| 186 | |||
| 187 | $downloadedDependencies = []; |
||
| 188 | $updatedDependencies = []; |
||
| 189 | $installQueue = []; |
||
| 190 | |||
| 191 | // First resolve all dependencies and the sub-dependencies until all queues are empty as new extensions might be |
||
| 192 | // added each time |
||
| 193 | // Extensions have to be installed in reverse order. Extensions which were added at last are dependencies of |
||
| 194 | // earlier ones and need to be available before |
||
| 195 | while (!$this->downloadQueue->isQueueEmpty('download') |
||
| 196 | || !$this->downloadQueue->isQueueEmpty('update') |
||
| 197 | ) { |
||
| 198 | $installQueue = array_merge($this->downloadQueue->resetExtensionInstallStorage(), $installQueue); |
||
| 199 | // Get download and update information |
||
| 200 | $queue = $this->downloadQueue->resetExtensionQueue(); |
||
| 201 | if (!empty($queue['download'])) { |
||
| 202 | $downloadedDependencies = array_merge($downloadedDependencies, $this->downloadDependencies($queue['download'])); |
||
| 203 | } |
||
| 204 | $installQueue = array_merge($this->downloadQueue->resetExtensionInstallStorage(), $installQueue); |
||
| 205 | if ($this->automaticInstallationEnabled) { |
||
| 206 | if (!empty($queue['update'])) { |
||
| 207 | $this->downloadDependencies($queue['update']); |
||
| 208 | $updatedDependencies = array_merge($updatedDependencies, $this->uninstallDependenciesToBeUpdated($queue['update'])); |
||
| 209 | } |
||
| 210 | $installQueue = array_merge($this->downloadQueue->resetExtensionInstallStorage(), $installQueue); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | // If there were any dependency errors we have to abort here |
||
| 215 | if ($this->dependencyUtility->hasDependencyErrors()) { |
||
| 216 | return false; |
||
| 217 | } |
||
| 218 | |||
| 219 | // Attach extension to install queue |
||
| 220 | $this->downloadQueue->addExtensionToInstallQueue($extension); |
||
| 221 | $installQueue += $this->downloadQueue->resetExtensionInstallStorage(); |
||
| 222 | $installedDependencies = []; |
||
| 223 | if ($this->automaticInstallationEnabled) { |
||
| 224 | $installedDependencies = $this->installDependencies($installQueue); |
||
| 225 | } |
||
| 226 | |||
| 227 | return array_merge($downloadedDependencies, $updatedDependencies, $installedDependencies); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Returns the unresolved dependency errors |
||
| 232 | * |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | public function getDependencyErrors() |
||
| 236 | { |
||
| 237 | return $this->dependencyUtility->getDependencyErrors(); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param string $extensionKey |
||
| 242 | * @return Extension |
||
| 243 | * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException |
||
| 244 | */ |
||
| 245 | public function getExtension($extensionKey) |
||
| 246 | { |
||
| 247 | return Extension::createFromExtensionArray( |
||
| 248 | $this->installUtility->enrichExtensionWithDetails($extensionKey) |
||
| 249 | ); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Checks if an extension is available in the system |
||
| 254 | * |
||
| 255 | * @param string $extensionKey |
||
| 256 | * @return bool |
||
| 257 | */ |
||
| 258 | public function isAvailable($extensionKey) |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param string $extensionKey |
||
| 265 | * @throws \TYPO3\CMS\Core\Package\Exception\InvalidPackageStateException if the package isn't available |
||
| 266 | * @throws \TYPO3\CMS\Core\Package\Exception\InvalidPackageKeyException if an invalid package key was passed |
||
| 267 | * @throws \TYPO3\CMS\Core\Package\Exception\InvalidPackagePathException if an invalid package path was passed |
||
| 268 | * @throws \TYPO3\CMS\Core\Package\Exception\InvalidPackageManifestException if no extension configuration file could be found |
||
| 269 | */ |
||
| 270 | public function reloadPackageInformation($extensionKey) |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Check dependencies for an extension and its required extensions |
||
| 277 | * |
||
| 278 | * @param Extension $extension |
||
| 279 | * @return bool Returns TRUE if all dependencies can be resolved, otherwise FALSE |
||
| 280 | */ |
||
| 281 | protected function checkDependencies(Extension $extension) |
||
| 282 | { |
||
| 283 | $this->dependencyUtility->setSkipDependencyCheck($this->skipDependencyCheck); |
||
| 284 | $this->dependencyUtility->checkDependencies($extension); |
||
| 285 | |||
| 286 | return !$this->dependencyUtility->hasDependencyErrors(); |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Uninstall extensions that will be updated |
||
| 291 | * This is not strictly necessary but cleaner all in all |
||
| 292 | * |
||
| 293 | * @param Extension[] $updateQueue |
||
| 294 | * @return array |
||
| 295 | */ |
||
| 296 | protected function uninstallDependenciesToBeUpdated(array $updateQueue) |
||
| 297 | { |
||
| 298 | $resolvedDependencies = []; |
||
| 299 | foreach ($updateQueue as $extensionToUpdate) { |
||
| 300 | $this->installUtility->uninstall($extensionToUpdate->getExtensionKey()); |
||
| 301 | $resolvedDependencies['updated'][$extensionToUpdate->getExtensionKey()] = $extensionToUpdate; |
||
| 302 | } |
||
| 303 | return $resolvedDependencies; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Install dependent extensions |
||
| 308 | * |
||
| 309 | * @param array $installQueue |
||
| 310 | * @return array |
||
| 311 | */ |
||
| 312 | protected function installDependencies(array $installQueue) |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Download dependencies |
||
| 331 | * expects an array of extension objects to download |
||
| 332 | * |
||
| 333 | * @param Extension[] $downloadQueue |
||
| 334 | * @return array |
||
| 335 | */ |
||
| 336 | protected function downloadDependencies(array $downloadQueue) |
||
| 337 | { |
||
| 338 | $resolvedDependencies = []; |
||
| 339 | foreach ($downloadQueue as $extensionToDownload) { |
||
| 340 | $this->rawDownload($extensionToDownload); |
||
| 341 | $this->downloadQueue->removeExtensionFromQueue($extensionToDownload); |
||
| 342 | $resolvedDependencies['downloaded'][$extensionToDownload->getExtensionKey()] = $extensionToDownload; |
||
| 343 | $this->markExtensionForInstallation($extensionToDownload->getExtensionKey()); |
||
| 344 | } |
||
| 345 | return $resolvedDependencies; |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Get and resolve dependencies |
||
| 350 | * |
||
| 351 | * @param Extension $extension |
||
| 352 | * @return array |
||
| 353 | */ |
||
| 354 | public function getAndResolveDependencies(Extension $extension) |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Downloads the extension the user wants to install |
||
| 367 | * This is separated from downloading the dependencies |
||
| 368 | * as an extension is able to provide it's own dependencies |
||
| 369 | * |
||
| 370 | * @param Extension $extension |
||
| 371 | */ |
||
| 372 | public function downloadMainExtension(Extension $extension) |
||
| 373 | { |
||
| 374 | // The extension object has a uid if the extension is not present in the system |
||
| 375 | // or an update of a present extension is triggered. |
||
| 376 | if ($extension->getUid()) { |
||
|
|
|||
| 377 | $this->rawDownload($extension); |
||
| 378 | } |
||
| 379 | } |
||
| 380 | |||
| 381 | protected function rawDownload(Extension $extension): void |
||
| 382 | { |
||
| 383 | if ( |
||
| 384 | Environment::isComposerMode() |
||
| 385 | || (bool)GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('extensionmanager', 'offlineMode') |
||
| 386 | ) { |
||
| 387 | throw new ExtensionManagerException('Extension Manager is in offline mode. No TER connection available.', 1437078620); |
||
| 388 | } |
||
| 389 | |||
| 390 | $remoteIdentifier = $extension->getRemoteIdentifier(); |
||
| 391 | |||
| 392 | if ($this->remoteRegistry->hasRemote($remoteIdentifier)) { |
||
| 393 | $this->remoteRegistry |
||
| 394 | ->getRemote($remoteIdentifier) |
||
| 395 | ->downloadExtension( |
||
| 396 | $extension->getExtensionKey(), |
||
| 397 | $extension->getVersion(), |
||
| 398 | $this->fileHandlingUtility, |
||
| 399 | $extension->getMd5hash(), |
||
| 400 | $this->downloadPath |
||
| 401 | ); |
||
| 402 | } |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Set the download path |
||
| 407 | * |
||
| 408 | * @param string $downloadPath |
||
| 409 | * @throws ExtensionManagerException |
||
| 410 | */ |
||
| 411 | public function setDownloadPath(string $downloadPath): void |
||
| 417 | } |
||
| 418 | } |
||
| 419 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: