Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Push — crud-uploads ( c657db...bf972d )
by Pedro
13:12
created

CrudColumn::upload()   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 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Library\CrudPanel;
4
5
use Backpack\CRUD\app\Library\CrudPanel\Traits\Support\HasMacros;
6
use Illuminate\Support\Traits\Macroable;
7
8
/**
9
 * Adds fluent syntax to Backpack CRUD Columns.
10
 *
11
 * In addition to the existing:
12
 * - CRUD::addColumn(['name' => 'price', 'type' => 'number']);
13
 *
14
 * Developers can also do:
15
 * - CRUD::column('price')->type('number');
16
 *
17
 * And if the developer uses CrudColumn as Column in their CrudController:
18
 * - Column::name('price')->type('number');
19
 *
20
 * @method self type(string $value)
21
 * @method self label(string $value)
22
 * @method self searchLogic(mixed $value)
23
 * @method self orderLogic(callable $value)
24
 * @method self orderable(bool $value)
25
 * @method self wrapper(array $value)
26
 * @method self visibleInTable(bool $value)
27
 * @method self visibleInModal(bool $value)
28
 * @method self visibleInExport(bool $value)
29
 * @method self visibleInShow(bool $value)
30
 * @method self priority(int $value)
31
 * @method self key(string $value)
32
 * @method self upload(bool $value)
33
 */
