Passed
Push — 1.2 ( 392f29...968e7d )
by Quentin
08:39 queued 03:08
created

HandleBrowsers::getFormFieldsHandleBrowsers()   B

Complexity

Conditions 10
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 12
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 18
rs 7.6666

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
namespace A17\Twill\Repositories\Behaviors;
4
5
trait HandleBrowsers
6
{
7
    /**
8
     * @param \A17\Twill\Models\Model $object
9
     * @param array $fields
10
     * @return void
11
     */
12
    public function afterSaveHandleBrowsers($object, $fields)
13
    {
14
        if (property_exists($this, 'browsers')) {
15
            foreach ($this->browsers as $module) {
16
                if (is_string($module)) {
17
                    $this->updateBrowser($object, $fields, $module, 'position', $module);
18
                } elseif (is_array($module)) {
19
                    $browserName = !empty($module['browserName']) ? $module['browserName'] : key($module);
20
                    $relation = !empty($module['relation']) ? $module['relation'] : key($module);
21
                    $positionAttribute = !empty($module['positionAttribute']) ? $module['positionAttribute'] : 'position';
22
                    $this->updateBrowser($object, $fields, $relation, $positionAttribute, $browserName);
23
                }
24
            }
25
        }
26
    }
27
28
    /**
29
     * @param \A17\Twill\Models\Model $object
30
     * @param array $fields
31
     * @return array
32
     */
33
    public function getFormFieldsHandleBrowsers($object, $fields)
34
    {
35
        if (property_exists($this, 'browsers')) {
36
            foreach ($this->browsers as $module) {
37
                if (is_string($module)) {
38
                    $fields['browsers'][$module] = $this->getFormFieldsForBrowser($object, $module, null, 'title', null);
39
                } elseif (is_array($module)) {
40
                    $relation = !empty($module['relation']) ? $module['relation'] : key($module);
41
                    $routePrefix = isset($module['routePrefix']) ? $module['routePrefix'] : null;
42
                    $titleKey = !empty($module['titleKey']) ? $module['titleKey'] : 'title';
43
                    $moduleName = isset($module['moduleName']) ? $module['moduleName'] : null;
44
                    $browserName = !empty($module['browserName']) ? $module['browserName'] : key($module);
45
                    $fields['browsers'][$browserName] = $this->getFormFieldsForBrowser($object, $relation, $routePrefix, $titleKey, $moduleName);
46
                }
47
            }
48
        }
49
        
50
        return $fields;
51
    } 
52
53
    /**
54
     * @param \A17\Twill\Models\Model $object
55
     * @param array $fields
56
     * @param string $relationship
57
     * @param string $positionAttribute
58
     * @return void
59
     */
60
    public function updateBrowser($object, $fields, $relationship, $positionAttribute = 'position', $browserName = null)
61
    {
62
        $browserName = $browserName ?? $relationship;
63
        $fieldsHasElements = isset($fields['browsers'][$browserName]) && !empty($fields['browsers'][$browserName]);
64
        $relatedElements = $fieldsHasElements ? $fields['browsers'][$browserName] : [];
65
        $relatedElementsWithPosition = [];
66
        $position = 1;
67
        foreach ($relatedElements as $relatedElement) {
68
            $relatedElementsWithPosition[$relatedElement['id']] = [$positionAttribute => $position++];
69
        }
70
71
        $object->$relationship()->sync($relatedElementsWithPosition);
72
    }
73
    
74
    /**
75
     * @param \A17\Twill\Models\Model $object
76
     * @param array $fields
77
     * @param string $relationship
78
     * @param string $positionAttribute
79
     * @return void
80
     */
81
    public function updateOrderedBelongsTomany($object, $fields, $relationship, $positionAttribute = 'position') {
82
        $this->updateBrowser($object, $fields, $relationship, $positionAttribute);
83
    }
84
    
85
    /**
86
     * @param mixed $object
87
     * @param array $fields
88
     * @param string $browserName
89
     * @return void
90
     */
91
    public function updateRelatedBrowser($object, $fields, $browserName)
92
    {
93
        $object->saveRelated($fields['browsers'][$browserName] ?? [], $browserName);
94
    }
95
96
    /**
97
     * @param \A17\Twill\Models\Model $object
98
     * @param string $relation
99
     * @param string|null $routePrefix
100
     * @param string $titleKey
101
     * @param string|null $moduleName
102
     * @return array
103
     */
104
    public function getFormFieldsForBrowser($object, $relation, $routePrefix = null, $titleKey = 'title', $moduleName = null)
105
    {
106
        return $object->$relation->map(function ($relatedElement) use ($titleKey, $routePrefix, $relation, $moduleName) {
107
            return [
108
                'id' => $relatedElement->id,
109
                'name' => $relatedElement->titleInBrowser ?? $relatedElement->$titleKey,
110
                'edit' => moduleRoute($moduleName ?? $relation, $routePrefix ?? '', 'edit', $relatedElement->id),
111
                'endpointType' => $relatedElement->getMorphClass(),
112
            ] + (classHasTrait($relatedElement, HasMedias::class) ? [
0 ignored issues
show
Bug introduced by
The type A17\Twill\Repositories\Behaviors\HasMedias 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...
113
                'thumbnail' => $relatedElement->defaultCmsImage(['w' => 100, 'h' => 100]),
114
            ] : []);
115
        })->toArray();
116
    }
117
118
    /**
119
     * @param \A17\Twill\Models\Model $object
120
     * @param string $relation
121
     * @return array
122
     */
123
    public function getFormFieldsForRelatedBrowser($object, $relation)
124
    {
125
        return $object->getRelated($relation)->map(function ($relatedElement) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return $object->getRelat...})->values()->toArray() also could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the documented return type array.
Loading history...
126
            return ($relatedElement != null) ? [
127
                'id' => $relatedElement->id,
128
                'name' => $relatedElement->titleInBrowser ?? $relatedElement->title,
129
                'endpointType' => $relatedElement->getMorphClass(),
130
            ] + (($relatedElement->adminEditUrl ?? null) ? [] : [
131
                'edit' => $relatedElement->adminEditUrl,
132
            ]) + (classHasTrait($relatedElement, HasMedias::class) ? [
133
                'thumbnail' => $relatedElement->defaultCmsImage(['w' => 100, 'h' => 100]),
134
            ] : []) : [];
135
        })->reject(function ($item) {
136
            return empty($item);
137
        })->values()->toArray();
138
    }
139
}
140