RouteCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 71
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 50 5
A getController() 0 8 2
1
<?php namespace Wn\Generators\Commands;
2
3
4
use InvalidArgumentException;
5
6
class RouteCommand extends BaseCommand {
7
8
	protected $signature = 'wn:route
9
		{resource : Name of the resource.}
10
        {--controller= : Name of the RESTful controller.}
11
        {--laravel= : Use Laravel style route definitions}
12
    ';
13
14
	protected $description = 'Generates RESTful routes.';
15
16
    public function handle()
17
    {
18
        $resource = $this->argument('resource');
19
        $laravelRoutes = $this->option('laravel');
20
        $templateFile = 'routes';
21
        $routesPath = 'routes/web.php';
22
        if ($laravelRoutes) {
23
            $templateFile = 'routes-laravel';
24
            $routesPath = 'routes/api.php';
25
            if (!$this->fs->isFile($routesPath)) {
26
                if (!$this->fs->isDirectory('./routes')) {
27
                    $this->fs->makeDirectory('./routes');
28
                }
29
                $this->fs->put($routesPath, "
30
<?php
31
32
    use Illuminate\Http\Request;
33
    
34
    /*
35
    |--------------------------------------------------------------------------
36
    | API Routes
37
    |--------------------------------------------------------------------------
38
    |
39
    | Here is where you can register API routes for your application. These
40
    | routes are loaded by the RouteServiceProvider within a group which
41
    | is assigned the \"api\" middleware group. Enjoy building your API!
42
    |
43
    */
44
    
45
    Route::middleware('auth:api')->get('/user', function (Request \$request) {
46
        return \$request->user();
47
    });
48
49
            ");
50
            }
51
        }
52
53
        if (!$this->fs->isFile($routesPath)) {
54
            $routesPath = 'app/Http/routes.php';
55
        }
56
        $content = $this->fs->get($routesPath);
57
58
        $content .= PHP_EOL . $this->getTemplate($templateFile)
59
                ->with([
60
                    'resource' => $resource,
61
                    'controller' => $this->getController()
62
                ])
63
                ->get();
64
        $this->save($content, $routesPath, "{$resource} routes", true);
65
    }
66
67
    protected function getController()
68
    {
69
        $controller = $this->option('controller');
70
        if(! $controller){
71
            $controller = ucwords(\Illuminate\Support\Str::plural(\Illuminate\Support\Str::camel($this->argument('resource')))) . 'Controller';
0 ignored issues
show
Bug introduced by
It seems like $this->argument('resource') targeting Illuminate\Console\Command::argument() can also be of type array; however, Illuminate\Support\Str::camel() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
72
        }
73
        return $controller;
74
    }
75
76
}
77