Value   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 23

1 Method

Rating   Name   Duplication   Size   Complexity  
F parse() 0 78 23
1
<?php
2
3
/**
4
 * Parser Object Value
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 Value extends \Migrations\Parser {
15
16
    public function parse() {
17
        $options = $this->param->options ? json_decode($this->param->options, true) : [];
18
        $modelName = get_class($this->model);
19
        $cols = $modelName::$cols;
0 ignored issues
show
Bug introduced by
The property cols does not exist on string.
Loading history...
20
        $value = $this->data;
21
        if (is_array($value)) {
22
            $value = '';
23
        }
24
        if (!empty($cols[$this->param->value])) {
25
            $col = $cols[$this->param->value];
26
            if ($col['type'] == 'dynamicType') {
27
                switch ($col['typeSource']) {
28
                    case 'selfMethod':
29
                        $type = $this->model->{$col['selfMethod']}();
30
                        if (is_array($type)) {
31
                            if (strpos($type['relation'], ':')) {
32
                                $relationPath = explode(':', $type['relation']);
33
                                $relationName = array_pop($relationPath);
34
                                $item = $this->model;
35
                                foreach ($relationPath as $path) {
36
                                    $item = $item->$path;
37
                                }
38
                                $itemModel = get_class($item);
39
                                $relation = $itemModel::getRelation($relationName);
40
                                $sourceModel = $relation['model'];
41
                            } else {
42
                                $relation = $modelName::getRelation($type['relation']);
43
                                $sourceModel = $relation['model'];
44
                            }
45
                            $objectId = \App::$cur->migrations->findObject((string) $value, $sourceModel);
46
                            if ($objectId) {
47
                                $value = $objectId->object_id;
48
                            }
49
                        }
50
                        break;
51
                }
52
            } else {
53
                $type = $col['type'];
54
            }
55
        } else {
56
            $type = 'text';
57
        }
58
59
        if (!empty($options['valueReplace'])) {
60
            $values = $this->param->values(['key' => 'original']);
61
            if (!isset($values[$value])) {
62
                $valueObject = new \Migrations\Migration\Object\Param\Value();
63
                $valueObject->param_id = $this->param->id;
0 ignored issues
show
Bug Best Practice introduced by
The property param_id does not exist on Migrations\Migration\Object\Param\Value. Since you implemented __set, consider adding a @property annotation.
Loading history...
64
                $valueObject->original = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property original does not exist on Migrations\Migration\Object\Param\Value. Since you implemented __set, consider adding a @property annotation.
Loading history...
65
                $valueObject->save();
66
            } else {
67
                $valueObject = $values[$value];
68
            }
69
            $value = $valueObject->replace;
0 ignored issues
show
Bug Best Practice introduced by
The property replace does not exist on Migrations\Migration\Object\Param\Value. Since you implemented __get, consider adding a @property annotation.
Loading history...
70
        }
71
        switch ($type) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $type does not seem to be defined for all execution paths leading up to this point.
Loading history...
72
            case 'image':
73
                $notEq = true;
74
                $dir = pathinfo($this->object->walker->migtarionLog->source, PATHINFO_DIRNAME);
75
                if ($this->model->{$this->param->value}) {
76
                    $file = \Files\File::get($this->model->{$this->param->value});
0 ignored issues
show
Bug introduced by
The type Files\File 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...
77
                    if ($file && $value && file_exists($dir . '/' . $value) && file_exists(\App::$primary->path . $file->path) && md5_file($dir . '/' . $value) == md5_file(\App::$primary->path . $file->path)) {
78
                        $notEq = false;
79
                    }
80
                    if ($file && $notEq) {
81
                        $file->delete();
82
                        $this->model->{$this->param->value} = 0;
83
                    }
84
                }
85
                if ($notEq) {
86
                    $this->model->{$this->param->value} = \App::$primary->files->uploadFromUrl($dir . '/' . $value, ['accept_group' => 'image', 'upload_code' => 'MigrationUpload']);
87
                }
88
                break;
89
            default:
90
                if (is_array($value)) {
91
                    $value = implode(' ', $value);
92
                }
93
                $this->model->{$this->param->value} = $value;
94
        }
95
    }
96
97
}
98