Completed
Pull Request — master (#39)
by
unknown
05:32
created

GenerateAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 93.94%

Importance

Changes 7
Bugs 0 Features 1
Metric Value
wmc 7
c 7
b 0
f 1
lcom 1
cbo 4
dl 0
loc 121
ccs 31
cts 33
cp 0.9394
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C run() 0 43 7
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 $categoryClass;
58
59
    /**
60
     * @var string[]
61
     */
62
    public $offerClass;
63
64
    /**
65
     * @var string
66
     */
67
    public $customOfferClass;
68
69
    /**
70
     * @var string
71
     */
72
    public $deliveryOptionClass;
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 = 'gzip -f {keep_src} {src}';
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) . DIRECTORY_SEPARATOR . $this->fileName;
97 3
        $handle = new $this->handleClass($fileName);
98 3
99
        $generatorAttributes = [
100 3
            'shopClass',
101 2
            'currencyClass',
102 3
            'categoryClass',
103 3
            'offerClass',
104 3
            'onValidationError',
105 3
            'deliveryOptionClass'
106 3
        ];
107 3
        $generatorParams = [
108 3
            'handle' => $handle,
109 3
        ];
110 2
        foreach ($generatorAttributes as $attribute) {
111 3
            $generatorParams[$attribute] = $this->$attribute;
112
        }
113 3
        $generator = new YmlCatalog($generatorParams);
114 3
        $generator->generate();
115 3
116 1
        if ($this->enableGzip === true) {
117 2
            Command::exec($this->gzipCommand, [
118 3
                'src' => $fileName,
119 3
                'keep_src' => $this->keepBoth ? '-k' : ''
120 2
            ]);
121 2
        }
122
123 3
        $publicPath = \Yii::getAlias($this->publicPath);
124 3
        if (!$this->enableGzip || $this->keepBoth) {
125 3
            rename($fileName, $publicPath . DIRECTORY_SEPARATOR . basename($fileName));
126
        }
127
        if ($this->enableGzip) {
128 3
            $fileName .= '.gz';
129
            rename($fileName, $publicPath . DIRECTORY_SEPARATOR . basename($fileName));
130 3
        }
131
        Yii::endProfile('yml generate');
132
133
        return Controller::EXIT_CODE_NORMAL;
134
    }
135
}
136