Total Complexity | 56 |
Total Lines | 463 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Complex classes like MODXPackages 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 MODXPackages, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class MODXPackages |
||
15 | { |
||
16 | /** @var string */ |
||
17 | protected $date_format = ''; |
||
18 | |||
19 | /** @var \modX */ |
||
20 | protected $modx; |
||
21 | |||
22 | /** @var array $providerCache */ |
||
23 | protected $providerCache = array(); |
||
24 | |||
25 | protected $provider_map = []; |
||
26 | |||
27 | /** @var int $updates_cache_expire */ |
||
28 | protected $updates_cache_expire = 300; |
||
29 | |||
30 | /** @var array */ |
||
31 | protected $possible_package_signatures = []; |
||
32 | |||
33 | /** @var \LCI\MODX\Console\Helpers\UserInteractionHandler */ |
||
34 | protected $userInteractionHandler; |
||
35 | |||
36 | /** |
||
37 | * MODXPackages constructor. |
||
38 | * @param modX $modx |
||
39 | * @param UserInteractionHandler $userInteractionHandler |
||
40 | */ |
||
41 | public function __construct(modX $modx, UserInteractionHandler $userInteractionHandler) |
||
48 | } |
||
49 | |||
50 | // Code ideas for MODX packages from: core/model/modx/processors/workspace/packages/getlist.class.php and |
||
51 | // https://github.com/modmore/SiteDashClient/blob/master/core/components/sitedashclient/src/Package/Update.php |
||
52 | |||
53 | /** |
||
54 | * Get basic version information about the package |
||
55 | * |
||
56 | * @param string $signature |
||
57 | * @return array |
||
58 | */ |
||
59 | public static function getVersionInfo($signature) |
||
66 | ]; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param int $limit |
||
71 | * @param int $start |
||
72 | * @param string $search |
||
73 | * @return array |
||
74 | */ |
||
75 | public function getList($limit=25, $start=0, $search='') { |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * This is only needed if TransportNotFoundException is caught |
||
125 | * @return array |
||
126 | */ |
||
127 | public function getPossiblePackageSignatures(): array |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param string $signature - ex: ace-1.8.0-pl |
||
134 | * @param bool $latest_version |
||
135 | * @param string $provider_name |
||
136 | * @return bool |
||
137 | * @throws TransportException |
||
138 | * @throws TransportNotFoundException |
||
139 | */ |
||
140 | public function requirePackage($signature, $latest_version=true, $provider_name='modx.com') |
||
141 | { |
||
142 | // partial signature like fred sent, so is this package installed? |
||
143 | $package_info = static::getVersionInfo($signature); |
||
144 | |||
145 | // get the latest installed, might sort for the release column |
||
146 | $query = $this->modx->newQuery('transport.modTransportPackage'); |
||
147 | $query->where(['signature:LIKE' => $package_info['base'].'-%']); |
||
148 | $query->sortby('version_major', 'DESC'); |
||
149 | $query->sortby('version_minor', 'DESC'); |
||
150 | $query->sortby('version_patch', 'DESC'); |
||
151 | $query->sortby('release_index', 'ASC'); |
||
152 | $query->limit(1); |
||
153 | |||
154 | /** @var modTransportPackage $package */ |
||
155 | $package = $this->modx->getObject('transport.modTransportPackage', $query); |
||
156 | |||
157 | $type = 'install'; |
||
158 | |||
159 | if ($package instanceof \modTransportPackage) { |
||
160 | $signature = $package->get('signature'); |
||
161 | $provider = $this->getPackageProvider($package); |
||
162 | $type = 'update'; |
||
163 | |||
164 | } else { |
||
165 | $provider = $this->getProvider($provider_name); |
||
166 | } |
||
167 | |||
168 | $transfer_options = []; |
||
169 | |||
170 | // this only returns the |
||
171 | $options = $this->getPackageLatestVersions($provider, $signature); |
||
172 | |||
173 | if (isset($options[$signature])) { |
||
174 | $transfer_options['location'] = $options[$signature]['location']; |
||
175 | } |
||
176 | |||
177 | if ($latest_version && count($options) > 0) { |
||
178 | $opt = reset($options); |
||
179 | |||
180 | $signature = $opt['signature']; |
||
181 | $transfer_options['location'] = $opt['location']; |
||
182 | |||
183 | } elseif ($type == 'update' && $package instanceof modTransportPackage && !empty($package->get('installed'))) { |
||
184 | MODXPackagesConfig::addPackageConfig($signature, $latest_version, $provider_name); |
||
185 | $this->userInteractionHandler->tellUser('Extra '.$signature.' is already installed, skipping!', userInteractionHandler::MASSAGE_ERROR); |
||
186 | return true; |
||
187 | } |
||
188 | |||
189 | if (!$package instanceof modTransportPackage || ($package instanceof modTransportPackage && $package->get('signature') != $signature)) { |
||
190 | $package = $this->downloadPackageFiles($signature, $provider, $latest_version); |
||
191 | } |
||
192 | |||
193 | if (!$package instanceof \modTransportPackage) { |
||
194 | $this->userInteractionHandler->tellUser('Extra '.$signature.' not found', userInteractionHandler::MASSAGE_ERROR); |
||
195 | return false; |
||
196 | } |
||
197 | |||
198 | if ($success = $this->runPackageInstallUpdate($package)) { |
||
199 | MODXPackagesConfig::addPackageConfig($signature, $latest_version, $provider_name); |
||
200 | } |
||
201 | |||
202 | return $success; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @param string $signature - ex: ace-1.8.0-pl |
||
207 | * @param bool $force |
||
208 | * @return bool |
||
209 | * @throws TransportException |
||
210 | */ |
||
211 | public function removePackage($signature, $force=true) |
||
212 | { |
||
213 | /** @var modTransportPackage $package */ |
||
214 | $package = $this->modx->getObject('transport.modTransportPackage', [ |
||
215 | 'signature' => $signature, |
||
216 | ]); |
||
217 | |||
218 | if ($package instanceof \modTransportPackage) { |
||
219 | $package->getTransport(); |
||
220 | |||
221 | if (!$success = $package->removePackage($force)) { |
||
222 | throw new TransportException('Error Package did not uninstall.'); |
||
223 | } |
||
224 | |||
225 | $this->userInteractionHandler->tellUser('Extra '.$signature.' has been removed', userInteractionHandler::MASSAGE_SUCCESS); |
||
226 | |||
227 | $this->modx->cacheManager->refresh([ |
||
228 | $this->modx->getOption('cache_packages_key', null, 'packages') => [] |
||
229 | ]); |
||
230 | $this->modx->cacheManager->refresh(); |
||
231 | |||
232 | } else { |
||
233 | throw new TransportException('Package with the signature: '.$signature.' does not seem to be installed. '. |
||
234 | 'Run the extra command to see list of installed extras with proper signatures. You can only remove packages that are installed.'); |
||
235 | } |
||
236 | |||
237 | return $success; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @param string $signature - ex: ace-1.8.0-pl |
||
242 | * @param int $preexisting_mode, see xPDOTransport |
||
243 | * xPDOTransport::PRESERVE_PREEXISTING = 0; |
||
244 | xPDOTransport::REMOVE_PREEXISTING = 1; |
||
245 | xPDOTransport::RESTORE_PREEXISTING = 2; |
||
246 | * @return bool |
||
247 | * @throws TransportException |
||
248 | */ |
||
249 | public function unInstallPackage($signature, $preexisting_mode=null) |
||
250 | { |
||
251 | /** @var modTransportPackage $package */ |
||
252 | $package = $this->modx->getObject('transport.modTransportPackage', [ |
||
253 | 'signature' => $signature, |
||
254 | ]); |
||
255 | |||
256 | if ($package instanceof \modTransportPackage) { |
||
257 | $package->getTransport(); |
||
258 | /* uninstall package */ |
||
259 | $options = array( |
||
260 | xPDOTransport::PREEXISTING_MODE => is_null($preexisting_mode) ? xPDOTransport::REMOVE_PREEXISTING : $preexisting_mode, |
||
261 | ); |
||
262 | |||
263 | if (!$success = $package->uninstall($options)) { |
||
264 | throw new TransportException('Error Package did not uninstall.'); |
||
265 | } |
||
266 | |||
267 | $this->userInteractionHandler->tellUser('Extra '.$signature.' has been uninstalled', userInteractionHandler::MASSAGE_SUCCESS); |
||
268 | |||
269 | $this->modx->cacheManager->refresh([ |
||
270 | $this->modx->getOption('cache_packages_key', null, 'packages') => [] |
||
271 | ]); |
||
272 | $this->modx->cacheManager->refresh(); |
||
273 | |||
274 | } else { |
||
275 | throw new TransportException('Package with the signature: '.$signature.' does not seem to be installed. '. |
||
276 | 'Run the extra command to see list of installed extras with proper signatures. You can only remove packages that are installed.'); |
||
277 | } |
||
278 | |||
279 | return $success; |
||
280 | } |
||
281 | |||
282 | // @TODO local packages |
||
283 | |||
284 | /** |
||
285 | * @param string $signature - ex: ace-1.8.0-pl |
||
286 | * @param modTransportProvider $provider |
||
287 | * @param bool $latest |
||
288 | * @throws TransportNotFoundException |
||
289 | * @return bool|modTransportPackage |
||
290 | */ |
||
291 | protected function downloadPackageFiles($signature, modTransportProvider $provider, $latest=true) |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * @param modTransportPackage $package |
||
325 | * @return bool |
||
326 | * @throws TransportException |
||
327 | */ |
||
328 | protected function runPackageInstallUpdate(modTransportPackage $package) |
||
329 | { |
||
330 | $installed = $package->install([]); |
||
331 | $this->modx->cacheManager->refresh([ |
||
332 | $this->modx->getOption('cache_packages_key', null, 'packages') => [] |
||
333 | ]); |
||
334 | $this->modx->cacheManager->refresh(); |
||
335 | |||
336 | if (!$installed) { |
||
337 | throw new TransportException('Failed to install package ' . $package->signature); |
||
338 | } |
||
339 | |||
340 | $this->userInteractionHandler->tellUser('Extra '.$package->get('signature').' has been installed!', userInteractionHandler::MASSAGE_SUCCESS); |
||
341 | |||
342 | $this->modx->invokeEvent('OnPackageInstall', array( |
||
343 | 'package' => $package, |
||
344 | 'action' => $package->previousVersionInstalled() ? \xPDOTransport::ACTION_UPGRADE : \xPDOTransport::ACTION_INSTALL |
||
345 | )); |
||
346 | |||
347 | return $installed; |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * Format installed, created and updated dates |
||
352 | * @param array $packageArray |
||
353 | * @return array |
||
354 | */ |
||
355 | protected function formatDates(array $packageArray) |
||
356 | { |
||
357 | if ($packageArray['updated'] != '0000-00-00 00:00:00' && $packageArray['updated'] != null) { |
||
358 | $packageArray['updated'] = utf8_encode(date($this->date_format, strtotime($packageArray['updated']))); |
||
359 | } else { |
||
360 | $packageArray['updated'] = ''; |
||
361 | } |
||
362 | |||
363 | $packageArray['created']= utf8_encode(date($this->date_format, strtotime($packageArray['created']))); |
||
364 | |||
365 | if ($packageArray['installed'] == null || $packageArray['installed'] == '0000-00-00 00:00:00') { |
||
366 | $packageArray['installed'] = null; |
||
367 | } else { |
||
368 | $packageArray['installed'] = utf8_encode(date($this->date_format, strtotime($packageArray['installed']))); |
||
369 | } |
||
370 | return $packageArray; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * @param modTransportPackage $package |
||
375 | * @return array |
||
376 | */ |
||
377 | protected function getAvailableUpdateInfo(modTransportPackage $package) |
||
378 | { |
||
379 | $updates = [ |
||
380 | 'count' => 0, |
||
381 | 'versions' => [] |
||
382 | ]; |
||
383 | if ($package->get('provider') > 0 && $this->modx->getOption('auto_check_pkg_updates',null,false)) { |
||
384 | $updateCacheKey = 'mgr/providers/updates/'.$package->get('provider').'/'.$package->get('signature'); |
||
385 | $updateCacheOptions = array( |
||
386 | xPDO::OPT_CACHE_KEY => $this->modx->cacheManager->getOption('cache_packages_key', null, 'packages'), |
||
387 | xPDO::OPT_CACHE_HANDLER => $this->modx->cacheManager->getOption('cache_packages_handler', null, $this->modx->cacheManager->getOption(xPDO::OPT_CACHE_HANDLER)), |
||
388 | ); |
||
389 | $updates = $this->modx->cacheManager->get($updateCacheKey, $updateCacheOptions); |
||
390 | |||
391 | if (empty($updates)) { |
||
392 | /* cache providers to speed up load time */ |
||
393 | /** @var modTransportProvider $provider */ |
||
394 | $provider = $this->getPackageProvider($package); |
||
395 | |||
396 | if ($provider) { |
||
397 | $options = $this->getPackageLatestVersions($provider, $package->get('signature')); |
||
398 | |||
399 | $updates['count'] = count($options); |
||
400 | $updates['versions'] = $options; |
||
401 | |||
402 | $this->modx->cacheManager->set($updateCacheKey, $updates, $this->updates_cache_expire, $updateCacheOptions); |
||
403 | } |
||
404 | } |
||
405 | } |
||
406 | |||
407 | return $updates; |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * @param modTransportProvider $provider |
||
412 | * @param string $signature |
||
413 | * @return array - only returns values if the extra has a version that is more recent then the signature |
||
414 | */ |
||
415 | protected function getPackageLatestVersions(modTransportProvider $provider, $signature) |
||
416 | { |
||
417 | $package_versions = $provider->latest($signature); |
||
418 | |||
419 | $options = []; |
||
420 | /** @var \SimpleXMLElement $package */ |
||
421 | foreach ($package_versions as $package_version) { |
||
422 | $version_info = array_merge([ |
||
423 | 'location' => (string)$package_version['location'], |
||
424 | 'signature' => (string)$package_version['signature'], |
||
425 | 'package_name' => (string)$package_version['package_name'], |
||
426 | 'release' => '', |
||
427 | 'version' => '' |
||
428 | ], |
||
429 | self::getVersionInfo((string)$package_version['signature']) |
||
430 | ); |
||
431 | |||
432 | $options[$version_info['signature']] = $version_info; |
||
433 | } |
||
434 | |||
435 | return $options; |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * @param modTransportPackage $package |
||
440 | * @return modTransportProvider|bool |
||
441 | */ |
||
442 | protected function getPackageProvider(modTransportPackage $package) |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * @param string $name |
||
460 | * @return bool|modTransportProvider |
||
461 | */ |
||
462 | protected function getProvider($name='modx.com') |
||
477 | } |
||
478 | } |
||
479 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths