AssetPackageController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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