Completed
Push — master ( 5b69f2...ce3c4a )
by
unknown
05:25
created

SelectMultiple::setFieldValue()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4.944
Metric Value
dl 0
loc 15
ccs 4
cts 10
cp 0.4
rs 9.4285
cc 3
eloc 8
nc 2
nop 1
crap 4.944
1
<?php
2
namespace Anavel\Crud\Abstractor\Eloquent\Relation;
3
4
use Anavel\Crud\Abstractor\Eloquent\Relation\Traits\CheckRelationCompatibility;
5
use Anavel\Crud\Abstractor\Eloquent\Relation\Traits\CheckRelationConfig;
6
use Anavel\Crud\Repository\Criteria\InArrayCriteria;
7
use App;
8
use Doctrine\DBAL\Schema\Column;
9
use Illuminate\Http\Request;
10
11
class SelectMultiple extends Select
12
{
13
    use CheckRelationCompatibility;
14
15
    protected $compatibleEloquentRelations = array(
16
        'Illuminate\Database\Eloquent\Relations\HasMany'
17
    );
18
19 6
    public function setup()
20
    {
21 6
        $this->checkRelationCompatibility();
22 6
        $this->checkDisplayConfig();
23 6
    }
24
25
    /**
26
     * @param array|null $relationArray
27
     * @return mixed
28
     */
29 1
    public function persist(array $relationArray = null)
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
66
67 1
    protected function addToArray($arrayKey, $field)
68
    {
69 1
        $select = [];
70 1
        if ($arrayKey == 'main') {
71
            $arrayKey = $this->name;
72
        }
73 1
        $select[$arrayKey][] = $field;
74 1
        return $select;
75
    }
76
77
    /**
78
     * @return array
79
     */
80 1
    protected function getConfig()
81
    {
82
        return [
83 1
            'name' => $this->eloquentRelation->getRelated()->getKeyName(),
84 1
            'presentation' => $this->getPresentation(),
85 1
            'form_type' => 'select',
86
            'attr' => [
87
                'multiple' => true
88 1
            ],
89 1
            'no_validate' => true,
90 1
            'validation' => null,
91
            'functions' => null
92 1
        ];
93
    }
94
95
96
97 1
    protected function setFieldValue($field)
98
    {
99 1
        $results = $this->eloquentRelation->getResults();
100
101 1
        if (! $results->isEmpty()) {
102
            $values = [];
103
104
            foreach ($results as $result) {
105
                $values[] = $result->getKey();
106
            }
107
108
            $field->setValue($values);
109
        }
110 1
        return $field;
111
    }
112
113
114
    /**
115
     * @param \ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer $dbal
116
     * @return Column
117
     */
118 1
    protected function getColumn($dbal)
119
    {
120 1
        return $dbal->getTableColumn($this->eloquentRelation->getPlainForeignKey());
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    public function getDisplayType()
127
    {
128
        return self::DISPLAY_TYPE_INLINE;
129
    }
130
}
131