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

GeneratorModelCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 GeneratorModelCommand. Add new model based on carcase template
16
 * @package Apps\Console
17
 */
18
class GeneratorModelCommand extends Command
19
{
20
    /**
21
     * Register command and options
22
     */
23
    public function configure()
24
    {
25
        $this->setName('generator:model')
26
            ->setDescription('Generate default model template')
27
            ->addOption('loader', 'loader', InputOption::VALUE_OPTIONAL, 'Set which loader will be used to create model. Example: front, admin, install')
28
            ->addOption('controller', 'controller', InputOption::VALUE_OPTIONAL, 'Set related controller for this model. This value will be used as sub-directory for model')
29
            ->addOption('name', 'name', InputOption::VALUE_OPTIONAL, 'Set model name. Example: FormUserCreate, EntityContentShow');
30
    }
31
32
    /**
33
     * Generate model from template carcase
34
     * @param InputInterface $input
35
     * @param OutputInterface $output
36
     * @throws \Exception
37
     * @return void
38
     */
39
    public function execute(InputInterface $input, OutputInterface $output)
40
    {
41
        // get options from input
42
        $loader = $this->optionOrAsk('loader', 'Loader name(front|admin|api)', 'front');
43
        $controller = $this->optionOrAsk('controller', 'Related controller');
44
        $name = $this->optionOrAsk('name', 'Controller name');
45
46
        $loader = ucfirst(Str::lowerCase($loader));
47
        $controller = ucfirst(Str::lowerCase($controller));
48
        $name = ucfirst(Str::lowerCase($name));
49
50
        // check loader definition
51
        if (!Arr::in($loader, ['Front', 'Admin', 'Api'])) {
52
            throw new \Exception('Wrong definition for loader. You shoud use front, admin, api');
53
        }
54
55
        $namespace = 'Apps\Model\\' . $loader . '\\' . $controller;
56
        $tpl = File::read('/Private/Carcase/Model.tphp');
57
        $code = Str::replace(['%namespace%', '%name%'], [$namespace, $name], $tpl);
0 ignored issues
show
Bug introduced by zenn
It seems like $tpl 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

57
        $code = Str::replace(['%namespace%', '%name%'], [$namespace, $name], /** @scrutinizer ignore-type */ $tpl);
Loading history...
58
        $path = '/Apps/Model/' . $loader . '/' . $controller . '/' . $name . '.php';
59
        File::write($path, $code);
60
        $output->writeln('Model are successful created: ' . $path);
61
    }
62
63
}