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.

CrudManager::setEntry()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Akibatech\Crud\Services;
4
5
use Akibatech\Crud\Exceptions\InvalidRouteIdentifierException;
6
use Illuminate\Routing\Router;
7
use Illuminate\Support\Facades\Route;
8
9
/**
10
 * Class CrudManager
11
 *
12
 * @package Akibatech\Crud\Services
13
 */
14
class CrudManager
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $name = 'Entry';
20
21
    /**
22
     * @var string
23
     */
24
    protected $pluralized_name = 'Entries';
25
26
    /**
27
     * @var null|CrudEntry
28
     */
29
    protected $entry;
30
31
    /**
32
     * @var string
33
     */
34
    protected $route_uri_prefix = 'crud/';
35
36
    /**
37
     * @var string
38
     */
39
    protected $route_name_prefix = 'crud.';
40
41
    /**
42
     * @var string
43
     */
44
    protected $controller;
45
46
    /**
47
     * @var int
48
     */
49
    protected $per_page = 25;
50
51
    /**
52
     * @var array
53
     */
54
    protected $routes = [
55
        'index'   => [
56
            'method' => 'GET',
57
            'uri'    => 'index',
58
            'as'     => 'index',
59
            'action' => 'index'
60
        ],
61
        'create'  => [
62
            'method' => 'GET',
63
            'uri'    => 'create',
64
            'as'     => 'create',
65
            'action' => 'create'
66
        ],
67
        'store'   => [
68
            'method' => 'POST',
69
            'uri'    => 'store',
70
            'as'     => 'store',
71
            'action' => 'store'
72
        ],
73
        'edit'    => [
74
            'method' => 'GET',
75
            'uri'    => 'edit/{id}',
76
            'as'     => 'edit',
77
            'action' => 'edit'
78
        ],
79
        'update'  => [
80
            'method' => 'PUT',
81
            'uri'    => 'update/{id}',
82
            'as'     => 'update',
83
            'action' => 'update'
84
        ],
85
        'destroy' => [
86
            'method' => 'GET',
87
            'uri'    => 'destroy/{id}/{csrf}',
88
            'as'     => 'destroy',
89
            'action' => 'destroy'
90
        ],
91
    ];
92
93
    /**
94
     * CrudManager constructor.
95
     *
96
     * @param   mixed $model
97
     */
98
    public function __construct($model = null)
99
    {
100
        if (!is_null($model))
101
        {
102
            $name = $this->normalizeClassName($model);
103
            $pluralized = str_plural($name);
104
            $lowered = strtolower($pluralized);
105
106
            $this->setController($pluralized . 'Controller')
107
                ->setName($name)
108
                ->setNamePrefix($lowered)
109
                ->setUriPrefix($lowered);
110
        }
111
    }
112
113
    /**
114
     * Make staticly a new instance.
115
     *
116
     * @param   void
117
     * @return  CrudManager
118
     */
119
    public static function make($model = null)
120
    {
121
        return new static($model);
122
    }
123
124
    /**
125
     * @param   void
126
     * @return  string
127
     */
128
    public function getName()
129
    {
130
        return $this->name;
131
    }
132
133
    /**
134
     * @param   string      $name
135
     * @param   null|string $pluralized_name
136
     * @return  self
137
     */
138
    public function setName($name, $pluralized_name = null)
139
    {
140
        $this->name = $name;
141
        $this->pluralized_name = is_null($pluralized_name) ? str_plural($name) : $pluralized_name;
142
143
        return $this;
144
    }
145
146
    /**
147
     * @param   void
148
     * @return  string
149
     */
150
    public function getPluralizedName()
151
    {
152
        return $this->pluralized_name;
153
    }
154
155
    /**
156
     * @param   string $prefix
157
     * @return  self
158
     */
159
    public function setUriPrefix($prefix)
160
    {
161
        $this->route_uri_prefix = rtrim($prefix, "\r\n\r\0\x0B\\/") . '/';
162
163
        return $this;
164
    }
165
166
    /**
167
     * @param   void
168
     * @return  string
169
     */
170
    public function getUriPrefix()
171
    {
172
        return $this->route_uri_prefix;
173
    }
