Completed
Push — master ( a5fb28...cfd235 )
by Dmitry
02:39
created

AssetPackageController::actionAddUpdateCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

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 5
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
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 Yii;
17
use yii\helpers\Console;
18
19
class AssetPackageController extends \yii\console\Controller
20
{
21
    /**
22
     * @param string $type the package type. Can be either `bower` or `npm`
23
     * @param string $name the package name
24
     * @return boolean Whether the update was successful
25
     */
26
    public function actionUpdate($type, $name)
27
    {
28
        try {
29
            $package = new AssetPackage($type, $name);
30
            Yii::createObject(PackageUpdateCommand::class, [$package])->run();
31
            echo 'updated ' . $package->getHash() . ' ' . $package->getFullName() . "\n";
32
33
            return true;
34
        } catch (\Exception $e) {
35
            echo Console::renderColoredString("%Rfailed%N $type/$name:%n {$e->getMessage()}\n");
36
37
            return false;
38
        }
39
    }
40
41
    public function actionUpdateList($file = STDIN)
42
    {
43
        $handler = is_resource($file) ? $file : fopen($file, 'r');
44
45
        $errorPackages = [];
46
47
        while ($line = fgets($handler)) {
48
            list($full) = preg_split('/\s+/', trim($line));
49
            list($type, $name) = AssetPackage::splitFullName($full);
50
            if (!$this->actionUpdate($type, $name)) {
51
                $errorPackages[] = $full;
52
            }
53
        }
54
55
        if (!is_resource($file)) {
56
            fclose($handler);
57
        }
58
59
        if (!empty($errorPackages)) {
60
            echo Console::renderColoredString("%RThe following packages were not updated due to unrecoverable errors:%n\n");
61
            echo implode("\n", $errorPackages);
62
        }
63
        echo "\n";
64
    }
65
66
    public function actionAddUpdateCommand($type, $name)
67
    {
68
        $package = new AssetPackage($type, $name);
69
        Yii::$app->queue->push(Yii::createObject(PackageUpdateCommand::class, [$package]));
70
        echo Console::renderColoredString("%GAdded%N $type/$name%n\n");
71
    }
72
73
    public function actionUpdateAll()
74
    {
75
        $this->actionUpdateList(Yii::getAlias('@hiqdev/assetpackagist/config/packages.list'));
76
    }
77
78
    public function actionList()
79
    {
80
        $packages = Yii::$app->get('packageStorage')->listPackages();
81
        ksort($packages);
82
        foreach ($packages as $name => $data) {
83
            echo "$name\n";
84
        }
85
    }
86
}
87