Completed
Pull Request — master (#74)
by Adrian
11:16
created

Translation::getEditFields()   C

Complexity

Conditions 10
Paths 4

Size

Total Lines 71
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 10.0616

Importance

Changes 0
Metric Value
cc 10
eloc 40
nc 4
nop 1
dl 0
loc 71
ccs 43
cts 47
cp 0.9149
crap 10.0616
rs 5.9513
c 0
b 0
f 0

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
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 = config('anavel.translation_languages');
0 ignored issues
show
Documentation Bug introduced by
It seems like config('anavel.translation_languages') of type * is incompatible with the declared type array of property $langs.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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
        $dbal = $this->modelManager->getAbstractionLayer(get_class($this->eloquentRelation->getRelated()));
35
36 1
        $columns = $dbal->getTableColumns();
37
38 1
        $results = $this->eloquentRelation->getResults();
39
40 1
        $results = $results->keyBy('locale');
41
42 1
        $this->readConfig('edit');
43
44 1
        if (empty($arrayKey)) {
45 1
            $arrayKey = $this->name;
46 1
        }
47
48 1
        $translationFields = [];
49 1
        if (!empty($columns)) {
50 1
            foreach ($this->langs as $key => $lang) {
51 1
                $tempFields = [];
52 1
                foreach ($columns as $columnName => $column) {
53 1
                    if ($columnName === $this->eloquentRelation->getPlainForeignKey()) {
54 1
                        continue;
55
                    }
56
57 1
                    if ($columnName === $this->eloquentRelation->getParent()->getKeyName()) {
58
                        continue;
59
                    }
60
61 1
                    $formType = null;
62
63 1
                    if ($columnName === 'locale') {
64 1
                        $formType = 'hidden';
65 1
                    }
66
67
                    $config = [
68 1
                        'name'         => $columnName,
69 1
                        'presentation' => ucfirst(transcrud($columnName)).' ['.$lang.']',
70 1
                        'form_type'    => $formType,
71 1
                        'no_validate'  => true,
72 1
                        'validation'   => null,
73 1
                        'functions'    => null,
74 1
                    ];
75
76 1
                    $config = $this->setConfig($config, $columnName);
77
78
                    /** @var Field $field */
79 1
                    $field = $this->fieldFactory
80 1
                        ->setColumn($column)
81 1
                        ->setConfig($config)
82 1
                        ->get();
83
84 1
                    if ($columnName === 'locale') {
85 1
                        $field->setValue($lang);
86 1
                    }
87
88 1
                    if ($results->has($lang)) {
89
                        $item = $results->get($lang);
90
91
                        $field->setValue($item->getAttribute($columnName));
92
                    }
93
94 1
                    $tempFields[] = $field;
95 1
                }
96 1
                $translationFields[$arrayKey][$lang] = $tempFields;
97 1
            }
98 1
        }
99
100 1
        return $translationFields;
101
    }
102
103
    /**
104
     * @param array|null $relationArray
105
     *
106
     * @return mixed
107
     */
108 2
    public function persist(array $relationArray = null, Request $request)
109
    {
110 2
        if (!empty($relationArray)) {
111 2
            $currentTranslations = $this->eloquentRelation->getResults();
112 2
            $currentTranslations = $currentTranslations->keyBy('locale');
113
114 2
            foreach ($relationArray as $translation) {
115 2
                $isEmpty = true;
116
117 2
                foreach ($translation as $fieldKey => $fieldValue) {
118 2
                    if ($isEmpty && $fieldKey != 'locale') {
119 2
                        $isEmpty = ($isEmpty === ($fieldValue === ''));
120 2
                    }
121 2
                }
122
123 2
                if ($currentTranslations->has($translation['locale'])) {
124 1
                    $translationModel = $currentTranslations->get($translation['locale']);
125 1
                    if ($isEmpty) {
126 1
                        $translationModel->delete();
127 1
                        continue;
128
                    }
129 1
                } else {
130 1
                    $translationModel = $this->eloquentRelation->getRelated()->newInstance();
131
                }
132
133 2
                if ($isEmpty === false) {
134 2
                    $translationModel->setAttribute($this->eloquentRelation->getForeignKey(), $this->relatedModel->id);
135
136 2
                    foreach ($translation as $fieldKey => $fieldValue) {
137 2
                        $translationModel->setAttribute($fieldKey, $fieldValue);
138 2
                    }
139 2
                    $translationModel->save();
140 2
                }
141 2
            }
142 2
        }
143 2
    }
144
145
    /**
146
     * @return string
147
     */
148
    public function getDisplayType()
149
    {
150
        return self::DISPLAY_TYPE_INLINE;
151
    }
152
}
153