Completed
Pull Request — master (#8)
by
unknown
03:17
created

GenerateAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 93.75%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 5
c 4
b 0
f 0
lcom 1
cbo 4
dl 0
loc 112
ccs 30
cts 32
cp 0.9375
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 39 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 callable
71
     */
72
    public $onValidationError;
73
74
    /**
75
     * @var string
76
     */
77
    public $handleClass = 'pastuhov\FileStream\BaseFileStream';
78
79
    /**
80
     * @var string
81
     */
82
    public $gzipCommand = 'cat {src} | gzip > {dst}';
83
84
    /**
85
     * Генерация YML.
86
     */
87 3
    public function run()
88
    {
89 3
        Yii::beginProfile('yml generate');
90
91 3
        $fileName = \Yii::getAlias($this->runtimePath) . '/' . $this->fileName;
92 3
        $gzipedFileName = $fileName . '.gz';
93 3
        $handle = new $this->handleClass($fileName);
94
95 3
        $generator = new YmlCatalog(
96 2
            $handle,
97 3
            $this->shopClass,
98 3
            $this->currencyClass,
99 3
            $this->categoryClass,
100 3
            $this->localDeliveryCostClass,
101 3
            $this->offerClasses,
102 3
            null,
103 3
            $this->onValidationError
104 2
        );
105 3
        $generator->generate();
106
107 3
        if ($this->enableGzip === true) {
108 3
            Command::exec($this->gzipCommand, [
109 3
                'src' => $fileName,
110 1
                'dst' => $gzipedFileName
111 2
            ]);
112 3
            if (!$this->keepBoth) {
113 3
                $fileName = $gzipedFileName;
114 2
            }
115 2
        }
116
117 3
        $publicPath = \Yii::getAlias($this->publicPath);
118 3
        rename($fileName, $publicPath . '/' . basename($fileName));
119 3
        if ($this->enableGzip && $this->keepBoth) {
120
            rename($gzipedFileName, $publicPath . '/' . basename($gzipedFileName));
121
        }
122 3
        Yii::endProfile('yml generate');
123
124 3
        return Controller::EXIT_CODE_NORMAL;
125
    }
126
}
127