Completed
Push — master ( 2ec9f0...d428ee )
by Andrii
30:10 queued 22:08
created

AbstractPackageCommand::__sleep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 1
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
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\commands;
12
13
use hiqdev\assetpackagist\models\AssetPackage;
14
use hiqdev\assetpackagist\repositories\PackageRepository;
15
use Yii;
16
use yii\base\Component;
17
use zhuravljov\yii\queue\Job;
18
19
abstract class AbstractPackageCommand extends Component implements Job
20
{
21
    const EVENT_BEFORE_RUN = 'beforeRun';
22
    const EVENT_AFTER_RUN = 'afterRun';
23
24
    /**
25
     * @var string
26
     */
27
    protected $fullName;
28
29
    /**
30
     * @var AssetPackage
31
     */
32
    protected $package;
33
34
    /**
35
     * @var PackageRepository
36
     */
37
    protected $packageRepository;
38
39
    /**
40
     * Triggers event before run.
41
     */
42
    public function beforeRun()
43
    {
44
        $this->trigger(self::EVENT_BEFORE_RUN);
45
    }
46
47
    /**
48
     * Triggers event after run.
49
     */
50
    public function afterRun()
51
    {
52
        $this->trigger(self::EVENT_AFTER_RUN);
53
    }
54
55
    /**
56
     * CollectDependenciesCommand constructor.
57
     * @param AssetPackage $package
58
     * @param PackageRepository $packageRepository
59
     * @param array $config
60
     */
61
    public function __construct(AssetPackage $package, PackageRepository $packageRepository, $config = [])
62
    {
63
        parent::__construct($config);
64
65
        $this->fullName = $package->getFullName();
66
        $this->package = $package;
67
        $this->packageRepository = $packageRepository;
68
    }
69
70
    /**
71
     * Serialize only the name of package for more performance.
72
     *
73
     * @void
74
     */
75
    public function __sleep()
76
    {
77
        return ['fullName'];
78
    }
79
80
    /**
81
     * Reloads package on wake up to ensure it is up to date.
82
     *
83
     * @void
84
     */
85
    public function __wakeup()
86
    {
87
        if ($this->fullName) {
88
            $this->package = AssetPackage::fromFullName($this->fullName);
89
        }
90
        $this->package->load();
91
        $this->packageRepository = Yii::createObject(PackageRepository::class);
92
    }
93
94
    /**
95
     * @return AssetPackage
96
     */
97
    public function getPackage()
98
    {
99
        return $this->package;
100
    }
101
}
102