Completed
Push — master ( cfd235...bd5dca )
by Dmitry
02:31
created

PackageUpdateCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 59
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 25 4
B transformException() 0 28 5
1
<?php
2
3
namespace hiqdev\assetpackagist\commands;
4
5
use hiqdev\assetpackagist\exceptions\CorruptedPackageException;
6
use hiqdev\assetpackagist\exceptions\PackageNotExistsException;
7
use hiqdev\assetpackagist\exceptions\PermanentProblemExceptionInterface;
8
use Yii;
9
10
/**
11
 * Class PackageUpdateCommand runs package update command and creates tasks to
12
 * fetch its dependencies.
13
 *
14
 * @package hiqdev\assetpackagist\commands
15
 */
16
class PackageUpdateCommand extends AbstractPackageCommand
17
{
18
    public function run()
19
    {
20
        $this->beforeRun();
21
22
        if (!$this->package->canBeUpdated()) {
23
            if (!$this->packageRepository->exists($this->package)) {
24
                $this->packageRepository->insert($this->package);
25
            }
26
        } else {
27
            try {
28
                $this->package->update();
29
                $this->packageRepository->save($this->package);
30
            } catch (\Exception $e) {
31
                Yii::error('Failed to update package "' . $this->package->getFullName() . '": ' . $e->getMessage(), __CLASS__);
32
                $this->transformException($e);
33
34
                throw $e;
35
            }
36
37
            Yii::$app->queue->push(Yii::createObject(CollectDependenciesCommand::class, [$this->package]));
38
        }
39
40
41
        $this->afterRun();
42
    }
43
44
    private function transformException(\Exception $e)
45
    {
46
        $avoidMarkers = [
47
            'file could not be downloaded (HTTP/1.1 404 Not Found)' => PackageNotExistsException::class,
48
            'npm asset package must be present for create a VCS Repository' => CorruptedPackageException::class,
49
            'Could not parse version constraint' => CorruptedPackageException::class,
50
        ];
51
52
        foreach ($avoidMarkers as $marker => $exceptionClass) {
53
            if (!stripos($e->getMessage(), $marker)) {
54
                continue;
55
            }
56
57
            $newException = new $exceptionClass($e->getMessage(), 0, $e);
58
59
            if (
60
                $newException instanceof PermanentProblemExceptionInterface
61
                && $this->packageRepository->exists($this->package)
62
            ) {
63
                Yii::warning('Package ' . $this->package->getFullName() . ' is marked as avoided', __CLASS__);
64
                $this->packageRepository->markAvoided($this->package);
65
            }
66
67
            throw $newException;
68
        }
69
70
        return false;
71
    }
72
73
74
}
75