Completed
Push — master ( 9930d3...e136e6 )
by Alexey
05:19
created

Warehouse::parse()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
cc 7
eloc 18
c 3
b 2
f 0
nc 6
nop 0
dl 0
loc 26
rs 6.7272
1
<?php
2
3
/**
4
 * Parser Item Offer Warehouse
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Exchange1c\Parser\Item\Offer;
13
14
class Warehouse extends \Migrations\Parser
15
{
16
    public function parse()
17
    {
18
        if (is_array($this->data) && empty($this->data['@attributes'])) {
19
            foreach ($this->data as $warehouseCount) {
20
                $count = $warehouseCount['@attributes']['КоличествоНаСкладе'];
21
                $objectId = \App::$cur->migrations->findObject((string) $warehouseCount['@attributes']['ИдСклада'], 'Ecommerce\Warehouse');
22
                if ($objectId) {
23
                    $modelName = get_class($this->model);
24
                    $warehouse = \Ecommerce\Item\Offer\Warehouse::get([[$modelName::index(), $this->model->pk()], [\Ecommerce\Warehouse::index(), $objectId->object_id]]);
25
                    if (!$warehouse) {
26
                        $warehouse = new \Ecommerce\Item\Offer\Warehouse([
27
                            $modelName::index() => $this->model->pk(),
28
                            \Ecommerce\Warehouse::index() => $objectId->object_id,
29
                            'count' => $count
30
                        ]);
31
                        $warehouse->save();
32
                    } else {
33
                        if ($warehouse->count != $count) {
34
                            $warehouse->count = $count;
35
                            $warehouse->save();
36
                        }
37
                    }
38
                }
39
            }
40
        }
41
    }
42
43
}
44