Passed
Pull Request — master (#247)
by Kevin
04:29
created

MakeFactory::generate()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 4
c 3
b 0
f 0
dl 0
loc 7
ccs 4
cts 5
cp 0.8
rs 10
cc 3
nc 4
nop 3
crap 3.072
1
<?php
2
3
namespace Zenstruck\Foundry\Bundle\Maker;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\Persistence\ManagerRegistry;
7
use Symfony\Bundle\MakerBundle\ConsoleStyle;
8
use Symfony\Bundle\MakerBundle\DependencyBuilder;
9
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
10
use Symfony\Bundle\MakerBundle\Generator;
11
use Symfony\Bundle\MakerBundle\InputConfiguration;
12
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Zenstruck\Foundry\ModelFactory;
18
19
/**
20
 * @author Kevin Bond <[email protected]>
21
 */
22
final class MakeFactory extends AbstractMaker
23
{
24
    private const ORM_DEFAULTS = [
25 20
        'ARRAY' => '[],',
26
        'ASCII_STRING' => 'self::faker()->text(),',
27 20
        'BIGINT' => 'self::faker()->randomNumber(),',
28 20
        'BLOB' => 'self::faker()->text(),',
29
        'BOOLEAN' => 'self::faker()->boolean(),',
30 4
        'DATE' => 'self::faker()->datetime(),',
31
        'DATE_MUTABLE' => 'self::faker()->datetime(),',
32 4
        'DATE_IMMUTABLE' => 'self::faker()->datetime(),',
33
        'DATETIME_MUTABLE' => 'self::faker()->datetime(),',
34
        'DATETIME_IMMUTABLE' => 'self::faker()->datetime(),',
35 20
        'DATETIMETZ_MUTABLE' => 'self::faker()->datetime(),',
36
        'DATETIMETZ_IMMUTABLE' => 'self::faker()->datetime(),',
37
        'DECIMAL' => 'self::faker()->randomFloat(),',
38 20
        'FLOAT' => 'self::faker()->randomFloat(),',
39 20
        'INTEGER' => 'self::faker()->randomNumber(),',
40 20
        'JSON' => '[],',
41
        'JSON_ARRAY' => '[],',
42
        'SIMPLE_ARRAY' => '[],',
43 20
        'SMALLINT' => 'self::faker()->numberBetween(1, 32767),',
44 20
        'STRING' => 'self::faker()->text(),',
45
        'TEXT' => 'self::faker()->text(),',
46 20
        'TIME_MUTABLE' => 'self::faker()->datetime(),',
47
        'TIME_IMMUTABLE' => 'self::faker()->datetime(),',
48 20
    ];
49 12
50
    /** @var ManagerRegistry */
51
    private $managerRegistry;
52 8
53 4
    /** @var string[] */
54 4
    private $entitiesWithFactories;
55
56
    public function __construct(ManagerRegistry $managerRegistry, \Traversable $factories)
57 8
    {
58 8
        $this->managerRegistry = $managerRegistry;
59
        $this->entitiesWithFactories = \array_map(
60 8
            static function(ModelFactory $factory) {
61 8
                return $factory::getEntityClass();
62
            },
63 20
            \iterator_to_array($factories)
64
        );
65 20
    }
66
67 20
    public static function getCommandName(): string
68 4
    {
69
        return 'make:factory';
70
    }
71 20
72 4
    public static function getCommandDescription(): string
73
    {
74
        return 'Creates a Foundry model factory for a Doctrine entity class';
75 16
    }
76 16
77 16
    public function configureCommand(Command $command, InputConfiguration $inputConfig): void
78 16
    {
79 16
        $command
80
            ->setDescription(self::getCommandDescription())
81
            ->addArgument('entity', InputArgument::OPTIONAL, 'Entity class to create a factory for')
82 16
            ->addOption('namespace', null, InputOption::VALUE_REQUIRED, 'Customize the namespace for generated factories', 'Factory')
83
            ->addOption('test', null, InputOption::VALUE_NONE, 'Create in <fg=yellow>tests/</> instead of <fg=yellow>src/</>')
84 16
            ->addOption('all-fields', null, InputOption::VALUE_NONE, 'Create defaults for all entity fields, not only required fields')
85
        ;
86 16
87
        $inputConfig->setArgumentAsNonInteractive('entity');
88
    }
89 16
90 16
    public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
91 16
    {
92
        if ($input->getArgument('entity')) {
93 16
            return;
94 16
        }
95
96
        if (!$input->getOption('test')) {
97
            $io->text('// Note: pass <fg=yellow>--test</> if you want to generate factories in your <fg=yellow>tests/</> directory');
98 16
            $io->newLine();
99
        }
100 16
101
        if (!$input->getOption('all-fields')) {
102 16
            $io->text('// Note: pass <fg=yellow>--all-fields</> if you want to generate default values for all fields, not only required fields');
103 16
            $io->newLine();
104
        }
105
106 16
        $argument = $command->getDefinition()->getArgument('entity');
107
        $entity = $io->choice($argument->getDescription(), \array_merge($this->entityChoices(), ['All']));
108 20
109
        $input->setArgument('entity', $entity);
110
    }
111 20
112
    public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
113 8
    {
114
        $entity = $input->getArgument('entity');
115 8
        $classes = 'All' === $entity ? $this->entityChoices() : [$entity];
116
117 8
        foreach ($classes as $class) {
118 8
            $this->generateFactory($class, $input, $io, $generator);
119 8
        }
120
    }
121
122
    /**
123 8
     * Generates a single entity factory.
124
     */
125 8
    private function generateFactory(string $class, InputInterface $input, ConsoleStyle $io, Generator $generator)
126
    {
127
        if (!\class_exists($class)) {
128
            $class = $generator->createClassNameDetails($class, 'Entity\\')->getFullName();
129
        }
130
131
        if (!\class_exists($class)) {
132
            throw new RuntimeCommandException(\sprintf('Entity "%s" not found.', $input->getArgument('entity')));
133
        }
134
135
        $namespace = $input->getOption('namespace');
136
137
        // strip maker's root namespace if set
138
        if (0 === \mb_strpos($namespace, $generator->getRootNamespace())) {
139
            $namespace = \mb_substr($namespace, \mb_strlen($generator->getRootNamespace()));
140
        }
141
142
        $namespace = \trim($namespace, '\\');
143
144
        // if creating in tests dir, ensure namespace prefixed with Tests\
145
        if ($input->getOption('test') && 0 !== \mb_strpos($namespace, 'Tests\\')) {
146
            $namespace = 'Tests\\'.$namespace;
147
        }
148
149
        $entity = new \ReflectionClass($class);
150
        $factory = $generator->createClassNameDetails($entity->getShortName(), $namespace, 'Factory');
151
152
        $repository = new \ReflectionClass($this->managerRegistry->getRepository($entity->getName()));
153
154
        if (0 !== \mb_strpos($repository->getName(), $generator->getRootNamespace())) {
155
            // not using a custom repository
156
            $repository = null;
157
        }
158
159
        $generator->generateClass(
160
            $factory->getFullName(),
161
            __DIR__.'/../Resources/skeleton/Factory.tpl.php',
162
            [
163
                'entity' => $entity,
164
                'defaultProperties' => $this->defaultPropertiesFor($entity->getName(), $input->getOption('all-fields')),
165
                'repository' => $repository,
166
            ]
167
        );
168
169
        $generator->writeChanges();
170
171
        $this->writeSuccessMessage($io);
172
173
        $io->text([
174
            'Next: Open your new factory and set default values/states.',
175
            'Find the documentation at https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories',
176
        ]);
177
    }
178
179
    public function configureDependencies(DependencyBuilder $dependencies): void
180
    {
181
        // noop
182
    }
183
184
    private function entityChoices(): array
185
    {
186
        $choices = [];
187
188
        foreach ($this->managerRegistry->getManagers() as $manager) {
189
            foreach ($manager->getMetadataFactory()->getAllMetadata() as $metadata) {
190
                if (!\in_array($metadata->getName(), $this->entitiesWithFactories, true)) {
191
                    $choices[] = $metadata->getName();
192
                }
193
            }
194
        }
195
196
        \sort($choices);
197
198
        if (empty($choices)) {
199
            throw new RuntimeCommandException('No entities or documents found, or none left to make factories for.');
200
        }
201
202
        return $choices;
203
    }
204
205
    private function defaultPropertiesFor(string $class, bool $allFields): iterable
206
    {
207
        $em = $this->managerRegistry->getManagerForClass($class);
208
209
        if (!$em instanceof EntityManagerInterface) {
210
            return [];
211
        }
212
213
        $metadata = $em->getClassMetadata($class);
214
        $ids = $metadata->getIdentifierFieldNames();
215
216
        foreach ($metadata->fieldMappings as $property) {
217
            // ignore identifiers and nullable fields
218
            if ((!$allFields && ($property['nullable'] ?? false)) || \in_array($property['fieldName'], $ids, true)) {
219
                continue;
220
            }
221
222
            $type = \mb_strtoupper($property['type']);
223
            $value = "null, // TODO add {$type} ORM type manually";
224
225
            if (\array_key_exists($type, self::ORM_DEFAULTS)) {
226
                $value = self::ORM_DEFAULTS[$type];
227
            }
228
229
            yield $property['fieldName'] => $value;
230
        }
231
    }
232
}
233