Completed
Push — master ( 1aa2d2...b07d11 )
by Dmitry
15:52
created

PackageUpdateCommand::execute()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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