GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — development (#846)
by
unknown
08:14
created

ManyToMany::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Form\Related\Forms;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Collection;
7
use SleepingOwl\Admin\Form\Related\Elements;
8
use SleepingOwl\Admin\Form\Columns\Column;
9
use SleepingOwl\Admin\Form\Columns\Columns;
10
use SleepingOwl\Admin\Form\Related\Select;
11
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
12
13
/**
14
 * Class ManyToMany
15
 *
16
 * @package Admin\Form\Elements\RelatedElements\Forms
17
 *
18
 * @method BelongsToMany getEmptyRelation()
19
 */
20
class ManyToMany extends Elements
21
{
22
    /**
23
     * Relation name of the model.
24
     *
25
     * @var string
26
     */
27
    protected $relationName;
28
29
    protected $primaries;
30
31
    protected $relatedElement;
32
33
    /**
34
     * @var Columns
35
     */
36
    protected $relatedWrapper;
37
38
    public function __construct($relationName, array $elements = [])
39
    {
40
        $elements = array_prepend($elements, $this->relatedElement = new Select('__new_element__'));
41
42
        parent::__construct($relationName, $elements);
43
    }
44
45
    public function getPrimaries()
46
    {
47
        return $this->primaries ?: [
48
            $this->getEmptyRelation()->getForeignPivotKeyName(),
49
            $this->getRelatedForeignKeyName(),
50
        ];
51
    }
52
53
    public function setPrimaries(array $primaries)
54
    {
55
        $this->primaries = $primaries;
56
    }
57
58
    public function getRelatedElement()
59
    {
60
        return $this->relatedElement;
61
    }
62
63
    /**
64
     * @return void
65
     */
66
    public function initializeElements()
67
    {
68
        $this->initializeRelatedElement();
69
        parent::initializeElements();
70
71
    }
72
73
    protected function initializeRelatedElement()
74
    {
75
        $select = $this->getRelatedElement();
76
77
        $select->setName($name = $this->getRelatedForeignKeyName());
78
        $select->setModelAttributeKey($name);
79
        $select->setPath($this->getEmptyRelation()->getRelated()->getKeyName());
80
        $select->setModelForOptions(get_class($this->getEmptyRelation()->getRelated()));
81
        $select->setModel($this->getModelForElements());
82
        $select->required();
83
84
        $this->unique([$name], trans('sleeping_owl::lang.form.unique'));
0 ignored issues
show
Bug introduced by
It seems like trans('sleeping_owl::lang.form.unique') can also be of type array; however, parameter $message of SleepingOwl\Admin\Form\Related\Elements::unique() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
        $this->unique([$name], /** @scrutinizer ignore-type */ trans('sleeping_owl::lang.form.unique'));
Loading history...
85
86
        if ($this->relatedWrapper) {
87
            $this->getElements()->forget(0);
88
            $column = new Column([$select]);
89
            $this->relatedWrapper->getElements()->prepend($column);
90
        }
91
    }
92
93
    protected function retrieveRelationValuesFromQuery($query)
94
    {
95
        return $query->get()->pluck('pivot')->keyBy(function ($item) {
96
            return $this->getKeyFromItem($item);
97
        })->forget($this->toRemove->all());
98
    }
99
100
    /**
101
     * @param $item
102
     *
103
     * @return string
104
     */
105
    protected function getKeyFromItem($item)
106
    {
107
        return $this->getCompositeKey($item, $this->getPrimaries());
108
    }
109
110
    /**
111
     * @param \Illuminate\Support\Collection $values
112
     *
113
     * @return Collection
114
     */
115
    protected function buildRelatedMap(Collection $values)
116
    {
117
        $relatedKey = $this->getRelatedForeignKeyName();
118
119
        return $values->mapWithKeys(function ($attributes) use ($relatedKey) {
120
            return [
121
                array_get($attributes, $relatedKey) => array_except($attributes, [
122
                    $relatedKey,
123
                    $this->getEmptyRelation()->getForeignPivotKeyName(),
124
                ]),
125
            ];
126
        });
127
    }
128
129
    protected function getRelatedForeignKeyName()
130
    {
131
        return $this->getEmptyRelation()->getRelatedPivotKeyName();
132
    }
133
134
    protected function proceedSave(Request $request)
135
    {
136
        // By this time getModel method will always return existed model object, not empty
137
        // so wee need to fresh it, because if it's new model creating relation will throw
138
        // exception 'call relation method on null'
139
        $relation = $this->getRelation();
140
        $relation->sync($this->buildRelatedMap($this->relatedValues));
141
    }
142
143
    protected function prepareRelatedValues(array $data)
144
    {
145
        $elements = $this->flatNamedElements($this->getNewElements());
146
        foreach ($data as $key => $attributes) {
147
            $related = $this->addOrGetRelated($key);
148
149
            foreach ($elements as $index => $element) {
150
                $attribute = $element->getModelAttributeKey();
151
                $value = $element->prepareValue(array_get($attributes, $attribute));
152
                $related->setAttribute($attribute, $value);
153
154
                $element->setModel($related);
155
            }
156
        }
157
    }
158
159
    protected function getModelForElements()
160
    {
161
        return $this->getEmptyRelation()->newPivot();
162
    }
163
164
    public function wrapRelatedInto(Columns $columns)
165
    {
166
        $this->relatedWrapper = $columns;
167
    }
168
169
    public function dontWrap()
170
    {
171
        $this->relatedWrapper = null;
172
    }
173
174
    public function __call($name, $arguments)
175
    {
176
        if (method_exists($this->getRelatedElement(), $name)) {
177
            $this->getRelatedElement()->$name(...$arguments);
178
        }
179
180
        throw new \RuntimeException("Method {$name} doesn't exist.");
181
    }
182
183
    /**
184
     * Returns fresh instance of model for each element in form.
185
     *
186
     * @return \Illuminate\Database\Eloquent\Model
187
     */
188
    protected function getFreshModelForElements()
189
    {
190
        return $this->getModelForElements();
191
    }
192
}
193