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
Branch development (1ea943)
by butschster
05:38
created

Display::setView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 9.4285
1
<?php
2
3
namespace SleepingOwl\Admin\Display;
4
5
use Illuminate\Support\Collection;
6
use SleepingOwl\Admin\Traits\Assets;
7
use KodiComponents\Support\HtmlAttributes;
8
use SleepingOwl\Admin\Contracts\Initializable;
9
use SleepingOwl\Admin\Display\Extension\Apply;
10
use SleepingOwl\Admin\Display\Extension\Scopes;
11
use SleepingOwl\Admin\Contracts\ActionInterface;
12
use SleepingOwl\Admin\Contracts\FilterInterface;
13
use SleepingOwl\Admin\Display\Extension\Actions;
14
use SleepingOwl\Admin\Display\Extension\Filters;
15
use SleepingOwl\Admin\Contracts\Display\Placable;
16
use SleepingOwl\Admin\Contracts\DisplayInterface;
17
use SleepingOwl\Admin\Contracts\RepositoryInterface;
18
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface;
19
use SleepingOwl\Admin\Contracts\Display\DisplayExtensionInterface;
20
use SleepingOwl\Admin\Traits\Renderable;
21
22
/**
23
 * Class Display.
24
 *
25
 * @method Actions getActions()
26
 * @method $this setActions(ActionInterface $action, ...$actions)
27
 *
28
 * @method Filters getFilters()
29
 * @method $this setFilters(FilterInterface $filter, ...$filters)
30
 *
31
 * @method Apply getApply()
32
 * @method $this setApply(\Closure $apply, ...$applies)
33
 *
34
 * @method Scopes getScopes()
35
 * @method $this setScopes(array $scope, ...$scopes)
36
 */
