MaintenanceController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 0
loc 92
ccs 0
cts 54
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A actionSyncToDb() 0 18 3
A actionUpdateExpired() 0 14 2
B actionCheckHashes() 0 25 6
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\components\StorageInterface;
15
use hiqdev\assetpackagist\models\AssetPackage;
16
use hiqdev\assetpackagist\repositories\PackageRepository;
17
use Yii;
18
use yii\console\Controller;
19
use yii\helpers\Console;
20
21
/**
22
 * Provides maintenance actions for the asset-packagist service.
23
 */
24
class MaintenanceController extends Controller
25
{
26
    /**
27
     * @var StorageInterface
28
     */
29
    protected $packageStorage;
30
31
    /**
32
     * @var PackageRepository
33
     */
34
    protected $packageRepository;
35
36
    /**
37
     * MaintenanceController constructor.
38
     * @param StorageInterface $packageStorage
39
     * @param PackageRepository $packageRepository
40
     * {@inheritdoc}
41
     */
42
    public function __construct($id, $module, StorageInterface $packageStorage, PackageRepository $packageRepository, $config = [])
43
    {
44
        parent::__construct($id, $module, $config);
45
46
        $this->packageStorage = $packageStorage;
47
        $this->packageRepository = $packageRepository;
48
    }
49
50
    /**
51
     * Synchronizes file system packages to the database.
52
     */
53
    public function actionSyncToDb()
54
    {
55
        $packages = $this->packageStorage->listPackages();
56
57
        foreach ($packages as $name => $data) {
58
            $message = "Package %N$name%n ";
59
            $package = AssetPackage::fromFullName($name);
60
            $package->load();
61
62
            $message .= $this->packageRepository->exists($package)
63
                ? 'already exists. %BUpdated.%n'
64
                : 'does not exist. %GCreated.%n';
65
66
            $this->packageRepository->save($package);
67
68
            $this->stdout(Console::renderColoredString($message . "\n"));
69
        }
70
    }
71
72
    /**
73
     * Updates expired packages.
74
     */
75
    public function actionUpdateExpired()
76
    {
77
        $packages = $this->packageRepository->getExpiredForUpdate();
78
79
        foreach ($packages as $package) {
80
            $package->load();
81
            Yii::$app->queue->push(Yii::createObject(PackageUpdateCommand::class, [$package]));
82
83
            $message = 'Package %N' . $package->getFullName() . '%n';
84
            $message .= ' was updated ' . Yii::$app->formatter->asRelativeTime($package->getUpdateTime());
85
            $message .= ". %GAdded to queue for update%n\n";
86
            $this->stdout(Console::renderColoredString($message));
87
        }
88
    }
89
90
    public function actionCheckHashes()
91
    {
92
        $packages = $this->packageStorage->listPackages();
93
94
        $i = 0;
95
        foreach ($packages as $name => $data) {
96
            if ($i++ % 10 === 0) { $this->stdout('.'); }
97
            if ($i % 1000 === 0) { $this->stdout(" [ $i ]\n"); }
98
99
            $package = AssetPackage::fromFullName($name);
100
            if ($this->packageRepository->isAvoided($package)
101
                || $this->packageStorage->checkIsSane($package)
102
            ) {
103
                continue;
104
            }
105
106
            Yii::$app->queue->push(Yii::createObject(PackageUpdateCommand::class, [$package]));
107
108
            $message = "\nPackage %N" . $package->getFullName() . '%n is corrupted. ';
109
            $message .= "%GAdded to queue for update%n\n";
110
            $this->stdout(Console::renderColoredString($message));
111
        }
112
113
        return 0;
114
    }
115
}
116