GenerateAction::run()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 41
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 6.0326

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 41
ccs 28
cts 31
cp 0.9032
rs 8.439
cc 6
eloc 29
nc 8
nop 0
crap 6.0326
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 $customCategoryClass;
78
79
    /**
80
     * @var string
81
     */
82
    public $deliveryOptionClass;
83
84
    /**
85
     * @var callable
86
     */
87
    public $onValidationError;
88
89
    /**
90
     * @var string
91
     */
92
    public $handleClass = 'pastuhov\FileStream\BaseFileStream';
93
94
    /**
95
     * @var string
96
     */
97 1
    public $gzipCommand = 'gzip {keep_src} {src}';
98
99 1
    /**
100
     * Генерация YML.
101 1
     */
102 1
    public function run()
103
    {
104 1
        Yii::beginProfile('yml generate');
105 1
106 1
        $fileName = \Yii::getAlias($this->runtimePath) . DIRECTORY_SEPARATOR . $this->fileName;
107 1
        $handle = new $this->handleClass($fileName);
108 1
109 1
        $generator = new YmlCatalog(
110 1
            $handle,
111 1
            $this->shopClass,
112 1
            $this->currencyClass,
113 1
            $this->categoryClass,
114 1
            $this->localDeliveryCostClass,
115 1
            $this->offerClasses,
116 1
            null,
117
            $this->onValidationError,
118 1
            $this->customOfferClass,
119 1
            $this->deliveryOptionClass,
120 1
            $this->customCategoryClass
121 1
        );
122 1
        $generator->generate();
123 1
124
        if ($this->enableGzip === true) {
125 1
            Command::exec($this->gzipCommand, [
126 1
                'src' => $fileName,
127
                'keep_src' => $this->keepBoth ? '-k' : ''
128
            ]);
129 1
        }
130 1
131 1
        $publicPath = \Yii::getAlias($this->publicPath);
132 1
        if (!$this->enableGzip || $this->keepBoth) {
133 1
            rename($fileName, $publicPath . DIRECTORY_SEPARATOR . basename($fileName));
134
        }
135 1
        if ($this->enableGzip) {
136
            $fileName .= '.gz';
137
            rename($fileName, $publicPath . DIRECTORY_SEPARATOR . basename($fileName));
138
        }
139
        Yii::endProfile('yml generate');
140
141
        return Controller::EXIT_CODE_NORMAL;
142
    }
143
}
144