174
175
    /**
176
     * @param   string $prefix
177
     * @return  self
178
     */
179
    public function setNamePrefix($prefix)
180
    {
181
        $this->route_name_prefix = rtrim($prefix, "\r\n\r\0\x0B\\/.") . '.';
182
183
        return $this;
184
    }
185
186
    /**
187
     * @param   void
188
     * @return  string
189
     */
190
    public function getNamePrefix()
191
    {
192
        return $this->route_name_prefix;
193
    }
194
195
    /**
196
     * @param   void
197
     * @return  string
198
     */
199
    public function getController()
200
    {
201
        return $this->controller;
202
    }
203
204
    /**
205
     * @param   string $controller
206
     * @return  self
207
     */
208
    public function setController($controller)
209
    {
210
        $this->controller = $controller;
211
212
        return $this;
213
    }
214
215
    /**
216
     * @param   string $identifier
217
     * @return  string
218
     * @throws  InvalidRouteIdentifierException
219
     */
220
    public function getActionRoute($identifier)
221
    {
222
        if (array_key_exists($identifier, $this->routes))
223
        {
224
            $route = $this->routes[$identifier];
225
            $name = $this->route_name_prefix . $route['as'];
226
            $params = [];
227
228
            if ($identifier == 'destroy')
229
            {
230
                $params = [
231
                    $this->entry->getId(),
232
                    csrf_token()
233
                ];
234
            }
235
236
            if ($identifier == 'update' || $identifier == 'edit')
237
            {
238
                $params = [$this->entry->getId()];
239
            }
240
241
            if (Route::has($name) === false)
242
            {
243
                return '#';
244
            }
245
246
            return empty($params) ? route($name) : route($name, $params);
247
        }
248
249
        throw new InvalidRouteIdentifierException("$identifier route identifier not found.");
250
    }
251
252
    /**
253
     * @param   string $identifier
254
     * @return  string
255
     * @throws  InvalidRouteIdentifierException
256
     */
257
    public function getActionMethod($identifier)
258
    {
259
        if (array_key_exists($identifier, $this->routes))
260
        {
261
            $route = $this->routes[$identifier];
262
            return $route['method'];
263
        }
264
265
        throw new InvalidRouteIdentifierException("$identifier route identifier not found.");
266
    }
267
268
    /**
269
     * @param   void
270
     * @return  self
271
     */
272
    public function registerRoutes()
273
    {
274
        $routes = $this->routes;
275
        $controller = $this->controller;
276
277
        Route::group([
278
            'prefix'     => $this->route_uri_prefix,
279
            'as'         => $this->route_name_prefix,
280
            'middleware' => 'web'
281
        ], function (Router $router) use ($routes, $controller)
282
        {
283
            foreach ($routes as $route)
284
            {
285
                $action = $controller . '@' . $route['action'];
286
287
                $router->{$route['method']}($route['uri'], [
288
                    'as'   => $route['as'],
289
                    'uses' => $action
290
                ]);
291
            }
292
        });
293
294
        return $this;
295
    }
296
297
    /**
298
     * @param   void
299
     * @return  CrudEntry|null
300
     */
301
    public function getEntry()
302
    {
303
        return $this->entry;
304
    }
305
306
    /**
307
     * @param   CrudEntry $entry
308
     * @return  self
309
     */
310
    public function setEntry(CrudEntry $entry)
311
    {
312
        $this->entry = $entry;
313
314
        return $this;
315
    }
316
317
    /**
318
     * @param   int $per_page
319
     * @return  self
320
     */
321
    public function setPerPage($per_page = 25)
322
    {
323
        $this->per_page = (int)$per_page;
324
325
        return $this;
326
    }
327
328
    /**
329
     * @param   void
330
     * @return  int
331
     */
332
    public function getPerPage()
333
    {
334
        return $this->per_page;
335
    }
336
337
    /**
338
     * @param   mixed $model
339
     * @return  string
340
     */
341
    protected function normalizeClassName($model)
342
    {
343
        $class = is_string($model) ? $model : get_class($model);
344
        $name = stripos($class, '\\') !== false ? substr(strrchr($class, '\\'), 1) : $class;
345
346
        return $name;
347
    }
348
}
349