SelectMultiple   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 72.58%

Importance

Changes 0
Metric Value
dl 0
loc 118
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  
A setup() 0 5 1
B persist() 0 34 6
A addToArray() 0 10 2
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 1
            $keyName = $this->eloquentRelation->getPlainForeignKey();
50 1
            foreach ($results as $result) {
51 1
                $result->$keyName = $this->relatedModel->getKey();
52 1
                $result->save();
53 1
            }
54
55 1
            if (!$missing->isEmpty()) {
56
                foreach ($missing as $result) {
57
                    $result->$keyName = null;
58
                    $result->save();
59
                }
60
            }
61 1
        }
62 1
    }
63
64 2
    protected function addToArray($arrayKey, $field)
65
    {
66 2
        $select = [];
67 2
        if ($arrayKey == 'main') {
68
            $arrayKey = $this->name;
69
        }
70 2
        $select[$arrayKey][] = $field;
71
72 2
        return $select;
73
    }
74
75
    /**
76
     * @return array
77
     */
78 2
    protected function getConfig()
79
    {
80
        return [
81 2
            'name'         => $this->eloquentRelation->getRelated()->getKeyName(),
82 2
            'presentation' => $this->getPresentation(),
83 2
            'form_type'    => 'select',
84
            'attr'         => [
85 2
                'multiple' => true,
86 2
            ],
87 2
            'no_validate' => true,
88 2
            'validation'  => null,
89 2
            'functions'   => null,
90 2
        ];
91
    }
92
93 2
    protected function setFieldValue($field)
94
    {
95 2
        $results = $this->eloquentRelation->getResults();
96
97 2
        if (!$results->isEmpty()) {
98
            $values = [];
99
100
            foreach ($results as $result) {
101
                $values[] = $result->getKey();
102
            }
103
104
            $field->setValue($values);
105
        }
106
107 2
        return $field;
108
    }
109
110
    /**
111
     * @param \ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer $dbal
112
     *
113
     * @return Column
114
     */
115 1
    protected function getColumn($dbal)
116
    {
117 1
        return $dbal->getTableColumn($this->eloquentRelation->getPlainForeignKey());
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    public function getDisplayType()
124
    {
125
        return self::DISPLAY_TYPE_INLINE;
126
    }
127
}
128