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

SelectMultiple   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 72.58%

Importance

Changes 0
Metric Value
dl 0
loc 119
ccs 45
cts 62
cp 0.7258
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
B persist() 0 35 6
A addToArray() 0 10 2
A setup() 0 5 1
A getConfig() 0 14 1
A setFieldValue() 0 16 3
A getColumn() 0 4 1
A getDisplayType() 0 4 1
1
<?php
2
3
namespace Anavel\Crud\Abstractor\Eloquent\Relation;
4
5
use Anavel\Crud\Abstractor\Eloquent\Relation\Traits\CheckRelationCompatibility;
6
use Anavel\Crud\Repository\Criteria\InArrayCriteria;
7
use Doctrine\DBAL\Schema\Column;
8
use Illuminate\Http\Request;
9
10
class SelectMultiple extends Select
11
{
12
    use CheckRelationCompatibility;
13
14
    protected $compatibleEloquentRelations = [
15
        'Illuminate\Database\Eloquent\Relations\HasMany',
16
    ];
17
18 6
    public function setup()
19
    {
20 6
        $this->checkRelationCompatibility();
21 6
        $this->checkDisplayConfig();
22 6
    }
23
24
    /**
25
     * @param array|null $relationArray
26
     *
27
     * @return mixed
28
     */
29 1
    public function persist(array $relationArray = null, Request $request)
30
    {
31 1
        if (!empty($relationArray)) {
32
            /** @var \ANavallaSuiza\Laravel\Database\Contracts\Repository\Repository $repo */
33 1
            $repo = $this->modelManager->getRepository(get_class($this->eloquentRelation->getRelated()));
34
35 1
            $relationName = $this->name;
36 1
            $relatedKeyName = $this->eloquentRelation->getRelated()->getKeyName();
37 1
            $alreadyAssociated = $this->relatedModel->$relationName;
38
39 1
            $search = [];
40 1
            if (!empty($relationArray[$relatedKeyName])) {
41
                $search = $relationArray[$relatedKeyName];
42
            }
43 1
            $results = $repo->pushCriteria(
44 1
                new InArrayCriteria($relatedKeyName, $search)
45 1
            )->all();
46
47 1
            $missing = $alreadyAssociated->diff($results);
48
49
50 1
            $keyName = $this->eloquentRelation->getPlainForeignKey();
51 1
            foreach ($results as $result) {
52 1
                $result->$keyName = $this->relatedModel->getKey();
53 1
                $result->save();
54 1
            }
55
56 1
            if (!$missing->isEmpty()) {
57
                foreach ($missing as $result) {
58
                    $result->$keyName = null;
59
                    $result->save();
60
                }
61
            }
62 1
        }
63 1
    }
64
65 2
    protected function addToArray($arrayKey, $field)
66
    {
67 2
        $select = [];
68 2
        if ($arrayKey == 'main') {
69
            $arrayKey = $this->name;
70
        }
71 2
        $select[$arrayKey][] = $field;
72
73 2
        return $select;
74
    }
75
76
    /**
77
     * @return array
78
     */
79 2
    protected function getConfig()
80
    {
81
        return [
82 2
            'name'         => $this->eloquentRelation->getRelated()->getKeyName(),
83 2
            'presentation' => $this->getPresentation(),
84 2
            'form_type'    => 'select',
85
            'attr'         => [
86 2
                'multiple' => true,
87 2
            ],
88 2
            'no_validate' => true,
89 2
            'validation'  => null,
90 2
            'functions'   => null,
91 2
        ];
92
    }
93
94 2
    protected function setFieldValue($field)
95
    {
96 2
        $results = $this->eloquentRelation->getResults();
97
98 2
        if (!$results->isEmpty()) {
99
            $values = [];
100
101
            foreach ($results as $result) {
102
                $values[] = $result->getKey();
103
            }
104
105
            $field->setValue($values);
106
        }
107
108 2
        return $field;
109
    }
110
111
    /**
112
     * @param \ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer $dbal
113
     *
114
     * @return Column
115
     */
116 1
    protected function getColumn($dbal)
117
    {
118 1
        return $dbal->getTableColumn($this->eloquentRelation->getPlainForeignKey());
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function getDisplayType()
125
    {
126
        return self::DISPLAY_TYPE_INLINE;
127
    }
128
}
129