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

MakeGrid   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 10
dl 0
loc 205
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getCommandName() 0 4 1
A configureCommand() 0 11 1
A interact() 0 34 4
A configureDependencies() 0 3 1
A generate() 0 23 3
A askForNextField() 0 38 5
A generateGridConfigFile() 0 30 1
A getGridConfigDir() 0 7 1
A formatFields() 0 23 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 App\Maker\Helper\GridHelper;
17
use App\Maker\Helper\ResourceHelper;
18
use Symfony\Bundle\MakerBundle\ConsoleStyle;
19
use Symfony\Bundle\MakerBundle\DependencyBuilder;
20
use Symfony\Bundle\MakerBundle\Generator;
21
use Symfony\Bundle\MakerBundle\InputConfiguration;
22
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
23
use Symfony\Component\Console\Command\Command;
24
use Symfony\Component\Console\Input\InputArgument;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Question\ChoiceQuestion;
27
use Symfony\Component\Filesystem\Filesystem;
28
use Webmozart\Assert\Assert;
29
30
final class MakeGrid extends AbstractMaker
31
{
32
    /** @var string */
33
    private $projectDir;
34
35
    /** @var ResourceHelper */
36
    private $resourceHelper;
37
38
    /** @var GridHelper */
39
    private $gridHelper;
40
41
    /** @var Filesystem */
42
    private $fileSystem;
43
44
    public function __construct(string $projectDir, ResourceHelper $resourceHelper, GridHelper $gridHelper)
45
    {
46
        $this->projectDir = $projectDir;
47
        $this->resourceHelper = $resourceHelper;
48
        $this->gridHelper = $gridHelper;
49
50
        $this->fileSystem = new Filesystem();
51
    }
52
53
    public static function getCommandName(): string
54
    {
55
        return 'make:sylius-grid';
56
    }
57
58
    public function configureCommand(Command $command, InputConfiguration $inputConfig)
59
    {
60
        $command
61
            ->setDescription('Creates a new grid')
62
            ->addArgument('section', InputArgument::REQUIRED, 'Section of the grid (backend or frontend)')
63
            ->addArgument('resource', InputArgument::REQUIRED, 'Resource alias of the grid')
64
        ;
65
66
        $inputConfig->setArgumentAsNonInteractive('resource');
67
        $inputConfig->setArgumentAsNonInteractive('section');
68
    }
69
70
    public function interact(InputInterface $input, ConsoleStyle $io, Command $command)
71
    {
72
        if (!$input->getArgument('section')) {
73
            $question = new ChoiceQuestion(
74
                'Please select a section for your grid',
75
                ['backend', 'frontend'],
76
                0
77
            );
78
79
            $section = $io->askQuestion($question);
80
81
            $input->setArgument('section', $section);
82
        }
83
84
        if (!$input->getArgument('resource')) {
85
            $question = new ChoiceQuestion(
86
                'Please select a resource for your grid',
87
                $this->resourceHelper->getResourcesAliases(),
88
                0
89
            );
90
91
            $resourceAlias = $io->askQuestion($question);
92
93
            $input->setArgument('resource', $resourceAlias);
94
        }
95
96
        if ($resourceAlias = $input->getArgument('resource')) {
97
            Assert::true($this->resourceHelper->isResourceAliasExist($resourceAlias), sprintf(
0 ignored issues
show
Bug introduced by
It seems like $resourceAlias defined by $input->getArgument('resource') on line 96 can also be of type array<integer,string>; however, App\Maker\Helper\Resourc...: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...
98
                    'Resource with alias %s not found',
99
                    $resourceAlias
100
                )
101
            );
102
        }
103
    }
104
105
    public function configureDependencies(DependencyBuilder $dependencies)
106
    {
107
    }
108
109
    public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
110
    {
111
        $resourceAlias = $input->getArgument('resource');
112
113
        $fields = [];
114
        $isFirstField = true;
115
        while (true) {
116
            $newField = $this->askForNextField(
117
                $io,
118
                $this->resourceHelper->getResourceModelFromAlias($resourceAlias),
0 ignored issues
show
Bug introduced by
It seems like $resourceAlias defined by $input->getArgument('resource') on line 111 can also be of type array<integer,string> or null; however, App\Maker\Helper\Resourc...esourceModelFromAlias() 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...
119
                $isFirstField
120
            );
121
            $isFirstField = false;
122
123
            if (null === $newField) {
124
                break;
125
            }
126
127
            $fields[] = $newField;
128
        }
129
130
        $this->generateGridConfigFile($input, $io, $generator, $fields);
131
    }
132
133
    private function askForNextField(ConsoleStyle $io, string $entityClass, bool $isFirstField)
0 ignored issues
show
Unused Code introduced by
The parameter $entityClass 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...
134
    {
135
        $io->writeln('');
136
137
        if ($isFirstField) {
138
            $questionText = 'New field id (press <return> to stop adding fields)';
139
        } else {
140
            $questionText = 'Add another field? Enter the field id (or press <return> to stop adding fields)';
141
        }
142
143
        $fieldId = $io->ask($questionText, null, function ($name) {
144
            // allow it to be empty
145
            if (!$name) {
146
                return $name;
147
            }
148
149
            return $name;
150
        });
151
152
        if (!$fieldId) {
153
            return null;
154
        }
155
156
        $fieldType = $io->choice('Enter the field type', $this->gridHelper->getFilterIds(), 'string');
157
        $fieldLabel = $io->ask('Enter the field label', '~');
158
        $isSortable = $io->confirm('Is the field sortable?', true);
159
160
        if ($isSortable) {
161
            $fieldSortable = $io->ask('Enter the sortable path', '~');
162
        }
163
164
        return [
165
            $fieldId,
166
            $fieldType,
167
            $fieldLabel,
168
            $fieldSortable ?? null,
169
        ];
170
    }
171
172
    private function generateGridConfigFile(
173
        InputInterface $input,
174
        ConsoleStyle $io,
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...
175
        Generator $generator,
0 ignored issues
show
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...
176
        array $fields
177
    ): void {
178
        $section = $input->getArgument('section');
179
        $resourceAlias = $input->getArgument('resource');
180
        [$appName, $resourceName] = $this->resourceHelper->splitResourceAlias($resourceAlias);
0 ignored issues
show
Bug introduced by
It seems like $resourceAlias defined by $input->getArgument('resource') on line 179 can also be of type array<integer,string> or null; however, App\Maker\Helper\Resourc...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...
181
        $gridId = sprintf('%s_%s_%s', $appName, $section, $resourceName);
182
183
        $gridConfigDir = $this->getGridConfigDir($resourceAlias, $section);
0 ignored issues
show
Bug introduced by
It seems like $resourceAlias defined by $input->getArgument('resource') on line 179 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 178 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...
184
        $modelClass = $this->resourceHelper->getResourceModelFromAlias($resourceAlias);
0 ignored issues
show
Bug introduced by
It seems like $resourceAlias defined by $input->getArgument('resource') on line 179 can also be of type array<integer,string> or null; however, App\Maker\Helper\Resourc...esourceModelFromAlias() 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...
185
186
        $formattedFields = $this->formatFields($fields);
187
188
        $this->fileSystem->dumpFile($gridConfigDir, <<<EOM
189
sylius_grid:
190
    grids:
191
        $gridId:
192
            driver:
193
                name: doctrine/orm
194
                options:
195
                    class: "%$modelClass%"
196
            fields:
197
                $formattedFields
198
199
EOM
200
);
201
    }
202
203
    private function getGridConfigDir(string $resourceAlias, string $section)
204
    {
205
        $resource = $this->resourceHelper->getResourceNameFromAlias($resourceAlias);
206
        $filename = $resource.'.yaml';
207
208
        return sprintf('%s/%s/%s', $this->projectDir.'/config/packages/grids', $section, $filename);
209
    }
210
211
    private function formatFields(array $fields): string
212
    {
213
        $data = '';
214
215
        foreach ($fields as $field) {
216
            [$id, $type, $label, $sortable] = $field;
217
218
            $data .= <<<EOM
219
$id:
220
                    type: $type
221
                    label: $label
222
EOM;
223
224
            if (null !== $sortable) {
225
                $data .= <<<EOM
226
227
                    sortable: $sortable
228
EOM;
229
            }
230
        }
231
232
        return $data;
233
    }
234
}
235