Passed
Push — master ( 073341...6ef52f )
by Alexey
05:07
created

Walker::startObjectParse()   D

Complexity

Conditions 10
Paths 98

Size

Total Lines 37
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 10
eloc 27
nc 98
nop 2
dl 0
loc 37
rs 4.8196
c 1
b 1
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        $walked = [];
27
        //walk know pathes
28
        foreach ($this->map->paths(['where' => ['path', $this->curPath]]) as $path) {
29
            if (isset($this->data[$path->item])) {
30
                if ($path->type == 'container') {
31
//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') {
42
//start parse path data
43
                    $this->startObjectParse($path->object_id, $this->data[$path->item]);
44
                }
45
            }
46
            $walked[$path->item] = true;
47
        }
48
        //check unparsed paths
49
        foreach ($this->data as $key => &$data) {
50
            //skip parsed and attribtes
51
            if ($key == '@attributes' || !empty($walked[$key])) {
52
                continue;
53
            }
54
            //search object for parse
55
            $object = Migration\Object::get([
56
                        ['code', $key],
57
                        ['migration_id', $this->migration->id]
58
            ]);
59
            if ($object) {
60
//parse as object
61
                $this->startObjectParse($object, $data);
62
            } else {
63
//create new map path for configure unknown path
64
                $this->mapPath = new Migration\Map\Path();
65
                $this->mapPath->parent_id = $this->mapPathParent ? $this->mapPathParent->id : 0;
66
                $this->mapPath->path = $this->curPath;
67
                $this->mapPath->item = $key;
68
                $this->mapPath->migration_map_id = $this->map->id;
69
                $this->mapPath->save();
70
            }
71
        }
72
    }
73
74
    private function startObjectParse($object_id, &$data) {
75
        $objectParser = new Parser\Object();
76
        $objectParser->object = is_object($object_id) ? $object_id : \App::$cur->migrations->getMigrationObject($object_id);
77
        $objectParser->data = $data;
78
        $objectParser->walker = $this;
79
        $ids = $objectParser->parse();
80
81
        if ($objectParser->object->clear && json_decode($objectParser->object->clear, true)) {
82
83
            $where = json_decode($objectParser->object->clear, true);
84
            if (!$where) {
85
                $where = [];
86
            } else {
87
                $where = [[$where]];
88
            }
89
            if ($ids) {
90
                $where[] = ['id', implode(',', $ids), 'NOT IN'];
91
            }
92
            if (empty(\App::$cur->migrations->ids['objectIds'])) {
93
                \App::$cur->migrations->loadObjectIds($objectParser->object->model);
94
            }
95
            if (!empty(\App::$cur->migrations->ids['objectIds'][$objectParser->object->model])) {
96
                $where[] = ['id', implode(',', array_keys(\App::$cur->migrations->ids['objectIds'][$objectParser->object->model])), 'IN'];
97
            }
98
            $modelName = $objectParser->object->model;
99
            $objects = $modelName::getList(['where' => $where]);
100
            foreach ($objects as $object) {
101
                $objectId = \App::$cur->migrations->findParse($object->id, $objectParser->object->model);
102
                if ($objectId) {
103
                    $objectId->delete();
104
                    unset(\App::$cur->migrations->ids['objectIds'][$objectParser->object->model][$object->id]);
105
                    unset(\App::$cur->migrations->ids['parseIds'][$objectParser->object->model][$objectId->parse_id]);
106
                }
107
                $object->delete();
108
            }
109
        }
110
    }
111
112
}
113