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

Select   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 75.86%

Importance

Changes 0
Metric Value
dl 0
loc 139
ccs 44
cts 58
cp 0.7586
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 8

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 5 1
B getEditFields() 0 36 2
A addToArray() 0 7 1
A persist() 0 4 1
A setDisplay() 0 16 4
A getConfig() 0 10 1
A setFieldValue() 0 10 2
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 Doctrine\DBAL\Schema\Column;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Http\Request;
9
10
class Select extends Relation
11
{
12
    use CheckRelationCompatibility;
13
14
    protected $compatibleEloquentRelations = [
15
        'Illuminate\Database\Eloquent\Relations\BelongsTo',
16
    ];
17
18 5
    public function setup()
19
    {
20 5
        $this->checkRelationCompatibility();
21 5
        $this->checkDisplayConfig();
22 5
    }
23
24
    /**
25
     * @param string|null $arrayKey
26
     *
27
     * @return array
28
     */
29 4
    public function getEditFields($arrayKey = 'main')
30
    {
31
        /** @var \ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer $dbal */
32 4
        $dbal = $this->modelManager->getAbstractionLayer(get_class($this->eloquentRelation->getRelated()));
33
34 4
        $column = $this->getColumn($dbal);
35
36 4
        $repo = $this->modelManager->getRepository(get_class($this->eloquentRelation->getRelated()));
37
38 4
        $results = $repo->all();
39
40 4
        $options = ['' => ''];
41
42 4
        $this->readConfig('edit');
43
44 4
        foreach ($results as $result) {
45 2
            $options[$result->getKey()] = $this->setDisplay($result);
46 4
        }
47
48 4
        $config = $this->getConfig();
49
50 4
        $config = $this->setConfig($config, $column->getName());
51
52 4
        $field = $this->fieldFactory
53 4
            ->setColumn($column)
54 4
            ->setConfig($config)
55 4
            ->get();
56
57 4
        $field->setOptions($options);
58
59 4
        $field = $this->setFieldValue($field);
60
61 4
        $select = $this->addToArray($arrayKey, $field);
62
63 4
        return $select;
64
    }
65
66 2
    protected function addToArray($arrayKey, $field)
67
    {
68 2
        $select = [];
69 2
        $select[$arrayKey][] = $field;
70
71 2
        return $select;
72
    }
73
74
    /**
75
     * @param array|null $relationArray
76
     *
77
     * @return mixed
78
     */
79
    public function persist(array $relationArray = null, Request $request)
80
    {
81
        //
82
    }
83
84
    /**
85
     * @param Model $result
86
     *
87
     * @return string
88
     */
89 2
    protected function setDisplay($result)
90
    {
91 2
        if (is_array($this->config['display'])) {
92
            $displayString = '';
93
            foreach ($this->config['display'] as $key => $display) {
94
                if ($key !== 0) {
95
                    $displayString .= ' | ';
96
                }
97
                $displayString .= $result->getAttribute($display);
98
            }
99
100
            return $displayString;
101
        }
102
103 2
        return $result->getAttribute($this->config['display']);
104
    }
105
106
    /**
107
     * @return array
108
     */
109 2
    protected function getConfig()
110
    {
111
        return [
112 2
            'name'         => $this->eloquentRelation->getForeignKey(),
113 2
            'presentation' => $this->getPresentation(),
114 2
            'form_type'    => 'select',
115 2
            'validation'   => null,
116 2
            'functions'    => null,
117 2
        ];
118
    }
119
120 2
    protected function setFieldValue($field)
121
    {
122 2
        $results = $this->eloquentRelation->getResults();
123
124 2
        if (!empty($results)) {
125
            $field->setValue($results->getKey());
126
        }
127
128 2
        return $field;
129
    }
130
131
    /**
132
     * @param \ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer $dbal
133
     *
134
     * @return Column
135
     */
136 2
    protected function getColumn($dbal)
137
    {
138 2
        return $dbal->getTableColumn($this->eloquentRelation->getOtherKey());
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    public function getDisplayType()
145
    {
146
        return self::DISPLAY_TYPE_INLINE;
147
    }
148
}
149