Completed
Pull Request — master (#74)
by
unknown
13:28 queued 09:37
created

MiniCrudSingle::getEditFields()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 78
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 49
CRAP Score 6.0002

Importance

Changes 0
Metric Value
cc 6
eloc 46
nc 4
nop 1
dl 0
loc 78
ccs 49
cts 50
cp 0.98
crap 6.0002
rs 8.4316
c 0
b 0
f 0

How to fix   Long Method   

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\Database\Eloquent\Model;
8
use Illuminate\Http\Request;
9
10
class MiniCrudSingle extends Relation
11
{
12
    use CheckRelationCompatibility;
13
14
    protected $compatibleEloquentRelations = [
15
        'Illuminate\Database\Eloquent\Relations\MorphOne',
16
    ];
17
18 5
    public function setup()
19
    {
20 5
        $this->checkRelationCompatibility();
21 4
    }
22
23
    /**
24
     * @return array
25
     */
26 1
    public function getEditFields($arrayKey = null)
27
    {
28 1
        if (empty($arrayKey)) {
29 1
            $arrayKey = $this->name;
30 1
        }
31
32 1
        $fields = [];
33
34 1
        $columns = $this->modelAbstractor->getColumns('edit');
35
36
        /** @var Model $result */
37 1
        $result = $this->eloquentRelation->getResults();
38
39
        $readOnly = [
40 1
            Model::CREATED_AT,
41 1
            Model::UPDATED_AT,
42 1
            $this->eloquentRelation->getPlainForeignKey(),
43 1
            $this->eloquentRelation->getPlainMorphType(),
44 1
            $this->eloquentRelation->getParent()->getKeyName(),
45 1
        ];
46
47
48 1
        $this->readConfig('edit');
49
50 1
        if (!empty($columns)) {
51
            //Add field for model deletion
52
            $config = [
53 1
                'name'         => '__delete',
54 1
                'presentation' => 'Delete',
55 1
                'form_type'    => 'checkbox',
56 1
                'no_validate'  => true,
57 1
                'validation'   => null,
58 1
                'functions'    => null,
59 1
            ];
60
61
            /** @var Field $field */
62 1
            $field = $this->fieldFactory
63 1
                ->setConfig($config)
64 1
                ->get();
65 1
            $fields[$arrayKey]['__delete'] = $field;
66
67 1
            foreach ($columns as $columnName => $column) {
68 1
                if (in_array($columnName, $readOnly, true)) {
69
                    continue;
70
                }
71
72 1
                $formType = null;
73
74
                $config = [
75 1
                    'name'         => $columnName,
76 1
                    'presentation' => $this->name.' '.ucfirst(transcrud($columnName)),
77 1
                    'form_type'    => $formType,
78 1
                    'no_validate'  => true,
79 1
                    'validation'   => null,
80 1
                    'functions'    => null,
81 1
                ];
82
83 1
                $config = $this->setConfig($config, $columnName);
84
85
                /** @var Field $field */
86 1
                $field = $this->fieldFactory
87 1
                    ->setColumn($column)
88 1
                    ->setConfig($config)
89 1
                    ->get();
90
91 1
                if (!empty($result->id)) {
92 1
                    $field->setValue($result->getAttribute($columnName));
93 1
                }
94
95 1
                $fields[$arrayKey][$columnName] = $field;
96 1
            }
97 1
        }
98
99 1
        $fields = $this->addSecondaryRelationFields($fields);
100
101
102 1
        return $fields;
103
    }
104
105
    /**
106
     * @param array|null $relationArray
107
     *
108
     * @return mixed
109
     */
110 2
    public function persist(array $relationArray = null, Request $request)
111
    {
112 2
        if (!empty($relationArray)) {
113 2
            $currentRelation = $this->eloquentRelation->getResults();
114 2
            if (!empty($currentRelation)) {
115 1
                $relationModel = $currentRelation;
116 1
            } else {
117 1
                $relationModel = $this->eloquentRelation->getRelated()->newInstance();
118
            }
119
120 2
            $this->setKeys($relationModel);
121
122 2
            $shouldBeSkipped = true;
123 2
            foreach ($relationArray as $fieldKey => $fieldValue) {
124
                // This field can only come from existing models
125 2
                if ($fieldKey === '__delete') {
126
                    $relationModel->delete();
127
                    $shouldBeSkipped = true;
128
                    break;
129
                }
130 2
                if ($shouldBeSkipped) {
131 2
                    $shouldBeSkipped = ($shouldBeSkipped === ($fieldValue === ''));
132 2
                }
133 2
                $relationModel->setAttribute($fieldKey, $fieldValue);
134 2
            }
135
136 2
            if (!$shouldBeSkipped) {
137 2
                $relationModel->save();
138 2
            }
139 2
        }
140 2
    }
141
142 2
    protected function setKeys(Model $relationModel)
143
    {
144 2
        $relationModel->setAttribute($this->eloquentRelation->getForeignKey(), $this->relatedModel->id);
145 2
        $relationModel->setAttribute($this->eloquentRelation->getPlainMorphType(),
146 2
            $this->eloquentRelation->getMorphClass());
147 2
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function getDisplayType()
153
    {
154
        return self::DISPLAY_TYPE_TAB;
155
    }
156
}
157