Completed
Push — master ( b2aa64...db9a1b )
by Alexey
05:01
created

Walker::startObjectParse()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.439
cc 6
eloc 21
nc 13
nop 2
1
<?php
2
3
/**
4
 * Data tree walker
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 Migrations;
13
14
class Walker
15
{
16
    public $migration = null;
17
    public $map = null;
18
    public $data = null;
19
    public $mapPath = null;
20
    public $mapPathParent = null;
21
    public $curPath = '/';
22
    public $migtarionLog = null;
23
24
    //walk map pathes on cur path
25
    public function walk()
26
    {
27
        $walked = [];
28
        //walk know pathes
29
        foreach ($this->map->paths(['where' => ['path', $this->curPath]]) as $path) {
30
            if (isset($this->data[$path->item])) {
31
                if ($path->type == 'container') { //create walker for container
32
                    $walker = new Walker();
33
                    $walker->migration = $this->migration;
34
                    $walker->map = $this->map;
35
                    $walker->data = &$this->data[$path->item];
36
                    $walker->curPath = $this->curPath . $path->item . '/';
37
                    $walker->mapPath = $path;
38
                    $walker->mapPathParent = $this->mapPath;
39
                    $walker->migtarionLog = $this->migtarionLog;
40
                    $walker->walk();
41
                } elseif ($path->type == 'object') { //start parse path data
42
                    $this->startObjectParse($path->object_id, $this->data[$path->item]);
43
                }
44
            }
45
            $walked[$path->item] = true;
46
        }
47
        //check unparsed paths
48
        foreach ($this->data as $key => &$data) {
49
            //skip parsed and attribtes
50
            if ($key == '@attributes' || !empty($walked[$key])) {
51
                continue;
52
            }
53
            //search object for parse
54
            $object = Migration\Object::get([
55
                        ['code', $key],
56
                        ['migration_id', $this->migration->id]
57
            ]);
58
            if ($object) { //parse as object
59
                $this->startObjectParse($object->id, $data);
60
            } else { //create new map path for configure unknown path
61
                $this->mapPath = new Migration\Map\Path();
62
                $this->mapPath->parent_id = $this->mapPathParent ? $this->mapPathParent->id : 0;
63
                $this->mapPath->path = $this->curPath;
64
                $this->mapPath->item = $key;
65
                $this->mapPath->migration_map_id = $this->map->id;
66
                $this->mapPath->save();
67
            }
68
        }
69
    }
70
71
    private function startObjectParse($object_id, &$data)
72
    {
73
        $objectParser = new Parser\Object();
74
        $objectParser->object = Migration\Object::get($object_id);
75
        $objectParser->data = $data;
76
        $objectParser->walker = $this;
77
        $ids = $objectParser->parse();
78
79
        if ($objectParser->object->clear) {
80
81
            $where = json_decode($objectParser->object->clear, true);
82
            if (!$where) {
83
                $where = [];
84
            } else {
85
                $where = [[$where]];
86
            }
87
            if ($ids) {
88
                $where[] = ['id', implode(',', $ids), 'NOT IN'];
89
            }
90
            $modelName = $objectParser->object->model;
91
            $objects = $modelName::getList(['where' => $where]);
92
            foreach ($objects as $object) {
93
                $objectId = \Migrations\Id::get([['object_id', $object->id], ['type', $objectParser->object->model]]);
94
                if ($objectId) {
95
                    $objectId->delete();
96
                }
97
                $object->delete();
98
            }
99
        }
100
    }
101
102
}
103