Completed
Push — master ( ec7bbc...a96ef4 )
by Dmitry
02:59 queued 13s
created

AssetPackageController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 4
crap 2
1
<?php
2
3
/*
4
 * Asset Packagist
5
 *
6
 * @link      https://github.com/hiqdev/asset-packagist
7
 * @package   asset-packagist
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\assetpackagist\console;
13
14
use hiqdev\assetpackagist\commands\PackageUpdateCommand;
15
use hiqdev\assetpackagist\models\AssetPackage;
16
use hiqdev\assetpackagist\repositories\PackageRepository;
17
use Yii;
18
use yii\helpers\Console;
19
20
class AssetPackageController extends \yii\console\Controller
21
{
22
     /**
23
     * @var PackageRepository
24
     */
25
    protected $packageRepository;
26
27
    /**
28
     * MaintenanceController constructor.
29
     * @param PackageRepository $packageRepository
30
     * @inheritdoc
31
     */
32
    public function __construct($id, $module, PackageRepository $packageRepository, $config = [])
33
    {
34
        parent::__construct($id, $module, $config);
35
36
        $this->packageRepository = $packageRepository;
37
    }
38
39
    /**
40
     * @param string $type the package type. Can be either `bower` or `npm`
41
     * @param string $name the package name
42
     * @return boolean Whether the update was successful
43
     */
44
    public function actionUpdate($type, $name)
45
    {
46
        try {
47
            $package = new AssetPackage($type, $name);
48
            Yii::createObject(PackageUpdateCommand::class, [$package])->run();
49
            echo 'updated ' . $package->getHash() . ' ' . $package->getFullName() . "\n";
50
51
            return true;
52
        } catch (\Exception $e) {
53
            echo Console::renderColoredString("%Rfailed%N $type/$name:%n {$e->getMessage()}\n");
54
55
            return false;
56
        }
57
    }
58
59
    public function actionUpdateList($file = STDIN)
60
    {
61
        $handler = is_resource($file) ? $file : fopen($file, 'r');
62
63
        $errorPackages = [];
64
65
        while ($line = fgets($handler)) {
66
            list($full) = preg_split('/\s+/', trim($line));
67
            list($type, $name) = AssetPackage::splitFullName($full);
68
            if (!$this->actionUpdate($type, $name)) {
69
                $errorPackages[] = $full;
70
            }
71
        }
72
73
        if (!is_resource($file)) {
74
            fclose($handler);
75
        }
76
77
        if (!empty($errorPackages)) {
78
            echo Console::renderColoredString("%RThe following packages were not updated due to unrecoverable errors:%n\n");
79
            echo implode("\n", $errorPackages);
80
        }
81
        echo "\n";
82
    }
83
84
    public function actionAddUpdateCommand($type, $name)
85
    {
86
        $package = new AssetPackage($type, $name);
87
        Yii::$app->queue->push(Yii::createObject(PackageUpdateCommand::class, [$package]));
88
        echo Console::renderColoredString("%GAdded%N $type/$name%n\n");
89
    }
90
91
    public function actionUpdateAll()
92
    {
93
        $this->actionUpdateList(Yii::getAlias('@hiqdev/assetpackagist/config/packages.list'));
94
    }
95
96
    public function actionAvoid($type, $name)
97
    {
98
        $package = new AssetPackage($type, $name);
99
        $this->packageRepository->markAvoided($package);
100
101
        echo Console::renderColoredString("Package %N$type/$name%n is %Ravoided%n now\n");
102
    }
103
104
    public function actionList()
105
    {
106
        $packages = Yii::$app->get('packageStorage')->listPackages();
107
        ksort($packages);
108
        foreach ($packages as $name => $data) {
109
            echo "$name\n";
110
        }
111
    }
112
}
113