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

Relation::parse()   C

Complexity

Conditions 15
Paths 48

Size

Total Lines 79
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 15
eloc 60
c 2
b 0
f 0
nc 48
nop 0
dl 0
loc 79
rs 5.098

How to fix   Long Method    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
 * Parser Object Relation
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\Parser\Object;
13
14
class Relation extends \Migrations\Parser
15
{
16
    public function parse()
17
    {
18
        $options = $this->param->options ? json_decode($this->param->options, true) : [];
19
        $modelName = $this->object->object->model;
20
        $relation = $modelName::getRelation($this->param->value);
21
        $object = \Migrations\Migration\Object::get([
22
                    ['model', $relation['model']],
23
                    ['migration_id', $this->object->object->migration_id]
24
        ]);
25
        if (!$object) {
26
            $object = new \Migrations\Migration\Object([
27
                'model' => $relation['model'],
28
                'code' => $this->param->code,
29
                'name' => $this->param->code,
30
                'migration_id' => $this->object->object->migration_id
31
            ]);
32
            $object->save();
33
        }
34
        if (!empty($relation['type']) && $relation['type'] == 'many') {
35
            $ids = [];
36
            if ($this->data) {
37
                foreach ($this->data as $code => &$item) {
38
                    if (!\Tools::isAssoc($this->data)) {
39
                        foreach ($this->data as &$item) {
40
                            $objectParser = new \Migrations\Parser\Object();
41
                            $objectParser->object = $object;
42
                            $objectParser->parentObject = $this->object;
43
                            $objectParser->parentModel = $this->model;
44
                            $objectParser->walker = $this->object->walker;
45
                            $objectParser->parentParam = $this;
46
                            $objectParser->data = &$item;
47
                            
48
49
                            if (!$this->model->pk()) {
50
                                $this->model->save();
51
                            }
52
                            $ids = array_merge($ids, $objectParser->parse([$relation['col'] => $this->model->pk()]));
53
                        }
54
                    } else {
55
                        $objectParser = new \Migrations\Parser\Object();
56
                        $objectParser->object = $object;
57
                        $objectParser->parentObject = $this->object;
58
                        $objectParser->parentModel = $this->model;
59
                        $objectParser->walker = $this->object->walker;
60
                        $objectParser->parentParam = $this;
61
                        $objectParser->data = &$item;
62
                        if (!$this->model->pk()) {
63
                            $this->model->save();
64
                        }
65
                        $ids = array_merge($ids, $objectParser->parse([$relation['col'] => $this->model->pk()]));
66
                    }
67
                }
68
            }
69
            if (!empty($options['clearMissing'])) {
70
                $where = [];
71
                $where[] = [$relation['col'], $this->model->pk()];
72
                if ($ids) {
73
                    $where[] = ['id', implode(',', $ids), 'NOT IN'];
74
                }
75
                $modelName = $relation['model'];
76
                $objects = $modelName::getList(['where' => $where]);
77
78
                foreach ($objects as $object) {
79
                    $object->delete();
80
                }
81
            }
82
        } else {
83
            $objectParser = new \Migrations\Parser\Object();
84
            $objectParser->object = $object;
85
            $objectParser->parentObject = $this->object;
86
            $objectParser->parentModel = $this->model;
87
            $objectParser->parentParam = $this;
88
            $objectParser->data = &$this->data;
89
            $id = $objectParser->parse();
90
            if ($id) {
91
                $this->model->{$relation['col']} = $id[0];
92
            }
93
        }
94
    }
95
96
}
97