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
Push — master ( ce7a57...96525c )
by Aden
03:03
created

ModelSaving::afterSave()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 3
Ratio 21.43 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 3
loc 14
rs 9.2
cc 4
eloc 6
nc 3
nop 0
1
<?php
2
3
namespace LaravelFlare\Flare\Traits\ModelAdmin;
4
5
use LaravelFlare\Flare\Events\ModelSave;
6
use LaravelFlare\Flare\Events\AfterSave;
7
use LaravelFlare\Flare\Events\BeforeSave;
8
use LaravelFlare\Flare\Exceptions\ModelAdminWriteableException as WriteableException;
9
10
trait ModelSaving
11
{
12
    /**
13
     * Used by beforeSave() to ensure child classes call parent::beforeSave().
14
     * 
15
     * @var bool
16
     */
17
    protected $brokenBeforeSave = false;
18
19
    /**
20
     * Used by afterSave() to ensure child classes call parent::afterSave().
21
     * 
22
     * @var bool
23
     */
24
    protected $brokenAfterSave = false;
25
26
    /**
27
     * Relations to Update during Save and
28
     * the appropriate method to fire the update with.
29
     * 
30
     * @var array
31
     */
32
    protected $doSaveRelations = ['BelongsTo' => 'associate'];
33
34
    /**
35
     * Relations to Update after Save and
36
     * the appropriate method to fire the update with.
37
     * 
38
     * @var array
39
     */
40
    protected $afterSaveRelations = ['BelongsToMany' => 'sync'];
41
42
    /**
43
     * Method fired before the Save action is undertaken.
44
     * 
45
     * @return
46
     */
47
    protected function beforeSave()
48
    {
49
        $this->brokenBeforeSave = false;
50
51
        event(new BeforeSave($this));
52
    }
53
54
    /**
55
     * Save Action.
56
     *
57
     * Fires off beforeSave(), doSave() and afterSave()
58
     * 
59
     * @return
60
     */
61
    public function save()
62
    {
63
        $this->brokenBeforeSave = true;
64
        $this->beforeSave();
65
        if ($this->brokenBeforeSave) {
66
            throw new WriteableException('ModelAdmin has a broken beforeSave method. Make sure you call parent::beforeSave() on all instances of beforeSave()', 1);
67
        }
68
69
        $this->doSave();
70
71
        $this->brokenAfterSave = true;
72
        $this->afterSave();
73
        if ($this->brokenAfterSave) {
74
            throw new WriteableException('ModelAdmin has a broken afterSave method. Make sure you call parent::afterSave() on all instances of afterSave()', 1);
75
        }
76
    }
77
78
    /**
79
     * The actual Save action, which does all of hte pre-processing
80
     * required before we are able to perform the save() function.
81
     * 
82
     * @return
83
     */
84
    private function doSave()
85
    {
86
        foreach (\Request::only(array_keys($this->fields)) as $key => $value) {
0 ignored issues
show
Bug introduced by
The property fields does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
87
            if (!\Schema::hasColumn($this->model->getTable(), $key)) {
88
                continue;
89
            }
90
91
            if ($this->hasSetMutator($key)) {
0 ignored issues
show
Bug introduced by
It seems like hasSetMutator() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
92
                $this->setAttribute($key, $value);
0 ignored issues
show
Bug introduced by
It seems like setAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
93
                continue;
94
            }
95
96
            // Could swap this out for model -> getAttribute, then check if we have an attribute or a relation... getRelationValue() is helpful
97 View Code Duplication
            if (method_exists($this->model, $key) && is_a(call_user_func_array([$this->model, $key], []), 'Illuminate\Database\Eloquent\Relations\Relation')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
                $this->saveRelation('doSave', $key, $value);
99
                continue;
100
            }
101
102
            $this->model->setAttribute($key, $value);
103
        }
104
105
        $this->model->save();
106
107
        event(new ModelSave($this));
108
    }
109
110
    /**
111
     * Method fired after the Save action is complete.
112
     * 
113
     * @return
114
     */
115
    protected function afterSave()
116
    {
117
        $this->brokenAfterSave = false;
118
119
        foreach (\Request::except('_token') as $key => $value) {
120
121
            // Could swap this out for model -> getAttribute, then check if we have an attribute or a relation... getRelationValue() is helpful
122 View Code Duplication
            if (method_exists($this->model, $key) && is_a(call_user_func_array([$this->model, $key], []), 'Illuminate\Database\Eloquent\Relations\Relation')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
                $this->saveRelation('afterSave', $key, $value);
124
            }
125
        }
126
127
        event(new AfterSave($this));
128
    }
129
130
    /**
131
     * Method fired to Save Relations.
132
     *
133
     * @param string $action The current action (either doSave or afterSave)
134
     * @param string $key
135
     * @param string $value
136
     * 
137
     * @return
138
     */
139
    private function saveRelation($action, $key, $value)
140
    {
141
        // Could swap this out for model -> getAttribute, then check if we have an attribute or a relation... getRelationValue() is helpful
142
        if (method_exists($this->model, $key) && is_a(call_user_func_array([$this->model, $key], []), 'Illuminate\Database\Eloquent\Relations\Relation')) {
143
            foreach ($this->{$action.'Relations'} as $relationship => $method) {
144
                if (is_a(call_user_func_array([$this->model, $key], []), 'Illuminate\Database\Eloquent\Relations\\'.$relationship)) {
145
                    $this->model->$key()->$method($value);
146
147
                    return;
148
                }
149
            }
150
        }
151
    }
152
}
153