Completed
Pull Request — master (#254)
by Loïc
01:50
created

MakeGrid::configureCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of monofony.
5
 *
6
 * (c) Mobizel
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace App\Maker;
15
16
use App\Maker\Helper\SyliusResourcesHelper;
17
use Symfony\Bundle\MakerBundle\ConsoleStyle;
18
use Symfony\Bundle\MakerBundle\DependencyBuilder;
19
use Symfony\Bundle\MakerBundle\Generator;
20
use Symfony\Bundle\MakerBundle\InputConfiguration;
21
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
22
use Symfony\Component\Console\Command\Command;
23
use Symfony\Component\Console\Input\InputArgument;
24
use Symfony\Component\Console\Input\InputInterface;
25
use Symfony\Component\Console\Question\ChoiceQuestion;
26
use Symfony\Component\Filesystem\Filesystem;
27
use Webmozart\Assert\Assert;
28
29
final class MakeGrid extends AbstractMaker
30
{
31
    /** @var string */
32
    private $projectDir;
33
34
    /** @var SyliusResourcesHelper */
35
    private $syliusResourcesHelper;
36
37
    /** @var Filesystem */
38
    private $fileSystem;
39
40
    public function __construct(string $projectDir, SyliusResourcesHelper $syliusResourcesHelper)
41
    {
42
        $this->projectDir = $projectDir;
43
        $this->syliusResourcesHelper = $syliusResourcesHelper;
44
45
        $this->fileSystem = new Filesystem();
46
    }
47
48
    public static function getCommandName(): string
49
    {
50
        return 'make:sylius-grid';
51
    }
52
53
    public function configureCommand(Command $command, InputConfiguration $inputConfig)
54
    {
55
        $command
56
            ->setDescription('Creates a new grid')
57
            ->addArgument('resource', InputArgument::REQUIRED, 'Resource alias of the grid')
58
            ->addArgument('section', InputArgument::REQUIRED, 'Section of the grid (backend or frontend)')
59
        ;
60
61
        $inputConfig->setArgumentAsNonInteractive('resource');
62
        $inputConfig->setArgumentAsNonInteractive('section');
63
    }
64
65
    public function interact(InputInterface $input, ConsoleStyle $io, Command $command)
66
    {
67
        if (!$input->getArgument('section')) {
68
            $question = new ChoiceQuestion(
69
                'Please select a section for your grid',
70
                ['backend', 'frontend'],
71
                0
72
            );
73
74
            $section = $io->askQuestion($question);
75
76
            $input->setArgument('section', $section);
77
        }
78
79
        if (!$input->getArgument('resource')) {
80
            $question = new ChoiceQuestion(
81
                'Please select a resource for your grid',
82
                $this->syliusResourcesHelper->getResourcesAliases(),
83
                0
84
            );
85
86
            $resourceAlias = $io->askQuestion($question);
87
88
            $input->setArgument('resource', $resourceAlias);
89
        }
90
91
        if ($resourceAlias = $input->getArgument('resource')) {
92
            Assert::true($this->syliusResourcesHelper->isResourceAliasExist($resourceAlias), sprintf(
0 ignored issues
show
Bug introduced by
It seems like $resourceAlias defined by $input->getArgument('resource') on line 91 can also be of type array<integer,string>; however, App\Maker\Helper\SyliusR...:isResourceAliasExist() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
93
                    'Resource with alias %s not found',
94
                    $resourceAlias
95
                )
96
            );
97
        }
98
    }
99
100
    public function configureDependencies(DependencyBuilder $dependencies)
101
    {
102
    }
103
104
    public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
105
    {
106
        $this->generateGridConfigFile($input, $io, $generator);
107
    }
108
109
    private function generateGridConfigFile(InputInterface $input, ConsoleStyle $io, Generator $generator)
0 ignored issues
show
Unused Code introduced by
The parameter $io is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $generator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
    {
111
        $section = $input->getArgument('section');
112
        $resourceAlias = $input->getArgument('resource');
113
        [$appName, $resourceName] = $this->syliusResourcesHelper->splitResourceAlias($resourceAlias);
0 ignored issues
show
Bug introduced by
It seems like $resourceAlias defined by $input->getArgument('resource') on line 112 can also be of type array<integer,string> or null; however, App\Maker\Helper\SyliusR...r::splitResourceAlias() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
114
        $gridId = sprintf('%s_%s_%s', $appName, $section, $resourceName);
115
116
        $gridConfigDir = $this->getGridConfigDir($resourceAlias, $section);
0 ignored issues
show
Bug introduced by
It seems like $resourceAlias defined by $input->getArgument('resource') on line 112 can also be of type array<integer,string> or null; however, App\Maker\MakeGrid::getGridConfigDir() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $section defined by $input->getArgument('section') on line 111 can also be of type array<integer,string> or null; however, App\Maker\MakeGrid::getGridConfigDir() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
117
        $modelClass = sprintf('%s.model.%s.class', $appName, $resourceName);
118
119
        $this->fileSystem->dumpFile($gridConfigDir, <<<EOM
120
sylius_grid:
121
    grids:
122
        $gridId:
123
            driver:
124
                name: doctrine/orm
125
                options:
126
                    class: "%$modelClass%"
127
128
EOM
129
);
130
    }
131
132
    private function getGridConfigDir(string $resourceAlias, string $section)
133
    {
134
        $resource = $this->syliusResourcesHelper->getResourceNameFromAlias($resourceAlias);
135
        $filename = $resource.'.yaml';
136
137
        return sprintf('%s/%s/%s', $this->projectDir.'/config/packages/grids', $section, $filename);
138
    }
139
}
140