Completed
Push — master ( 871cd8...ab1e03 )
by Kirill
03:44
created

GenerateAction::run()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 40
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 6.0537

Importance

Changes 6
Bugs 0 Features 1
Metric Value
c 6
b 0
f 1
dl 0
loc 40
ccs 31
cts 35
cp 0.8857
rs 8.439
cc 6
eloc 28
nc 8
nop 0
crap 6.0537
1
<?php
2
namespace pastuhov\ymlcatalog\actions;
3
4
use Yii;
5
use pastuhov\Command\Command;
6
use pastuhov\ymlcatalog\YmlCatalog;
7
use yii\base\Action;
8
use yii\console\Controller;
9
10
/**
11
 * Генерация YML.
12
 *
13
 * @package pastuhov\ymlcatalog\actions
14
 */
15
class GenerateAction extends Action
16
{
17
    /**
18
     * @var string
19
     */
20
    public $fileName = 'yml.xml';
21
22
    /**
23
     * @var bool
24
     */
25
    public $enableGzip = true;
26
27
    /**
28
     * Publish yml and .gz
29
     * 
30
     * @var bool
31
     */
32
    public $keepBoth = false;
33
34
    /**
35
     * @var string
36
     */
37
    public $publicPath;
38
39
    /**
40
     * @var string
41
     */
42
    public $runtimePath;
43
44
    /**
45
     * @var string
46
     */
47
    public $shopClass;
48
49
    /**
50
     * @var string
51
     */
52
    public $currencyClass;
53
54
    /**
55
     * @var string
56
     */
57
    public $localDeliveryCostClass;
58
59
    /**
60
     * @var string
61
     */
62
    public $categoryClass;
63
64
    /**
65
     * @var string[]
66
     */
67
    public $offerClasses;
68
69
    /**
70
     * @var string
71
     */
72
    public $customOfferClass;
73
74
    /**
75
     * @var string
76
     */
77
    public $deliveryOptionClass;
78
79
    /**
80
     * @var callable
81
     */
82
    public $onValidationError;
83
84
    /**
85
     * @var string
86
     */
87
    public $handleClass = 'pastuhov\FileStream\BaseFileStream';
88
89
    /**
90
     * @var string
91
     */
92
    public $gzipCommand = 'gzip {keep_src} {src}';
93
94
    /**
95
     * Генерация YML.
96
     */
97 1
    public function run()
98
    {
99 1
        Yii::beginProfile('yml generate');
100
101 1
        $fileName = \Yii::getAlias($this->runtimePath) . DIRECTORY_SEPARATOR . $this->fileName;
102 1
        $handle = new $this->handleClass($fileName);
103
104 1
        $generator = new YmlCatalog(
105 1
            $handle,
106 1
            $this->shopClass,
107 1
            $this->currencyClass,
108 1
            $this->categoryClass,
109 1
            $this->localDeliveryCostClass,
110 1
            $this->offerClasses,
111 1
            null,
112 1
            $this->onValidationError,
113 1
            $this->customOfferClass,
114 1
            $this->deliveryOptionClass
115 1
        );
116 1
        $generator->generate();
117
118 1
        if ($this->enableGzip === true) {
119 1
            Command::exec($this->gzipCommand, [
120 1
                'src' => $fileName,
121 1
                'keep_src' => $this->keepBoth ? '-k' : ''
122 1
            ]);
123 1
        }
124
125 1
        $publicPath = \Yii::getAlias($this->publicPath);
126 1
        if (!$this->enableGzip || $this->keepBoth) {
127
            rename($fileName, $publicPath . DIRECTORY_SEPARATOR . basename($fileName));
128
        }
129 1
        if ($this->enableGzip) {
130 1
            $fileName .= '.gz';
131 1
            rename($fileName, $publicPath . DIRECTORY_SEPARATOR . basename($fileName));
132 1
        }
133 1
        Yii::endProfile('yml generate');
134
135 1
        return Controller::EXIT_CODE_NORMAL;
136
    }
137
}
138