Issues (197)

src/Commands/ControllerCommand.php (2 issues)

1
<?php
2
namespace Salah3id\Domains\Commands;
3
4
use Illuminate\Console\Command;
5
use Illuminate\Support\Collection;
6
use Salah3id\Domains\Repository\Generators\ControllerGenerator;
7
use Salah3id\Domains\Repository\Generators\FileAlreadyExistsException;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputOption;
10
use Salah3id\Domains\Traits\DomainCommandTrait;
11
12
/**
13
 * Class ControllerCommand
14
 * @package Salah3id\Domains\Commands
15
 * @author Anderson Andrade <[email protected]>
16
 */
17
class ControllerCommand extends Command
18
{
19
20
    use DomainCommandTrait;
21
22
    /**
23
     * The name of command.
24
     *
25
     * @var string
26
     */
27
    protected $name = 'domain:resource';
28
29
    /**
30
     * The description of command.
31
     *
32
     * @var string
33
     */
34
    protected $description = 'Create a new RESTful controller.';
35
36
    /**
37
     * The type of class being generated.
38
     *
39
     * @var string
40
     */
41
    protected $type = 'Controller';
42
43
    /**
44
     * ControllerCommand constructor.
45
     */
46
    public function __construct()
47
    {
48
        $this->name = ((float) app()->version() >= 5.5  ? 'make:rest-controller' : 'make:resource');
0 ignored issues
show
The method version() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        $this->name = ((float) app()->/** @scrutinizer ignore-call */ version() >= 5.5  ? 'make:rest-controller' : 'make:resource');
Loading history...
49
        parent::__construct();
50
    }
51
52
    /**
53
     * Execute the command.
54
     *
55
     * @see fire()
56
     * @return void
57
     */
58
    public function handle(){
59
        $this->laravel->call([$this, 'fire'], func_get_args());
60
    }
61
62
    /**
63
     * Execute the command.
64
     *
65
     * @return void
66
     */
67
    public function fire()
68
    {
69
        if($this->argument('domain') && $this->argument('domain-path')) {
70
            $domain = $this->argument('domain');
71
            $domainPath = $this->argument('domain-path');
72
            
73
        } else {
74
            $domain = $this->getDomainNameForRepo();
75
            $domainPath = $this->laravel['domains']->getDomainPath($domain);
76
        }
77
        try {
78
            $this->call('domain:make-request', [
79
                'name' => 'CreateRequests\\' . ucfirst($this->argument('name')) . 'CreateRequest',
80
                'domain' => $domain
81
            ]);
82
83
            // Generate update request for controller
84
            $this->call('domain:make-request', [
85
                'name' => 'UpdateRequests\\' . ucfirst($this->argument('name')) . 'UpdateRequest',
86
                'domain' => $domain
87
            ]);
88
89
            (new ControllerGenerator([
90
                'name' => $this->argument('name'),
91
                'force' => $this->option('force'),
92
            ],$domain,$domainPath))->run();
93
94
            $this->info($this->type . ' created successfully.');
95
96
        } catch (FileAlreadyExistsException $e) {
97
            $this->error($this->type . ' already exists!');
98
99
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type void.
Loading history...
100
        }
101
    }
102
103
104
    /**
105
     * The array of command arguments.
106
     *
107
     * @return array
108
     */
109
    public function getArguments()
110
    {
111
        return [
112
            [
113
                'name',
114
                InputArgument::REQUIRED,
115
                'The name of model for which the controller is being generated.',
116
                null
117
            ],
118
            ['domain', InputArgument::OPTIONAL, 'The name of domain will be used.'],
119
            ['domain-path', InputArgument::OPTIONAL, 'The path of domain will be used.'],
120
        ];
121
    }
122
123
124
    /**
125
     * The array of command options.
126
     *
127
     * @return array
128
     */
129
    public function getOptions()
130
    {
131
        return [
132
            [
133
                'force',
134
                'f',
135
                InputOption::VALUE_NONE,
136
                'Force the creation if file already exists.',
137
                null
138
            ],
139
        ];
140
    }
141
}
142