Completed
Pull Request — master (#39)
by
unknown
03:13
created

YmlCatalog::writeModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2
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 $offerClasses;
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' => [
94
                    $this->currencyClass,
95
                ],
96 9
                'categories' => [
97
                    $this->categoryClass,
98 9
                ],
99
                'delivery-options' => [
100 9
                    $this->deliveryOptionClass,
101 9
                ],
102 9
                'offers' => $this->offerClasses,
103 9
            ],
104 6
        ];
105
106 9
        $this->writeTags($tags);
107 9
108 9
        $this->write('</yml_catalog>');
109 9
    }
110 9
111 9
    /**
112 9
     * @param string $string
113 9
     * @throws \Exception
114 9
     */
115 9
    protected function write($string)
116 9
    {
117 9
        $this->handle->write($string);
118 4
    }
119 6
120 8
    /**
121
     * @param string $string tag name
122 6
     */
123 6
    protected function writeTag($string)
124
    {
125
        $this->write('<' . $string . '>' . PHP_EOL);
126
    }
127
128 9
    /**
129
     * @param BaseModel $model
130 9
     * @param $valuesModel
131
     * @throws Exception
132 9
     */
133 3
    protected function writeModel(BaseModel $model, $valuesModel)
134 2
    {
135
        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...
136 9
            $string = $model->getYml();
137
            $this->write($string);
138
        }
139
    }
140
141
    protected function writeTags($tags)
142
    {
143 9
        foreach ($tags as $tagName => $tagParams) {
144 6
            if (is_string($tagName)) {
145 9
                $this->writeTag($tagName);
146 9
                $this->writeTags($tagParams);
147
                $this->writeTag('/' . $tagName);
148
            } else if (!is_null($tagParams)) {
149
                $this->writeEachModel($tagParams);
150
            }
151 9
        }
152 6
    }
153 9
154 9
    /**
155
     * @param string|array $modelClass class name
156
     */
157
    protected function writeEachModel($modelClass)
158
    {
159
        $findParams = [];
160
        if (is_array($modelClass)) {
161 9
            if (array_key_exists('findParams', $modelClass)) {
162
                $findParams = $modelClass['findParams'];
163 9
                unset($modelClass['findParams']);
164 9
            }
165 9
166 9
            $modelClass = $modelClass['class'];
167
        }
168 9
169 6
        $interfaces = class_implements($modelClass);
170
        if (!isset($interfaces[ActiveRecordInterface::class])) {
171 9
            return $this->writeModel($this->getNewModel($modelClass), \Yii::createObject($modelClass));
172 9
        }
173 6
174 4
        /* @var \yii\db\ActiveQuery $query */
175 9
        $query = $modelClass::findYml($findParams);
176 6
177 4
        $newModel = $this->getNewModel($modelClass);
178
        foreach ($query->batch(100) as $models) {
179 9
            foreach ($models as $model) {
180 9
                $this->writeModel($newModel, $model);
181 9
            }
182 6
        }
183 3
    }
184
185 6
    /**
186 9
     * @param $modelClass
187
     * @return Category|Currency|SimpleOffer
188 9
     * @throws Exception
189
     */
190 9
    protected function getNewModel($modelClass)
191
    {
192
        $factory = new ModelsFactory([
193
            'modelClass' => $modelClass,
194
        ]);
195 9
196
        return $factory->create();
197 9
    }
198
}
199