37
abstract class Display implements DisplayInterface
38
{
39
    use HtmlAttributes, Assets, Renderable;
40
41
    /**
42
     * @var string
43
     */
44
    protected $modelClass;
45
46
    /**
47
     * @var array
48
     */
49
    protected $with = [];
50
51
    /**
52
     * @var string
53
     */
54
    protected $title;
55
56
    /**
57
     * @var string
58
     */
59
    protected $repositoryClass = RepositoryInterface::class;
60
61
    /**
62
     * @var RepositoryInterface
63
     */
64
    protected $repository;
65
66
    /**
67
     * @var DisplayExtensionInterface[]|Collection
68
     */
69
    protected $extensions;
70
71
    /**
72
     * @var bool
73
     */
74
    protected $initialized = false;
75
76
    /**
77
     * Display constructor.
78
     */
79
    public function __construct()
80
    {
81
        $this->extensions = new Collection();
82
83
        $this->extend('actions', new Actions());
84
        $this->extend('filters', new Filters());
85
        $this->extend('apply', new Apply());
86
        $this->extend('scopes', new Scopes());
87
88
        $this->initializePackage();
89
    }
90
91
    /**
92
     * @return bool
93
     */
94
    public function isInitialized()
95
    {
96
        return $this->initialized;
97
    }
98
99
    /**
100
     * @param string                    $name
101
     * @param DisplayExtensionInterface $extension
102
     *
103
     * @return DisplayExtensionInterface
104
     */
105
    public function extend($name, DisplayExtensionInterface $extension)
106
    {
107
        $this->extensions->put($name, $extension);
108
109
        $extension->setDisplay($this);
110
111
        return $extension;
112
    }
113
114
    /**
115
     * @return Collection|\SleepingOwl\Admin\Contracts\Display\DisplayExtensionInterface[]
116
     */
117
    public function getExtensions()
118
    {
119
        return $this->extensions;
120
    }
121
122
    /**
123
     * @return RepositoryInterface
124
     */
125
    public function getRepository()
126
    {
127
        return $this->repository;
128
    }
129
130
    /**
131
     * @param $repositoryClass
132
     *
133
     * @return $this
134
     */
135
    public function setRepositoryClass($repositoryClass)
136
    {
137
        $this->repositoryClass = $repositoryClass;
138
139
        return $this;
140
    }
141
142
    /**
143
     * @param array|string[] ...$relations
144
     *
145
     * @return $this
146
     */
147
    public function with($relations)
0 ignored issues
show
Unused Code introduced by
The parameter $relations is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
148
    {
149
        $this->with = array_flatten(func_get_args());
150
151
        return $this;
152
    }
153
154
    /**
155
     * @return void
156
     */
157
    public function initialize()
158
    {
159
        if ($this->isInitialized()) {
160
            return;
161
        }
162
163
        $this->repository = $this->makeRepository();
164
        $this->repository->with($this->with);
165
166
        $this->extensions->each(function (DisplayExtensionInterface $extension) {
167
            if ($extension instanceof Initializable) {
168
                $extension->initialize();
169
            }
170
        });
171
172
        $this->includePackage();
173
174
        $this->initialized = true;
175
    }
176
177
    /**
178
     * @param string $modelClass
179
     *
180
     * @return $this
181
     */
182
    public function setModelClass($modelClass)
183
    {
184
        if (is_null($this->modelClass)) {
185
            $this->modelClass = $modelClass;
186
        }
187
188
        return $this;
189
    }
190
191
    /**
192
     * @return string
193
     */
194
    public function getTitle()
195
    {
196
        $titles = [
197
            $this->title,
198
        ];
199
200
        $this->getExtensions()->each(function (DisplayExtensionInterface $extension) use (&$titles) {
201
            if (method_exists($extension, $method = 'getTitle')) {
202
                $titles[] = call_user_func([$extension, $method]);
203
            }
204
        });
205
206
        return implode(' | ', array_filter($titles));
207
    }
208
209
    /**
210
     * @param string $title
211
     *
212
     * @return $this
213
     */
214
    public function setTitle($title)
215
    {
216
        $this->title = $title;
217
218
        return $this;
219
    }
220
221
    /**
222
     * Get the instance as an array.
223
     *
224
     * @return array
225
     */
226
    public function toArray()
227
    {
228
        return [
229
            'title' => $this->getTitle(),
230
            'extensions' => $this->getExtensions()->toArray(),
231
            'attributes' => $this->htmlAttributesToString(),
232
        ];
233
    }
234
235
    /**
236
     * Get the evaluated contents of the object.
237
     *
238
     * @return string
239
     */
240
    public function render()
241
    {
242
        $view = app('sleeping_owl.template')->view($this->getView(), $this->toArray());
243
244
        $blocks = [];
245
246
        $placableExtensions = $this->getExtensions()->filter(function ($extension) {
247
            return $extension instanceof Placable;
248
        });
249
250
        foreach ($placableExtensions as $extension) {
251
            $blocks[$extension->getPlacement()][] = (string) app('sleeping_owl.template')->view(
252
                $extension->getView(),
253
                $extension->toArray()
254
            );
255
        }
256
257
        foreach ($blocks as $block => $data) {
258
            foreach ($data as $html) {
259
                if (! empty($html)) {
260
                    $view->getFactory()->startSection($block);
261
                    echo $html;
262
                    $view->getFactory()->yieldSection();
263
                }
264
            }
265
        }
266
267
        return $view;
268
    }
269
270
    /**
271
     * @param string $name
272
     * @param array  $arguments
273
     *
274
     * @return DisplayExtensionInterface
275
     */
276
    public function __call($name, $arguments)
277
    {
278
        $method = snake_case(substr($name, 3));
279
280
        if (starts_with($name, 'get') and $this->extensions->has($method)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
281
            return $this->extensions->get($method);
282
        } elseif (starts_with($name, 'set') and $this->extensions->has($method)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
283
            $extension = $this->extensions->get($method);
284
285
            if (method_exists($extension, 'set')) {
286
                return call_user_func_array([$extension, 'set'], $arguments);
287
            }
288
        }
289
290
        throw new \BadMethodCallException("Call to undefined method [{$name}]");
291
    }
292
293
    /**
294
     * @return ModelConfigurationInterface
295
     */
296
    protected function getModelConfiguration()
297
    {
298
        return app('sleeping_owl')->getModel($this->modelClass);
299
    }
300
301
    /**
302
     * @return \Illuminate\Foundation\Application|mixed
303
     * @throws \Exception
304
     */
305
    protected function makeRepository()
306
    {
307
        $repository = app($this->repositoryClass, [$this->modelClass]);
308
309
        if (! ($repository instanceof RepositoryInterface)) {
310
            throw new \Exception('Repository class must be instanced of [RepositoryInterface]');
311
        }
312
313
        return $repository;
314
    }
315
}
316