Completed
Push — master ( c9bd8a...63c7c8 )
by Dmitry
14:14
created

AssetPackageController::actionList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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