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.

ModelSaving::saveRelation()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.5222
c 0
b 0
f 0
cc 5
nc 4
nop 3
1
<?php
2
3
namespace LaravelFlare\Flare\Admin\Models\Traits;
4
5
use LaravelFlare\Flare\Events\ModelSave;
6
use LaravelFlare\Flare\Events\AfterSave;
7
use LaravelFlare\Flare\Events\BeforeSave;
8
9
trait ModelSaving
10
{
11
    /**
12
     * Relations to Update during Save and
13
     * the appropriate method to fire the update with.
14
     * 
15
     * @var array
16
     */
17
    protected $doSaveRelations = ['BelongsTo' => 'associate'];
18
19
    /**
20
     * Relations to Update after Save and
21
     * the appropriate method to fire the update with.
22
     * 
23
     * @var array
24
     */
25
    protected $afterSaveRelations = ['BelongsToMany' => 'sync'];
26
27
    /**
28
     * Method fired before the Save action is undertaken.
29
     * 
30
     * @return
31
     */
32
    protected function beforeSave()
33
    {
34
    }
35
36
    /**
37
     * Save Action.
38
     *
39
     * Fires off beforeSave(), doSave() and afterSave()
40
     * 
41
     * @return
42
     */
43
    public function save()
44
    {
45
        event(new BeforeSave($this));
46
47
        $this->beforeSave();
48
49
        $this->doSave();
50
51
        $this->afterSave();
52
53
        event(new AfterSave($this));
54
    }
55
56
    /**
57
     * The actual Save action, which does all of hte pre-processing
58
     * required before we are able to perform the save() function.
59
     * 
60
     * @return
61
     */
62
    private function doSave()
63
    {
64
        foreach (\Request::only(array_keys($this->fields)) as $key => $value) {
65
            if ($this->hasSetMutator($key)) {
66
                $this->setAttribute($key, $value);
67
                continue;
68
            }
69
70
            // Could swap this out for model -> getAttribute, then check if we have an attribute or a relation... getRelationValue() is helpful
71 View Code Duplication
            if (method_exists($this->model, $key) && is_a(call_user_func_array([$this->model, $key], []), 'Illuminate\Database\Eloquent\Relations\Relation')) {
72
                $this->saveRelation('doSave', $key, $value);
73
                continue;
74
            }
75
76
            $this->model->setAttribute($key, $value);
77
        }
78
79
        $this->model->save();
80
81
        event(new ModelSave($this));
82
    }
83
84
    /**
85
     * Method fired after the Save action is complete.
86
     * 
87
     * @return
88
     */
89
    protected function afterSave()
90
    {
91
        $this->brokenAfterSave = false;
92
93
        foreach (\Request::except('_token') as $key => $value) {
94
95
            // Could swap this out for model -> getAttribute, then check if we have an attribute or a relation... getRelationValue() is helpful
96 View Code Duplication
            if (method_exists($this->model, $key) && is_a(call_user_func_array([$this->model, $key], []), 'Illuminate\Database\Eloquent\Relations\Relation')) {
97
                $this->saveRelation('afterSave', $key, $value);
98
            }
99
        }
100
    }
101
102
    /**
103
     * Method fired to Save Relations.
104
     *
105
     * @param string $action The current action (either doSave or afterSave)
106
     * @param string $key
107
     * @param string $value
108
     * 
109
     * @return
110
     */
111
    private function saveRelation($action, $key, $value)
112
    {
113
        // Could swap this out for model -> getAttribute, then check if we have an attribute or a relation... getRelationValue() is helpful
114
        if (method_exists($this->model, $key) && is_a(call_user_func_array([$this->model, $key], []), 'Illuminate\Database\Eloquent\Relations\Relation')) {
115
            foreach ($this->{$action.'Relations'} as $relationship => $method) {
116
                if (is_a(call_user_func_array([$this->model, $key], []), 'Illuminate\Database\Eloquent\Relations\\'.$relationship)) {
117
                    $this->model->$key()->$method($value);
118
119
                    return;
120
                }
121
            }
122
        }
123
    }
124
}
125