Completed
Push — master ( a94186...6eb44b )
by Kirill
05:15
created

GenerateAction::run()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 37
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 5.0082

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 37
ccs 27
cts 29
cp 0.931
rs 8.439
cc 5
eloc 25
nc 6
nop 0
crap 5.0082
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 3
        Yii::beginProfile('yml generate');
85
86 3
        $fileName = \Yii::getAlias($this->runtimePath) . '/' . $this->fileName;
87 3
        $gzipedFileName = $fileName . '.gz';
88 3
        $handle = new $this->handleClass($fileName);
89
90 3
        $generator = new YmlCatalog(
91 3
            $handle,
92 3
            $this->shopClass,
93 3
            $this->currencyClass,
94 3
            $this->categoryClass,
95 3
            $this->localDeliveryCostClass,
96 3
            $this->offerClasses
97 3
        );
98 3
        $generator->generate();
99
100 3
        if ($this->enableGzip === true) {
101 3
            Command::exec($this->gzipCommand, [
102 3
                'src' => $fileName,
103
                'dst' => $gzipedFileName
104 3
            ]);
105 3
            if (!$this->keepBoth) {
106 3
                $fileName = $gzipedFileName;
107 3
            }
108 3
        }
109
110 3
        $publicPath = \Yii::getAlias($this->publicPath);
111 3
        rename($fileName, $publicPath . '/' . basename($fileName));
112 3
        if ($this->enableGzip && $this->keepBoth) {
113
            rename($gzipedFileName, $publicPath . '/' . basename($gzipedFileName));
114
        }
115 3
        Yii::endProfile('yml generate');
116
117 3
        return Controller::EXIT_CODE_NORMAL;
118
    }
119
}
120