Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

GeneratorControllerCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 16
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
A execute() 0 22 2
1
<?php
2
3
namespace Apps\Console;
4
5
6
use Ffcms\Console\Command;
7
use Ffcms\Core\Helper\FileSystem\File;
8
use Ffcms\Core\Helper\Type\Arr;
9
use Ffcms\Core\Helper\Type\Str;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * Class GeneratorControllerCommand. Generate controller carcase
16
 * @package Apps\Console
17
 */
18
class GeneratorControllerCommand extends Command
19
{
20
    /**
21
     * Register command and options
22
     */
23
    public function configure()
24
    {
25
        $this->setName('generator:controller')
26
            ->setDescription('Generate default controller template')
27
            ->addOption('loader', 'loader', InputOption::VALUE_OPTIONAL, 'Set which loader will be used to create controller. Example: front, admin, install')
28
            ->addOption('name', 'name', InputOption::VALUE_OPTIONAL, 'Set new controller name');
29
    }
30
31
    /**
32
     * Execute generator create controller command
33
     * @param InputInterface $input
34
     * @param OutputInterface $output
35
     * @throws \Exception
36
     * @return void
37
     */
38
    public function execute(InputInterface $input, OutputInterface $output)
39
    {
40
        // get options from input
41
        $loader = $this->optionOrAsk('loader', 'Loader name(front|admin|api)', 'front');
42
        $name = $this->optionOrAsk('name', 'Controller name');
43
44
        $loader = ucfirst(Str::lowerCase($loader));
45
        $name = ucfirst(Str::lowerCase($name));
46
47
        // check loader definition
48
        if (!Arr::in($loader, ['Front', 'Admin', 'Api'])) {
49
            throw new \Exception('Wrong definition for loader. You shoud use front, admin, api');
50
        }
51
52
        // prepare code & write
53
        $template = File::read('/Private/Carcase/' . $loader . '/Controller.tphp');
54
        $code = Str::replace(['%name%'], [$name], $template);
0 ignored issues
show
Bug introduced by zenn
It seems like $template can also be of type false; however, parameter $haystack of Ffcms\Core\Helper\Type\Str::replace() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

54
        $code = Str::replace(['%name%'], [$name], /** @scrutinizer ignore-type */ $template);
Loading history...
55
56
        $savePath = '/Apps/Controller/' . $loader . '/' . $name . '.php';
57
        File::write($savePath, $code);
58
59
        $output->write('Controller are successful created: ' . $savePath);
60
    }
61
}