Completed
Pull Request — master (#40)
by
unknown
04:16
created

YmlCatalog::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
ccs 16
cts 16
cp 1
rs 9.0856
cc 1
eloc 14
nc 1
nop 0
crap 1
1
<?php
2
namespace pastuhov\ymlcatalog;
3
4
use pastuhov\ymlcatalog\models\BaseModel;
5
use pastuhov\ymlcatalog\models\Category;
6
use pastuhov\ymlcatalog\models\Currency;
7
use pastuhov\ymlcatalog\models\CustomOffer;
8
use pastuhov\ymlcatalog\models\Shop;
9
use pastuhov\ymlcatalog\models\SimpleOffer;
10
use pastuhov\ymlcatalog\models\DeliveryOption;
11
use Yii;
12
use pastuhov\FileStream\BaseFileStream;
13
use yii\base\Exception;
14
use yii\base\Object;
15
use yii\db\ActiveRecordInterface;
16
17
/**
18
 * Yml генератор каталога.
19
 *
20
 * @package pastuhov\ymlcatalog
21
 */
22
class YmlCatalog extends Object
23
{
24
    /**
25
     * @var BaseFileStream
26
     */
27
    public $handle;
28
    /**
29
     * @var string
30
     */
31
    public $shopClass;
32
    /**
33
     * @var string
34
     */
35
    public $currencyClass;
36
    /**
37
     * @var string
38
     */
39
    public $categoryClass;
40
    /**
41
     * @var string
42
     */
43
    public $offerClass;
44
    /**
45
     * @var null|string
46
     */
47
    public $date;
48
49
    /**
50
     * @var null|callable
51
     */
52
    public $onValidationError;
53
54
    /**
55
     * @var null|string
56
     */
57
    public $customOfferClass;
58
59
    /**
60
     * @var null|string
61
     */
62
    public $deliveryOptionClass;
63
64
    /**
65
     * @return null|string
66
     */
67
    protected function getFormattedDate()
68
    {
69
        $date = $this->date;
70
71 9
        if ($date === null) {
72
            $date = Yii::$app->formatter->asDatetime(new \DateTime(), 'php:Y-m-d H:i');
73
        }
74 2
75
        return $date;
76
    }
77
78
    /**
79
     * @throws Exception
80
     */
81
    public function generate()
82 9
    {
83 9
        $date = $this->getFormattedDate();
84 9
85 9
        $this->write(
86 9
            '<?xml version="1.0" encoding="utf-8"?>' . PHP_EOL .
87 9
            '<yml_catalog date="' . $date . '">' . PHP_EOL
88 9
        );
89 9
90 9
        $tags = [
91 9
            'shop' => [
92
                $this->shopClass,
93
                'currencies' => $this->currencyClass,
94
                'categories' => $this->categoryClass,
95
                'delivery-options' => $this->deliveryOptionClass,
96 9
                'offers' => $this->offerClass,
97
            ],
98 9
        ];
99
100 9
        $this->writeTags($tags);
101 9
102 9
        $this->write('</yml_catalog>');
103 9
    }
104 6
105
    /**
106 9
     * @param string $string
107 9
     * @throws \Exception
108 9
     */
109 9
    protected function write($string)
110 9
    {
111 9
        $this->handle->write($string);
112 9
    }
113 9
114 9
    /**
115 9
     * @param string $string tag name
116 9
     */
117 9
    protected function writeTag($string)
118 4
    {
119 6
        $this->write('<' . $string . '>' . PHP_EOL);
120 8
    }
121
122 6
    /**
123 6
     * @param BaseModel $model
124
     * @param $valuesModel
125
     * @throws Exception
126
     */
127
    protected function writeModel(BaseModel $model, $valuesModel)
128 9
    {
129
        if($model->loadModel($valuesModel, $this->onValidationError)) {
0 ignored issues
show
Bug introduced by
It seems like $this->onValidationError can also be of type callable; however, pastuhov\ymlcatalog\models\BaseModel::loadModel() does only seem to accept null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
130 9
            $string = $model->getYml();
131
            $this->write($string);
132 9
        }
133 3
    }
134 2
135
    protected function writeTags($tags)
136 9
    {
137
        foreach ($tags as $tagName => $tagParams) {
138
            if (is_string($tagName)) {
139
                $this->writeTag($tagName);
140
                if (is_string($tagParams) || isset($tagParams['class'])) {
141
                    $this->writeEachModel($tagParams);
142
                } else {
143 9
                    $this->writeTags($tagParams);
144 6
                }
145 9
146 9
                $this->writeTag('/' . $tagName);
147
            } else if (!is_null($tagParams)) {
148
                $this->writeEachModel($tagParams);
149
            }
150
        }
151 9
    }
152 6
153 9
    /**
154 9
     * @param string|array $modelClass class name
155
     */
156
    protected function writeEachModel($modelClass)
157
    {
158
        $findParams = [];
159
        if (is_array($modelClass)) {
160
            if (array_key_exists('findParams', $modelClass)) {
161 9
                $findParams = $modelClass['findParams'];
162
                unset($modelClass['findParams']);
163 9
            }
164 9
165 9
            $modelClass = $modelClass['class'];
166 9
        }
167
168 9
        $interfaces = class_implements($modelClass);
169 6
        if (!isset($interfaces[ActiveRecordInterface::class])) {
170
            return $this->writeModel($this->getNewModel($modelClass), \Yii::createObject($modelClass));
171 9
        }
172 9
173 6
        /* @var \yii\db\ActiveQuery $query */
174 4
        $query = $modelClass::findYml($findParams);
175 9
176 6
        $newModel = $this->getNewModel($modelClass);
177 4
        foreach ($query->batch(100) as $models) {
178
            foreach ($models as $model) {
179 9
                $this->writeModel($newModel, $model);
180 9
            }
181 9
        }
182 6
    }
183 3
184
    /**
185 6
     * @param $modelClass
186 9
     * @return Category|Currency|SimpleOffer
187
     * @throws Exception
188 9
     */
189
    protected function getNewModel($modelClass)
190 9
    {
191
        $factory = new ModelsFactory([
192
            'modelClass' => $modelClass,
193
        ]);
194
195 9
        return $factory->create();
196
    }
197
}
198