34
class CrudColumn
35
{
36
    use Macroable { __call as macroCall; }
0 ignored issues
show
Bug introduced by
The trait Illuminate\Support\Traits\Macroable requires the property $name which is not provided by Backpack\CRUD\app\Library\CrudPanel\CrudColumn.
Loading history...
37
    use HasMacros;
38
39
    protected $attributes;
40
41
    public function upload($upload = true)
42
    {
43
        $this->attributes['upload'] = $upload;
44
45
        return $this->save();
46
    }
47
48
49
    public function __construct($name)
50
    {
51
        $column = $this->crud()->firstColumnWhere('name', $name);
52
53
        // if column exists
54
        if ((bool) $column) {
55
            // use all existing attributes
56
            $this->setAllAttributeValues($column);
0 ignored issues
show
Bug introduced by
$column of type boolean is incompatible with the type array expected by parameter $array of Backpack\CRUD\app\Librar...setAllAttributeValues(). ( Ignorable by Annotation )

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

56
            $this->setAllAttributeValues(/** @scrutinizer ignore-type */ $column);
Loading history...
57
        } else {
58
            // it means we're creating the column now,
59
            // so at the very least set the name attribute
60
            $this->setAttributeValue('name', $name);
61
        }
62
63
        // guess all attributes that weren't explicitly defined
64
        $this->attributes = $this->crud()->makeSureColumnHasNeededAttributes($this->attributes);
65
66
        $this->save();
67
    }
68
69
    public function crud()
70
    {
71
        return app()->make('crud');
72
    }
73
74
    /**
75
     * Create a CrudColumn object with the parameter as its name.
76
     *
77
     * @param  string  $name  Name of the column in the db, or model attribute.
78
     * @return CrudColumn
79
     */
80
    public static function name($name)
81
    {
82
        return new static($name);
83
    }
84
85
    /**
86
     * Change the CrudColumn key.
87
     *
88
     * @param  string  $key  New key for the column
89
     * @return CrudColumn
90
     */
91
    public function key(string $key)
92
    {
93
        if (! isset($this->attributes['name'])) {
94
            abort(500, 'Column name must be defined before changing the key.');
95
        }
96
97
        $columns = $this->crud()->columns();
98
99
        $searchKey = $this->attributes['key'];
100
        $column = $this->attributes;
101
102
        if (isset($columns[$searchKey])) {
103
            unset($columns[$searchKey]);
104
            $column['key'] = $key;
105
        }
106
        $this->attributes = $column;
107
        $this->crud()->setOperationSetting('columns', array_merge($columns, [$key => $column]));
0 ignored issues
show
Bug introduced by
array_merge($columns, array($key => $column)) of type array is incompatible with the type boolean expected by parameter $value of Backpack\CRUD\app\Librar...::setOperationSetting(). ( Ignorable by Annotation )

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

107
        $this->crud()->setOperationSetting('columns', /** @scrutinizer ignore-type */ array_merge($columns, [$key => $column]));
Loading history...
108
109
        return $this;
110
    }
111
112
    /**
113
     * Remove the current column from the current operation.
114
     *
115
     * @return void
116
     */
117
    public function remove()
118
    {
119
        $this->crud()->removeColumn($this->attributes['name']);
120
    }
121
122
    /**
123
     * Remove an attribute from the column definition array.
124
     *
125
     * @param  string  $attribute  Name of the attribute being removed
126
     * @return CrudColumn
127
     */
128
    public function forget($attribute)
129
    {
130
        $this->crud()->removeColumnAttribute($this->attributes['name'], $attribute);
131
132
        return $this;
133
    }
134
135
    /**
136
     * Move the current column after another column.
137
     *
138
     * @param  string  $destinationColumn  Name of the destination column.
139
     * @return CrudColumn
140
     */
141
    public function after($destinationColumn)
142
    {
143
        $this->crud()->removeColumn($this->attributes['name']);
144
        $this->crud()->addColumn($this->attributes)->afterColumn($destinationColumn);
145
146
        return $this;
147
    }
148
149
    /**
150
     * Move the current column before another column.
151
     *
152
     * @param  string  $destinationColumn  Name of the destination column.
153
     * @return CrudColumn
154
     */
155
    public function before($destinationColumn)
156
    {
157
        $this->crud()->removeColumn($this->attributes['name']);
158
        $this->crud()->addColumn($this->attributes)->beforeColumn($destinationColumn);
159
160
        return $this;
161
    }
162
163
    /**
164
     * Make the current column the first one in the columns list.
165
     *
166
     * @return CrudColumn
167
     */
168
    public function makeFirst()
169
    {
170
        $this->crud()->removeColumn($this->attributes['name']);
171
        $this->crud()->addColumn($this->attributes)->makeFirstColumn();
172
173
        return $this;
174
    }
175
176
    /**
177
     * Make the current column the last one in the columns list.
178
     *
179
     * @return CrudColumn
180
     */
181
    public function makeLast()
182
    {
183
        $this->crud()->removeColumn($this->attributes['name']);
184
        $this->crud()->addColumn($this->attributes);
185
186
        return $this;
187
    }
188
189
    // -----------------
190
    // DEBUGGING METHODS
191
    // -----------------
192
193
    /**
194
     * Dump the current object to the screen,
195
     * so that the developer can see its contents.
196
     *
197
     * @codeCoverageIgnore
198
     *
199
     * @return CrudColumn
200
     */
201
    public function dump()
202
    {
203
        dump($this);
204
205
        return $this;
206
    }
207
208
    /**
209
     * Dump and die. Duumps the current object to the screen,
210
     * so that the developer can see its contents, then stops
211
     * the execution.
212
     *
213
     * @codeCoverageIgnore
214
     *
215
     * @return CrudColumn
216
     */
217
    public function dd()
218
    {
219
        dd($this);
220
221
        return $this;
222
    }
223
224
    public function getAttributes()
225
    {
226
        return $this->attributes;
227
    }
228
229
    // ---------------
230
    // PRIVATE METHODS
231
    // ---------------
232
233
    /**
234
     * Set the value for a certain attribute on the CrudColumn object.
235
     *
236
     * @param  string  $attribute  Name of the attribute.
237
     * @param  mixed  $value  Value of that attribute.
238
     */
239
    private function setAttributeValue($attribute, $value)
240
    {
241
        $this->attributes[$attribute] = $value;
242
    }
243
244
    /**
245
     * Replace all column attributes on the CrudColumn object
246
     * with the given array of attribute-value pairs.
247
     *
248
     * @param  array  $array  Array of attributes and their values.
249
     */
250
    private function setAllAttributeValues($array)
251
    {
252
        $this->attributes = $array;
253
    }
254
255
    /**
256
     * Update the global CrudPanel object with the current column attributes.
257
     *
258
     * @return CrudColumn
259
     */
260
    private function save()
261
    {
262
        $key = $this->attributes['key'] ?? $this->attributes['name'];
263
264
        if ($this->crud()->hasColumnWhere('key', $key)) {
265
            $this->crud()->setColumnDetails($key, $this->attributes);
266
        } else {
267
            $this->crud()->addColumn($this->attributes);
268
        }
269
270
        return $this;
271
    }
272
273
    // -------------
274
    // MAGIC METHODS
275
    // -------------
276
277
    /**
278
     * If a developer calls a method that doesn't exist, assume they want:
279
     * - the CrudColumn object to have an attribute with that value;
280
     * - that column be updated inside the global CrudPanel object;.
281
     *
282
     * Eg: type('number') will set the "type" attribute to "number"
283
     *
284
     * @param  string  $method  The method being called that doesn't exist.
285
     * @param  array  $parameters  The arguments when that method was called.
286
     * @return CrudColumn
287
     */
288
    public function __call($method, $parameters)
289
    {
290
        if (static::hasMacro($method)) {
291
            return $this->macroCall($method, $parameters);
0 ignored issues
show
Bug introduced by
The method macroCall() does not exist on Backpack\CRUD\app\Library\CrudPanel\CrudColumn. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

291
            return $this->/** @scrutinizer ignore-call */ macroCall($method, $parameters);
Loading history...
292
        }
293
294
        $this->setAttributeValue($method, $parameters[0]);
295
296
        return $this->save();
297
    }
298
}
299