Completed
Pull Request — master (#254)
by Loïc
02:43
created

MakeGrid::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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 Symfony\Bundle\MakerBundle\ConsoleStyle;
17
use Symfony\Bundle\MakerBundle\DependencyBuilder;
18
use Symfony\Bundle\MakerBundle\Generator;
19
use Symfony\Bundle\MakerBundle\InputConfiguration;
20
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
21
use Symfony\Component\Console\Command\Command;
22
use Symfony\Component\Console\Input\InputArgument;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Question\ChoiceQuestion;
25
use Symfony\Component\Filesystem\Filesystem;
26
use Webmozart\Assert\Assert;
27
28
final class MakeGrid extends AbstractMaker
29
{
30
    /** @var string */
31
    private $projectDir;
32
33
    /** @var array */
34
    private $syliusResources;
35
36
    /** @var Filesystem */
37
    private $fileSystem;
38
39
    public function __construct(string $projectDir, array $syliusResources)
40
    {
41
        $this->projectDir = $projectDir;
42
        $this->syliusResources = $syliusResources;
43
44
        $this->fileSystem = new Filesystem();
45
    }
46
47
    public static function getCommandName(): string
48
    {
49
        return 'make:grid';
50
    }
51
52
    public function configureCommand(Command $command, InputConfiguration $inputConfig)
53
    {
54
        $command
55
            ->setDescription('Creates a new grid')
56
            ->addArgument('resource', InputArgument::REQUIRED, 'Resource alias of the grid')
57
            ->addArgument('section', InputArgument::REQUIRED, 'Section of the grid (backend or frontend)')
58
        ;
59
60
        $inputConfig->setArgumentAsNonInteractive('resource');
61
        $inputConfig->setArgumentAsNonInteractive('section');
62
    }
63
64
    public function interact(InputInterface $input, ConsoleStyle $io, Command $command)
65
    {
66
        if (!$input->getArgument('resource')) {
67
            $question = new ChoiceQuestion(
68
                'Please select a resource for your grid',
69
                $this->getResourcesAliases(),
70
                0
71
            );
72
73
            $resource = $io->askQuestion($question);
74
75
            $input->setArgument('resource', $resource);
76
        }
77
78
        if ($resource = $input->getArgument('resource')) {
79
            Assert::true(in_array($resource, $this->getResourcesAliases()), sprintf('Resource with alias %s not found', $resource));
80
        }
81
82
        if (!$input->getArgument('section')) {
83
            $question = new ChoiceQuestion(
84
                'Please select a section for your grid',
85
                ['backend', 'frontend'],
86
                0
87
            );
88
89
            $section = $io->askQuestion($question);
90
91
            $input->setArgument('section', $section);
92
        }
93
    }
94
95
    public function configureDependencies(DependencyBuilder $dependencies)
96
    {
97
    }
98
99
    public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
100
    {
101
        $this->generateGridConfigFile($input, $io, $generator);
102
103
        // $this->fileSystem->dumpFile($this->projectDir.'/config/packages/grids/backend/');
104
    }
105
106
    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...
107
    {
108
        $resource = $input->getArgument('resource');
109
        [$appName, $resourceName] = explode('.', $resource);
110
        $section = $input->getArgument('section');
111
        $gridId = sprintf('%s_%s_%s', $section, $appName, $resourceName);
112
113
        $gridConfigDir = $this->getGridConfigDir($resource, $section);
0 ignored issues
show
Bug introduced by
It seems like $resource defined by $input->getArgument('resource') on line 108 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 110 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...
114
        $modelClass = sprintf('%s.model.%s.class', $appName, $resourceName);
115
116
        $this->fileSystem->dumpFile($gridConfigDir, <<<EOM
117
sylius_grid:
118
    grids:
119
        $gridId:
120
            driver:
121
                name: doctrine/orm
122
                options:
123
                    class: "%$modelClass%"
124
EOM
125
);
126
    }
127
128
    private function getResourcesAliases(): array
129
    {
130
        return array_keys($this->syliusResources);
131
    }
132
133
    private function getGridConfigDir(string $resource, string $section)
134
    {
135
        $filename = explode('.', $resource)[1].'.yaml';
136
137
        return sprintf('%s/%s/%s', $this->projectDir.'/config/packages/grids', $section, $filename);
138
    }
139
}
140