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

GenerateAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 93.1%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
c 3
b 0
f 0
lcom 1
cbo 4
dl 0
loc 105
ccs 27
cts 29
cp 0.931
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 37 5
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