Completed
Pull Request — master (#6)
by
unknown
23:06
created

GenerateAction::run()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 37
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 8.4587

Importance

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