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 ( 413ca6...9053fe )
by Marceau
03:52
created

CrudManager::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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