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

HasMany   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getFreshModelForElements() 0 5 1
A prepareRelatedValues() 0 11 3
A initialize() 0 6 1
A proceedSave() 0 11 2
A getModelForElements() 0 3 1
A retrieveRelationValuesFromQuery() 0 3 1
1
<?php
2
3
namespace SleepingOwl\Admin\Form\Related\Forms;
4
5
use Illuminate\Support\Collection;
6
use SleepingOwl\Admin\Form\Related\Elements;
7
8
class HasMany extends Elements
9
{
10
    public function initialize()
11
    {
12
        parent::initialize();
13
14
        $this->modifyQuery(function ($query) {
15
            $query->orderBy($this->getEmptyRelation()->getRelated()->getKeyName());
16
        });
17
    }
18
19
    protected function proceedSave(\Illuminate\Http\Request $request)
20
    {
21
        $relation = $this->getRelation();
22
23
        // First we need to remove all entities
24
        if (! $this->toRemove->isEmpty()) {
25
            $class = get_class($relation->getRelated());
26
            $class::destroy($this->toRemove->all());
27
        }
28
29
        $relation->saveMany($this->relatedValues);
30
    }
31
32
    protected function prepareRelatedValues(array $data)
33
    {
34
        $elements = $this->flatNamedElements($this->getNewElements());
35
        foreach ($data as $relatedId => $attributes) {
36
            $related = $this->addOrGetRelated($relatedId);
37
38
            foreach ($elements as $element) {
39
                $attribute = $element->getModelAttributeKey();
40
                $value = $element->prepareValue(array_get($attributes, $attribute));
41
                $related->setAttribute($attribute, $value);
42
                $element->setModel($related);
43
            }
44
        }
45
    }
46
47
    protected function retrieveRelationValuesFromQuery($query)
48
    {
49
        return $query->get()->keyBy($this->getRelation()->getRelated()->getKeyName());
50
    }
51
52
    protected function getModelForElements()
53
    {
54
        return $this->getEmptyRelation()->getRelated();
55
    }
56
57
    /**
58
     * Returns fresh instance of model for each element in form.
59
     *
60
     * @return \Illuminate\Database\Eloquent\Model
61
     */
62
    protected function getFreshModelForElements()
63
    {
64
        $class = get_class($this->getEmptyRelation()->getRelated());
65
66
        return new $class();
67
    }
68
}
69