Completed
Push — master ( bc20b9...9515fd )
by Dmitry
02:03
created

MaintenanceController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 48
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A actionSyncToDb() 0 18 3
1
<?php
2
3
namespace hiqdev\assetpackagist\console;
4
5
use hiqdev\assetpackagist\components\StorageInterface;
6
use hiqdev\assetpackagist\models\AssetPackage;
7
use hiqdev\assetpackagist\repositories\PackageRepository;
8
use yii\console\Controller;
9
use yii\helpers\Console;
10
11
/**
12
 * Provides maintenance actions for the asset-packagist service
13
 * @package hiqdev\assetpackagist\console
14
 */
15
class MaintenanceController extends Controller
16
{
17
    /**
18
     * @var StorageInterface
19
     */
20
    protected $packageStorage;
21
22
    /**
23
     * @var PackageRepository
24
     */
25
    protected $packageRepository;
26
27
    /**
28
     * MaintenanceController constructor.
29
     * @param StorageInterface $packageStorage
30
     * @param PackageRepository $packageRepository
31
     * @inheritdoc
32
     */
33
    public function __construct($id, $module, StorageInterface $packageStorage, PackageRepository $packageRepository, $config = [])
34
    {
35
        parent::__construct($id, $module, $config);
36
37
        $this->packageStorage = $packageStorage;
38
        $this->packageRepository = $packageRepository;
39
    }
40
41
    /**
42
     * Synchronizes file system packages to the database
43
     */
44
    public function actionSyncToDb()
45
    {
46
        $packages = $this->packageStorage->listPackages();
47
48
        foreach ($packages as $name => $data) {
49
            $message = "Package %N$name%n ";
50
            $package = AssetPackage::fromFullName($name);
51
            $package->load();
52
53
            $message .= $this->packageRepository->exists($package)
54
                ? 'already exists. %BUpdated.%n'
55
                : 'does not exist. %GCreated.%n';
56
57
            $this->packageRepository->save($package);
58
59
            $this->stdout(Console::renderColoredString($message . "\n"));
60
        }
61
    }
62
}
63