Translation::persist()   D
last analyzed

Complexity

Conditions 10
Paths 23

Size

Total Lines 41
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 10.005

Importance

Changes 0
Metric Value
cc 10
eloc 22
nc 23
nop 2
dl 0
loc 41
ccs 26
cts 27
cp 0.963
crap 10.005
rs 4.8196
c 0
b 0
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
namespace Anavel\Crud\Abstractor\Eloquent\Relation;
4
5
use Anavel\Crud\Abstractor\Eloquent\Relation\Traits\CheckRelationCompatibility;
6
use Anavel\Crud\Contracts\Abstractor\Field;
7
use Illuminate\Http\Request;
8
9
class Translation extends Relation
10
{
11
    use CheckRelationCompatibility;
12
13
    protected $langs = [];
14
15
    protected $compatibleEloquentRelations = [
16
        'Illuminate\Database\Eloquent\Relations\HasMany',
17
    ];
18
19 5
    public function setup()
20
    {
21 5
        if (empty($this->langs)) {
22 5
            $this->langs = (array)config('anavel.translation_languages');
23 5
        }
24
25 5
        $this->checkRelationCompatibility();
26 5
    }
27
28
    /**
29
     * @return array
30
     */
31 1
    public function getEditFields($arrayKey = null)
32
    {
33
        /** @var \ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer $dbal */
34 1
        $columns = $this->modelManager
35 1
            ->getAbstractionLayer(get_class($this->eloquentRelation->getRelated()))
36 1
            ->getTableColumns();
37
38 1
        $this->readConfig('edit');
39
40 1
        if (empty($columns) || empty($this->langs)) {
41
            return array();
42
        }
43
44 1
        if (empty($arrayKey)) {
45 1
            $arrayKey = $this->name;
46 1
        }
47
48 1
        $results = $this->eloquentRelation->getResults()->keyBy('locale');
49
        
50 1
        $translationFields = [];
51
52 1
        foreach ($this->langs as $key => $lang) {
53 1
            $tempFields = [];
54
55 1
            foreach ($columns as $columnName => $column) {
56 1
                if ($columnName === $this->eloquentRelation->getPlainForeignKey()) {
57 1
                    continue;
58
                }
59
60 1
                if ($columnName === $this->eloquentRelation->getParent()->getKeyName()) {
61
                    continue;
62
                }
63
64 1
                $formType = null;
65
66 1
                if ($columnName === 'locale') {
67 1
                    $formType = 'hidden';
68 1
                }
69
70
                $config = [
71 1
                    'name'         => $columnName,
72 1
                    'presentation' => ucfirst(transcrud($columnName)).' ['.$lang.']',
73 1
                    'form_type'    => $formType,
74 1
                    'no_validate'  => true,
75 1
                    'validation'   => null,
76 1
                    'functions'    => null,
77 1
                ];
78
79 1
                $config = $this->setConfig($config, $columnName);
80
81
                /** @var Field $field */
82 1
                $field = $this->fieldFactory
83 1
                    ->setColumn($column)
84 1
                    ->setConfig($config)
85 1
                    ->get();
86
87 1
                if ($columnName === 'locale') {
88 1
                    $field->setValue($lang);
89 1
                }
90
91 1
                if ($results->has($lang)) {
92
                    $item = $results->get($lang);
93
94
                    $field->setValue($item->getAttribute($columnName));
95
                }
96
97 1
                $tempFields[] = $field;
98 1
            }
99
100 1
            $translationFields[$arrayKey][$lang] = $tempFields;
101 1
        }
102
103 1
        return $translationFields;
104
    }
105
106
    /**
107
     * @param array|null $relationArray
108
     *
109
     * @return mixed
110
     */
111 2
    public function persist(array $relationArray = null, Request $request)
112
    {
113 2
        if (empty($relationArray)) {
114
            return;
115
        }
116
117 2
        $currentTranslations = $this->eloquentRelation->getResults()->keyBy('locale');
118
119 2
        foreach ($relationArray as $translation) {
120 2
            $isEmpty = true;
121
122 2
            foreach ($translation as $fieldKey => $fieldValue) {
123 2
                if ($isEmpty && $fieldKey != 'locale') {
124 2
                    $isEmpty = ($isEmpty === ($fieldValue === ''));
125 2
                }
126 2
            }
127
128 2
            if ($currentTranslations->has($translation['locale'])) {
129 1
                $translationModel = $currentTranslations->get($translation['locale']);
130
                
131 1
                if ($isEmpty) {
132 1
                    $translationModel->delete();
133 1
                    continue;
134
                }
135 1
            } else {
136 1
                $translationModel = $this->eloquentRelation->getRelated()->newInstance();
137
            }
138
139 2
            if ($isEmpty !== false) {
140 1
                continue;
141
            }
142
            
143 2
            $translationModel->setAttribute($this->eloquentRelation->getForeignKey(), $this->relatedModel->id);
144
145 2
            foreach ($translation as $fieldKey => $fieldValue) {
146 2
                $translationModel->setAttribute($fieldKey, $fieldValue);
147 2
            }
148
149 2
            $translationModel->save();
150 2
        }
151 2
    }
152
153
    /**
154
     * @return string
155
     */
156
    public function getDisplayType()
157
    {
158
        return self::DISPLAY_TYPE_INLINE;
159
    }
160
}
161