Issues (68)

src/Eloquent/Concerns/MutatesModel.php (6 issues)

1
<?php
2
3
namespace Bakery\Eloquent\Concerns;
4
5
use Illuminate\Support\Facades\DB;
6
use Illuminate\Database\Eloquent\Model;
7
8
trait MutatesModel
9
{
10
    use QueuesTransactions;
11
    use InteractsWithRelations;
12
    use InteractsWithAttributes;
13
14
    /**
15
     * @var \Illuminate\Contracts\Auth\Access\Gate
16
     */
17
    protected $gate;
18
19
    /**
20
     * @var \Illuminate\Database\Eloquent\Model
21
     */
22
    protected $instance;
23
24
    /**
25
     * Return the policy of the class.
26
     *
27
     * @return mixed
28
     */
29
    protected function policy()
30
    {
31
        return $this->gate->getPolicyFor($this->instance);
32
    }
33
34
    /**
35
     * Execute a callable in a database transaction and inform the model about that.
36
     *
37
     * @param callable $callback
38
     * @return mixed
39
     */
40
    public function transaction(callable $callback)
41
    {
42
        if (method_exists($this->getInstance(), 'startTransaction')) {
0 ignored issues
show
It seems like getInstance() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

42
        if (method_exists($this->/** @scrutinizer ignore-call */ getInstance(), 'startTransaction')) {
Loading history...
43
            $this->getInstance()->startTransaction();
44
        }
45
46
        $result = DB::transaction($callback);
47
48
        if (method_exists($this->getInstance(), 'endTransaction')) {
49
            $this->getInstance()->endTransaction();
50
        }
51
52
        return $result;
53
    }
54
55
    /**
56
     * Create a new instance with GraphQL input.
57
     *
58
     * @param array $input
59
     * @return \Illuminate\Database\Eloquent\Model
60
     */
61
    public function create(array $input = []): Model
62
    {
63
        return $this->transaction(function () use ($input) {
64
            $this->make($input);
65
            $this->save();
66
67
            return $this->instance;
68
        });
69
    }
70
71
    /**
72
     * Create a model after checking if the user is authorised to do so.
73
     *
74
     * @param array $input
75
     * @return \Illuminate\Database\Eloquent\Model
76
     */
77
    public function createIfAuthorized(array $input = []): Model
78
    {
79
        return $this->transaction(function () use ($input) {
80
            $this->create($input);
81
            $this->authorizeToCreate();
0 ignored issues
show
It seems like authorizeToCreate() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

81
            $this->/** @scrutinizer ignore-call */ 
82
                   authorizeToCreate();
Loading history...
82
83
            return $this->instance;
84
        });
85
    }
86
87
    /**
88
     * @param array $input
89
     * @return \Illuminate\Database\Eloquent\Model
90
     * @throws \Illuminate\Auth\Access\AuthorizationException
91
     */
92
    public function make(array $input = []): Model
93
    {
94
        $this->instance = $this->instance->newInstance();
95
        $this->fill($input);
96
97
        return $this->instance;
98
    }
99
100
    /**
101
     * Update the model with GraphQL input.
102
     *
103
     * @param array $input
104
     * @return \Illuminate\Database\Eloquent\Model
105
     */
106
    public function update(array $input = []): Model
107
    {
108
        return $this->transaction(function () use ($input) {
109
            $this->fill($input);
110
            $this->save();
111
112
            return $this->instance;
113
        });
114
    }
115
116
    /**
117
     * Update a model after checking if the user is authorised to do so.
118
     *
119
     * @param array $input
120
     * @return \Illuminate\Database\Eloquent\Model
121
     */
122
    public function updateIfAuthorized(array $input = []): Model
123
    {
124
        $this->authorizeToUpdate();
0 ignored issues
show
It seems like authorizeToUpdate() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

124
        $this->/** @scrutinizer ignore-call */ 
125
               authorizeToUpdate();
Loading history...
125
        $this->update($input);
126
127
        return $this->instance;
128
    }
129
130
    /**
131
     * Fill the underlying model with input.
132
     *
133
     * @param array $input
134
     * @return $this
135
     * @throws \Illuminate\Auth\Access\AuthorizationException
136
     */
137
    public function fill(array $input = [])
138
    {
139
        $scalars = $this->getFillableScalars($input);
140
        $relations = $this->getFillableRelations($input);
141
        $connections = $this->getFillableConnections($input);
142
143
        $this->fillScalars($scalars);
144
        $this->fillRelations($relations);
145
        $this->fillConnections($connections);
146
147
        $this->checkScalars($scalars);
148
149
        return $this;
150
    }
151
152
    /**
153
     * Save the underlying model.
154
     *
155
     * @return $this
156
     */
157
    public function save(): self
158
    {
159
        $this->instance->save();
160
161
        $this->persistQueuedDatabaseTransactions();
162
163
        return $this;
164
    }
165
166
    /**
167
     * Get the attributes that are mass assignable by cross
168
     * referencing the attributes with the GraphQL fields.
169
     *
170
     * @param array $attributes
171
     * @return array
172
     */
173
    protected function getFillableScalars(array $attributes): array
174
    {
175
        $fields = $this->getFillableFields();
0 ignored issues
show
The method getFillableFields() does not exist on Bakery\Eloquent\Concerns\MutatesModel. Did you maybe mean getFillableRelations()? ( Ignorable by Annotation )

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

175
        /** @scrutinizer ignore-call */ 
176
        $fields = $this->getFillableFields();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
176
177
        return collect($attributes)->intersectByKeys($fields)->toArray();
178
    }
179
180
    /**
181
     * Get the relations that are assignable by cross referencing
182
     * the attributes with the GraphQL relations.
183
     *
184
     * @param array $attributes
185
     * @return array
186
     */
187
    protected function getFillableRelations(array $attributes): array
188
    {
189
        $relations = $this->getRelationFields();
0 ignored issues
show
The method getRelationFields() does not exist on Bakery\Eloquent\Concerns\MutatesModel. Did you maybe mean getRelationTypeName()? ( Ignorable by Annotation )

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

189
        /** @scrutinizer ignore-call */ 
190
        $relations = $this->getRelationFields();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
190
191
        return collect($attributes)->intersectByKeys($relations)->toArray();
192
    }
193
194
    /**
195
     * Get the relations that are assignable by cross referencing
196
     * the attributes with the GraphQL connections.
197
     *
198
     * @param array $attributes
199
     * @return array
200
     */
201
    protected function getFillableConnections(array $attributes): array
202
    {
203
        $connections = $this->getConnections();
0 ignored issues
show
It seems like getConnections() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

203
        /** @scrutinizer ignore-call */ 
204
        $connections = $this->getConnections();
Loading history...
204
205
        return collect($attributes)->intersectByKeys($connections->flip())->toArray();
206
    }
207
}
208