1 | <?php |
||||
2 | |||||
3 | namespace VGirol\JsonApi\Commands; |
||||
4 | |||||
5 | use Illuminate\Console\Command; |
||||
6 | use Symfony\Component\Console\Input\InputArgument; |
||||
7 | use Symfony\Component\Console\Input\InputOption; |
||||
8 | |||||
9 | class MakeResourcesCommand extends Command |
||||
10 | { |
||||
11 | use InteractsWithResults; |
||||
12 | use FixResolveCommand; |
||||
13 | |||||
14 | /** |
||||
15 | * Undocumented variable |
||||
16 | * |
||||
17 | * @var array |
||||
18 | */ |
||||
19 | private $results = []; |
||||
20 | |||||
21 | /** |
||||
22 | * The console command name. |
||||
23 | * |
||||
24 | * @var string |
||||
25 | */ |
||||
26 | protected $name = 'make:jsonapi:resources'; |
||||
27 | |||||
28 | /** |
||||
29 | * The console command description. |
||||
30 | * |
||||
31 | * @var string |
||||
32 | */ |
||||
33 | protected $description = 'Create new JsonApi resources'; |
||||
34 | |||||
35 | /** |
||||
36 | * {@inheritDoc} |
||||
37 | */ |
||||
38 | public function collectResults(): array |
||||
39 | { |
||||
40 | return $this->results; |
||||
41 | } |
||||
42 | |||||
43 | /** |
||||
44 | * Get the console command arguments. |
||||
45 | * |
||||
46 | * @return array |
||||
47 | */ |
||||
48 | protected function getArguments() |
||||
49 | { |
||||
50 | return [ |
||||
51 | ['name', InputArgument::REQUIRED, 'The name of the class'], |
||||
52 | ]; |
||||
53 | } |
||||
54 | |||||
55 | /** |
||||
56 | * {@inheritDoc} |
||||
57 | */ |
||||
58 | protected function getOptions(): array |
||||
59 | { |
||||
60 | return [ |
||||
61 | ['types', 't', InputOption::VALUE_REQUIRED, 'Comma separated list of the resource classes that will be created : ro, ri, roc, ric.'], |
||||
62 | ]; |
||||
63 | } |
||||
64 | |||||
65 | /** |
||||
66 | * Execute the console command. |
||||
67 | * |
||||
68 | * @return void |
||||
69 | */ |
||||
70 | public function handle() |
||||
71 | { |
||||
72 | $all = ['ro', 'ri', 'roc', 'ric']; |
||||
73 | $types = ($this->option('types') === null) ? $all : explode(',', $this->option('types')); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
74 | |||||
75 | if (!empty($diff = \array_diff($types, $all))) { |
||||
76 | $this->warn('The following types are not allowed : "' . implode('", "', $diff) . '".'); |
||||
77 | |||||
78 | return 1; |
||||
79 | } |
||||
80 | |||||
81 | foreach ($types as $type) { |
||||
82 | $parameters = [ |
||||
83 | 'name' => $this->getClassName($type), |
||||
84 | '--ro' => $this->isRO($type), |
||||
85 | '--ri' => $this->isRI($type), |
||||
86 | '--collection' => $this->isCollection($type) |
||||
87 | ]; |
||||
88 | |||||
89 | $command = $this->resolveCommand(MakeResourceCommand::class); |
||||
90 | $this->call($command, $parameters); |
||||
91 | |||||
92 | $this->results = \array_merge( |
||||
93 | \call_user_func([$command, 'collectResults']), |
||||
94 | $this->results |
||||
95 | ); |
||||
96 | } |
||||
97 | } |
||||
98 | |||||
99 | private function getClassName(string $type): string |
||||
100 | { |
||||
101 | return $this->argument('name') |
||||
0 ignored issues
–
show
Are you sure
$this->argument('name') of type null|string|string[] can be used in concatenation ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
102 | . ($this->isRO($type) ? 'Resource' : 'ResourceIdentifier') |
||||
103 | . ($this->isCollection($type) ? 'Collection' : ''); |
||||
104 | } |
||||
105 | |||||
106 | private function isRO(string $type): bool |
||||
107 | { |
||||
108 | return \in_array($type, ['ro', 'roc']); |
||||
109 | } |
||||
110 | |||||
111 | private function isRI(string $type): bool |
||||
112 | { |
||||
113 | return \in_array($type, ['ri', 'ric']); |
||||
114 | } |
||||
115 | |||||
116 | private function isCollection(string $type): bool |
||||
117 | { |
||||
118 | return \in_array($type, ['roc', 'ric']); |
||||
119 | } |
||||
120 | } |
||||
121 |