Completed
Push — master ( e9548e...c0dc7e )
by Kirill
02:34
created

GenerateAction::run()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 40
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 5.0054

Importance

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