Passed
Push — v5 ( e348bf...8a8f01 )
by Alexey
06:35
created

Object   D

Complexity

Total Complexity 59

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 208
rs 4.5454
c 0
b 0
f 0
wmc 59

3 Methods

Rating   Name   Duplication   Size   Complexity  
B parse() 0 16 6
D setModel() 0 91 28
D parseData() 0 85 25

How to fix   Complexity   

Complex Class

Complex classes like Object often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Object, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * Pareser object
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;
13
14
class Object extends \InjiObject {
0 ignored issues
show
Bug introduced by
The type InjiObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
    public $object;
17
    public $parentObject;
18
    public $parentModel;
19
    public $parentParam;
20
    /**
21
     * @var \Migrations\Walker
22
     */
23
    public $walker;
24
    public $data;
25
26
    public function parse($preset = []) {
27
        $ids = [];
28
        if (is_array($this->data) && !\Tools::isAssoc($this->data)) {
0 ignored issues
show
Bug introduced by
The type Tools was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
            foreach ($this->data as &$data) {
30
                $id = $this->parseData($data, $preset);
31
                if ($id) {
32
                    $ids[] = $id;
33
                }
34
            }
35
        } else {
36
            $id = $this->parseData($this->data, $preset);
37
            if ($id) {
38
                $ids[] = $id;
39
            }
40
        }
41
        return $ids;
42
    }
43
44
    private function parseData($data, $preset) {
45
        $keyLog = \App::$cur->log->start('start object model set');
46
        $model = $this->setModel($data);
47
        \App::$cur->log->end($keyLog);
48
        if ($model) {
49
            foreach ($preset as $col => $value) {
50
                $model->{$col} = $value;
51
            }
52
            if (defined('mdebug')) {
53
                echo " -> objectStart ({$this->object->id}) ";
54
            }
55
            $walked = [];
56
            foreach ($this->object->params as $param) {
57
                if (defined('mdebug')) {
58
                    echo " -> param ($param->id,$param->type,$param->value) ";
59
                }
60
                if ($model && $param->type && $param->type != 'item_key') {
61
                    if ($param->type == 'object') {
62
                        $object = \App::$cur->migrations->getMigrationObject($this->walker->migration, $param->value);
63
                        $parser = new \Migrations\Parser\Object;
64
                        $parser->data = &$data[$param->code];
65
                        $parser->object = $object;
66
                        $parser->parentObject = $this;
67
                        $parser->parentModel = $model;
68
                        $parser->walker = $this->walker;
69
                        if (defined('mdebug')) {
70
                            echo " -> objectParse ";
71
                        }
72
                        $parser->parse();
73
                    } else {
74
                        if ($param->type == 'custom') {
75
                            $parserName = $param->value;
76
                        } else {
77
                            $parserName = '\Migrations\Parser\Object\\' . ucfirst($param->type);
78
                        }
79
                        $parser = new $parserName;
80
                        $parser->data = &$data[$param->code];
81
                        $parser->param = $param;
82
                        $parser->model = $model;
83
                        $parser->walker = $this->walker;
84
                        $parser->object = $this;
85
                        if (defined('mdebug')) {
86
                            echo " -> parser ($parserName) ";
87
                        }
88
                        $parser->parse();
89
                    }
90
                }
91
                if (defined('mdebug')) {
92
                    echo " -> paramEnd ($param->id,$param->type,$param->value) ";
93
                }
94
                $walked[$param->code] = true;
95
            }
96
97
            //check unparsed params
98
            foreach ($data as $key => $item) {
99
                //skip parsed and attribtes
100
                if ($key == '@attributes' || !empty($walked[$key])) {
101
                    continue;
102
                }
103
                $param = new \Migrations\Migration\Object\Param();
104
                $param->object_id = $this->object->id;
105
                $param->code = $key;
106
                $param->save();
107
                $className = get_class($this->object);
108
                $this->object = $className::get($this->object->id);
109
            }
110
            if ($model) {
111
                if ($this->object->delete_empty && @json_decode($this->object->delete_empty, true)) {
112
                    $deleteIf = json_decode($this->object->delete_empty, true);
113
                    foreach ($deleteIf['params'] as $paramId) {
114
                        if ($model->{$this->object->params[$paramId]->value} === '') {
115
                            if($model->pk()){
116
                                $model->delete();
117
                            }
118
                            return 0;
119
                        }
120
                    }
121
                }
122
                if (!$model->pk() || !empty($model->_changedParams)) {
123
                    $model->save();
124
                }
125
                return $model->pk();
126
            }
127
        }
128
        return 0;
129
    }
130
131
    public function setModel($data) {
132
        $model = null;
133
        $keyCol = null;
134
        $uniques = [];
135
        foreach ($this->object->params as $param) {
136
            $options = $param->options ? json_decode($param->options, true) : [];
137
            if ($param->type == 'item_key') {
138
                $keyCol = $param->code;
139
                break;
140
            } elseif (!empty($options['unique'])) {
141
                $uniques[$param->code] = $param;
142
            }
143
        }
144
        if ($keyCol && isset($data[$keyCol])) {
145
            $objectId = \App::$cur->migrations->findObject((string) $data[$keyCol], $this->object->model);
146
            if ($objectId) {
147
                $modelName = $this->object->model;
148
                $model = $modelName::get($objectId->object_id);
149
            } else {
150
                $model = new $this->object->model;
151
                $model->save(['empty' => true]);
152
                $objectId = new \Migrations\Id();
153
                $objectId->object_id = $model->id;
154
                $objectId->parse_id = (string) $data[$keyCol];
155
                $objectId->type = $this->object->model;
156
                $objectId->save();
157
                \App::$cur->migrations->ids['objectIds'][$this->object->model][$model->id] = $objectId;
158
                \App::$cur->migrations->ids['parseIds'][$this->object->model][(string) $data[$keyCol]] = $objectId;
159
            }
160
        } elseif ($uniques) {
161
            $where = [];
162
            foreach ($uniques as $code => $param) {
163
                if (!isset($data[$code])) {
164
                    return;
165
                }
166
                switch ($param->type) {
167
                    case 'objectLink':
168
                        $object = \App::$cur->migrations->getMigrationObject($this->walker->migration, $param->value);
169
                        $objectId = \App::$cur->migrations->findObject((string) $data[$code], $object->model);
170
                        if (!$objectId) {
171
                            return;
172
                        }
173
                        $modelName = $object->model;
174
                        $where[] = [$modelName::index(), $objectId->object_id];
175
                        break;
176
                    case 'relation':
177
                        $modelName = $this->object->model;
178
                        $relation = $modelName::getRelation($param->value);
179
                        $objectId = \App::$cur->migrations->findObject((string) $data[$code], $relation['model']);
180
                        if (!$objectId) {
181
                            return;
182
                        }
183
                        $where[] = [$relation['col'], $objectId->object_id];
184
                        break;
185
                }
186
            }
187
            if ($where) {
188
                if ($this->parentParam) {
189
                    $modelName = $this->parentObject->object->model;
190
                    $relation = $modelName::getRelation($this->parentParam->param->value);
191
                    if (!empty($relation['type']) && $relation['type'] == 'many' && count($where) == 1) {
192
                        $relationName = $this->parentParam->param->value;
193
                        if (!empty($this->parentModel->$relationName(['key' => $where[0][0]])[$where[0][1]])) {
194
                            return $this->parentModel->$relationName(['key' => $where[0][0]])[$where[0][1]];
195
                        } else {
196
                            $model = new $this->object->model;
197
                            foreach ($where as $item) {
198
                                $model->{$item[0]} = $item[1];
199
                            }
200
                            return $model;
201
                        }
202
                    } elseif (!empty($relation['type']) && $relation['type'] == 'many') {
203
                        $where[] = [$relation['col'], $this->parentModel->pk()];
204
                    }
205
                } elseif ($this->parentObject) {
206
                    $modelName = $this->parentObject->object->model;
207
                    $where[] = [$modelName::index(), $this->parentModel->pk()];
208
                }
209
            }
210
            if ($where) {
211
                $modelName = $this->object->model;
212
                $model = $modelName::get($where);
213
                if (!$model) {
214
                    $model = new $this->object->model;
215
                    foreach ($where as $item) {
216
                        $model->{$item[0]} = $item[1];
217
                    }
218
                }
219
            }
220
        }
221
        return $model;
222
    }
223
}