Completed
Push — master ( 014c61...73e470 )
by Amine
11s
created

RouteCommand::handle()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 50
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 8.6315
c 0
b 0
f 0
cc 5
eloc 21
nc 8
nop 0
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(str_plural(camel_case($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, camel_case() 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