Passed
Push — master ( 864347...46e361 )
by Bruno
09:55
created

Processor::createSeederClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php declare(strict_types=1);
2
3
namespace Modelarium\Laravel;
4
5
use GraphQL\Type\Definition\ObjectType;
6
use GraphQL\Type\Definition\Type;
7
use Modelarium\GeneratedCollection;
8
use Modelarium\GeneratedItem;
9
use Modelarium\Laravel\Targets\EventGenerator;
10
use Modelarium\Laravel\Targets\FactoryGenerator;
11
use Modelarium\Laravel\Targets\MigrationGenerator;
12
use Modelarium\Laravel\Targets\ModelGenerator;
13
use Modelarium\Laravel\Targets\PolicyGenerator;
14
use Modelarium\Laravel\Targets\SeedGenerator;
15
use Modelarium\Parser;
16
use Modelarium\Processor as ModelariumProcessor;
17
use Nette\PhpGenerator\ClassType;
0 ignored issues
show
Bug introduced by
The type Nette\PhpGenerator\ClassType was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
class Processor extends ModelariumProcessor
20
{
21
    /**
22
     * @var Parser
23
     */
24
    protected $parser = null;
25
26
    /**
27
     * @var bool
28
     */
29
    protected $runMigration = true;
30
31
    /**
32
     * @var bool
33
     */
34
    protected $runSeed = true;
35
36
    /**
37
     * @var bool
38
     */
39
    protected $runFactory = true;
40
41
    /**
42
     * @var bool
43
     */
44
    protected $runModel = true;
45
46
    /**
47
     * @var bool
48
     */
49
    protected $runPolicy = true;
50
51
    /**
52
     * @var bool
53
     */
54
    protected $runEvent = true;
55
56
    /**
57
     * DatabaseSeeder class for Laravel
58
     *
59
     * @var ClassType
60
     */
61 1
    protected $seederClass = null;
62
63 1
    /**
64
     * Returns directives defined by Modelarium.
65
     *
66
     * @return string
67
     * @throws \Safe\Exceptions\FilesystemException
68
     */
69
    public static function getDirectives(): string
70
    {
71
        return \Safe\file_get_contents(__DIR__ . '/Graphql/definitions.graphql');
72
    }
73
74
    /**
75
     *
76
     * @param string[] $files
77
     * @return GeneratedCollection
78
     */
79
    public function processFiles(array $files): GeneratedCollection
80
    {
81
        $this->parser = Parser::fromFiles($files);
82 4
        return $this->process();
83
    }
84 4
85 4
    /**
86
     *
87
     * @param string $data
88
     * @return GeneratedCollection
89
     */
90
    public function processString(string $data): GeneratedCollection
91
    {
92
        $this->parser = Parser::fromString($data);
93 1
        return $this->process();
94
    }
95 1
96 1
    /**
97
     *
98
     * @param string[] $data
99
     * @return GeneratedCollection
100
     */
101
    public function processStrings(array $data): GeneratedCollection
102
    {
103 5
        $this->parser = Parser::fromStrings($data);
104
        return $this->process();
105 5
    }
106 5
107
    /**
108 5
     *
109 5
     * @return GeneratedCollection
110 5
     */
111 5
    public function process(): GeneratedCollection
112 1
    {
113
        $schema = $this->parser->getSchema();
114 5
        $typeMap = $schema->getTypeMap();
115 1
116
        $this->collection = new GeneratedCollection();
117 5
        if ($this->runSeed) {
118 5
            $this->createSeederClass();
119
        }
120
121
        foreach ($typeMap as $name => $object) {
122 5
            if ($object instanceof ObjectType) {
123
                if ($name === 'Query') {
124 5
                    continue;
125
                }
126
                if ($name === 'Mutation') {
127 5
                    continue;
128
                }
129 5
                $g = $this->processType((string)$name, $object);
130 5
                $this->collection = $this->collection->merge($g);
131
            }
132 5
        }
133
134
        $this->collection->merge($this->processMutation($schema->getMutationType()));
135 5
136 5
        if ($this->runSeed) {
137
            $printer = new \Nette\PhpGenerator\PsrPrinter;
0 ignored issues
show
Bug introduced by
The type Nette\PhpGenerator\PsrPrinter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
138 5
            $seeder = "<?php\n\n" . $printer->printClass($this->seederClass);
139 5
            $this->collection->add(
140
                new GeneratedItem(
141 5
                    GeneratedItem::TYPE_SEED,
142 5
                    $seeder,
143
                    SeedGenerator::getBasePath('database/seeds/DatabaseSeeder.php')
144 5
                )
145 5
            );
146
        }
147 5
148
        return $this->collection;
149
    }
150 5
151
    protected function processType(string $name, ObjectType $object): GeneratedCollection
152 5
    {
153 5
        $collection = new GeneratedCollection();
154 4
        if (str_starts_with($name, '__')) {
155
            // internal type
156 1
            return $collection;
157 1
        }
158
159 1
        if ($this->runMigration) {
160 1
            $collection = $collection->merge((new MigrationGenerator($this->parser, $name, $object))->generate());
161
        }
162 1
        if ($this->runSeed) {
163
            $generator = new SeedGenerator($this->parser, $name, $object);
164
            $collection = $collection->merge($generator->generate());
165
166
            $this->seederClass->getMethod('run')
167
                ->addBody('$this->call(' . $generator->getStudlyName() . 'Seeder::class);');
168
        }
169
        if ($this->runFactory) {
170
            $collection = $collection->merge((new FactoryGenerator($this->parser, $name, $object))->generate());
171
        }
172
        if ($this->runModel) {
173
            $collection = $collection->merge((new ModelGenerator($this->parser, $name, $object))->generate());
174
        }
175
        return $collection;
176
    }
177
178
    protected function processMutation(?Type $object):  GeneratedCollection
179
    {
180
        $collection = new GeneratedCollection();
181
        if (!$object) {
182
            return $collection;
183
        }
184
        if ($this->runPolicy) {
185
            $collection = (new PolicyGenerator($this->parser, 'Mutation', $object))->generate();
186
        }
187
        if ($this->runEvent) {
188
            $collection = (new EventGenerator($this->parser, 'Mutation', $object))->generate();
189
        }
190
        return $collection;
191
    }
192
193
    /**
194
     * Generates the DatabaseSeeder class.
195
     *
196
     * @return void
197
     */
198
    protected function createSeederClass(): void
199
    {
200
        $this->seederClass = new \Nette\PhpGenerator\ClassType('DatabaseSeeder');
201
        $this->seederClass->setExtends('Illuminate\Database\Seeder')
202
            ->addComment("This file was automatically generated by Modelarium.");
203
204
        $this->seederClass->addMethod('run')
205
                ->setPublic()
206
                ->addComment("Seed the application\'s database.\n@return void");
207
    }
208
209
    /**
210
     * Set the value of runMigration
211
     *
212
     * @param  bool  $runMigration
213
     *
214
     * @return  self
215
     */
216
    public function setRunMigration(bool $runMigration): self
217
    {
218
        $this->runMigration = $runMigration;
219
220
        return $this;
221
    }
222
223
    /**
224
     * Set the value of runSeed
225
     *
226
     * @param  bool  $runSeed
227
     *
228
     * @return  self
229
     */
230
    public function setRunSeed(bool $runSeed): self
231
    {
232
        $this->runSeed = $runSeed;
233
234
        return $this;
235
    }
236
237
    /**
238
     * Set the value of runFactory
239
     *
240
     * @param  bool  $runFactory
241
     *
242
     * @return  self
243
     */
244
    public function setRunFactory(bool $runFactory): self
245
    {
246
        $this->runFactory = $runFactory;
247
248
        return $this;
249
    }
250
251
    /**
252
     * Set the value of runModel
253
     *
254
     * @param  bool  $runModel
255
     *
256
     * @return  self
257
     */
258
    public function setRunModel(bool $runModel): self
259
    {
260
        $this->runModel = $runModel;
261
262
        return $this;
263
    }
264
265
    /**
266
     * Set the value of runPolicy
267
     *
268
     * @param  bool  $runPolicy
269
     *
270
     * @return  self
271
     */
272
    public function setRunPolicy(bool $runPolicy): self
273
    {
274
        $this->runPolicy = $runPolicy;
275
276
        return $this;
277
    }
278
279
    /**
280
     * Set the value of runEvent
281
     *
282
     * @param  bool  $runEvent
283
     *
284
     * @return  self
285
     */
286
    public function setRunEvent(bool $runEvent): self
287
    {
288
        $this->runEvent = $runEvent;
289
290
        return $this;
291
    }
292
}
293