Passed
Push — master ( 46e361...6daab3 )
by Bruno
09:42
created

Processor   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 304
Duplicated Lines 0 %

Test Coverage

Coverage 67.68%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 33
eloc 92
c 1
b 0
f 0
dl 0
loc 304
rs 9.76
ccs 44
cts 65
cp 0.6768

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setRunModel() 0 5 1
A getDirectivesGraphql() 0 24 5
A processMutation() 0 13 4
A processFiles() 0 4 1
A processType() 0 25 6
B process() 0 38 7
A setRunMigration() 0 5 1
A setRunEvent() 0 5 1
A getDirectivesGraphqlString() 0 3 1
A setRunSeed() 0 5 1
A processString() 0 4 1
A setRunFactory() 0 5 1
A createSeederClass() 0 9 1
A setRunPolicy() 0 5 1
A processStrings() 0 4 1
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 HaydenPierce\ClassFinder\ClassFinder;
0 ignored issues
show
Bug introduced by
The type HaydenPierce\ClassFinder\ClassFinder 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...
8
use Modelarium\GeneratedCollection;
9
use Modelarium\GeneratedItem;
10
use Modelarium\Laravel\Targets\EventGenerator;
11
use Modelarium\Laravel\Targets\FactoryGenerator;
12
use Modelarium\Laravel\Targets\MigrationGenerator;
13
use Modelarium\Laravel\Targets\ModelGenerator;
14
use Modelarium\Laravel\Targets\PolicyGenerator;
15
use Modelarium\Laravel\Targets\SeedGenerator;
16
use Modelarium\Parser;
17
use Modelarium\Processor as ModelariumProcessor;
18
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...
19
use Nuwave\Lighthouse\Schema\Factories\DirectiveFactory;
0 ignored issues
show
Bug introduced by
The type Nuwave\Lighthouse\Schema...tories\DirectiveFactory 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...
20
21
class Processor extends ModelariumProcessor
22
{
23
    /**
24
     * @var Parser
25
     */
26
    protected $parser = null;
27
28
    /**
29
     * @var bool
30
     */
31
    protected $runMigration = true;
32
33
    /**
34
     * @var bool
35
     */
36
    protected $runSeed = true;
37
38
    /**
39
     * @var bool
40
     */
41
    protected $runFactory = true;
42
43
    /**
44
     * @var bool
45
     */
46
    protected $runModel = true;
47
48
    /**
49
     * @var bool
50
     */
51
    protected $runPolicy = true;
52
53
    /**
54
     * @var bool
55
     */
56
    protected $runEvent = true;
57
58
    /**
59
     * DatabaseSeeder class for Laravel
60
     *
61 1
     * @var ClassType
62
     */
63 1
    protected $seederClass = null;
64
65
    /**
66
     * Scan the given namespaces for directive classes.
67
     *
68
     * @param  string[]  $directiveNamespaces
69
     * @return array<string, string>
70
     */
71
    public static function getDirectivesGraphql($directiveNamespaces = [ __DIR__ . 'Lighthouse/Directives' ]): array
72
    {
73
        $directives = [];
74
75
        foreach ($directiveNamespaces as $directiveNamespace) {
76
            /** @var string[] $classesInNamespace */
77
            $classesInNamespace = ClassFinder::getClassesInNamespace($directiveNamespace);
78
79
            foreach ($classesInNamespace as $class) {
80
                $reflection = new \ReflectionClass($class);
81
                if (! $reflection->isInstantiable()) {
82 4
                    continue;
83
                }
84 4
85 4
                if (! is_a($class, Directive::class, true)) {
0 ignored issues
show
Bug introduced by
The type Modelarium\Laravel\Directive 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...
86
                    continue;
87
                }
88
89
                $name = DirectiveFactory::directiveName($class);
90
                $directives[$name] = trim($class::definition());
91
            }
92
        }
93 1
94
        return $directives;
95 1
    }
96 1
97
    /**
98
     * Scan the given namespaces for directive classes.
99
     *
100
     * @param  string[]  $directiveNamespaces
101
     * @return string
102
     */
103 5
    public static function getDirectivesGraphqlString($directiveNamespaces = [ __DIR__ . 'Lighthouse/Directives' ]): string
104
    {
105 5
        return implode(self::getDirectivesGraphql($directiveNamespaces));
106 5
    }
107
108 5
    /**
109 5
     *
110 5
     * @param string[] $files
111 5
     * @return GeneratedCollection
112 1
     */
113
    public function processFiles(array $files): GeneratedCollection
114 5
    {
115 1
        $this->parser = Parser::fromFiles($files);
116
        return $this->process();
117 5
    }
118 5
119
    /**
120
     *
121
     * @param string $data
122 5
     * @return GeneratedCollection
123
     */
124 5
    public function processString(string $data): GeneratedCollection
125
    {
126
        $this->parser = Parser::fromString($data);
127 5
        return $this->process();
128
    }
129 5
130 5
    /**
131
     *
132 5
     * @param string[] $data
133
     * @return GeneratedCollection
134
     */
135 5
    public function processStrings(array $data): GeneratedCollection
136 5
    {
137
        $this->parser = Parser::fromStrings($data);
138 5
        return $this->process();
139 5
    }
140
141 5
    /**
142 5
     *
143
     * @return GeneratedCollection
144 5
     */
145 5
    public function process(): GeneratedCollection
146
    {
147 5
        $schema = $this->parser->getSchema();
148
        $typeMap = $schema->getTypeMap();
149
150 5
        $this->collection = new GeneratedCollection();
151
        if ($this->runSeed) {
152 5
            $this->createSeederClass();
153 5
        }
154 4
155
        foreach ($typeMap as $name => $object) {
156 1
            if ($object instanceof ObjectType) {
157 1
                if ($name === 'Query') {
158
                    continue;
159 1
                }
160 1
                if ($name === 'Mutation') {
161
                    continue;
162 1
                }
163
                $g = $this->processType((string)$name, $object);
164
                $this->collection = $this->collection->merge($g);
165
            }
166
        }
167
168
        $this->collection->merge($this->processMutation($schema->getMutationType()));
169
170
        if ($this->runSeed) {
171
            $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...
172
            $seeder = "<?php\n\n" . $printer->printClass($this->seederClass);
173
            $this->collection->add(
174
                new GeneratedItem(
175
                    GeneratedItem::TYPE_SEED,
176
                    $seeder,
177
                    SeedGenerator::getBasePath('database/seeds/DatabaseSeeder.php')
178
                )
179
            );
180
        }
181
182
        return $this->collection;
183
    }
184
185
    protected function processType(string $name, ObjectType $object): GeneratedCollection
186
    {
187
        $collection = new GeneratedCollection();
188
        if (str_starts_with($name, '__')) {
189
            // internal type
190
            return $collection;
191
        }
192
193
        if ($this->runMigration) {
194
            $collection = $collection->merge((new MigrationGenerator($this->parser, $name, $object))->generate());
195
        }
196
        if ($this->runSeed) {
197
            $generator = new SeedGenerator($this->parser, $name, $object);
198
            $collection = $collection->merge($generator->generate());
199
200
            $this->seederClass->getMethod('run')
201
                ->addBody('$this->call(' . $generator->getStudlyName() . 'Seeder::class);');
202
        }
203
        if ($this->runFactory) {
204
            $collection = $collection->merge((new FactoryGenerator($this->parser, $name, $object))->generate());
205
        }
206
        if ($this->runModel) {
207
            $collection = $collection->merge((new ModelGenerator($this->parser, $name, $object))->generate());
208
        }
209
        return $collection;
210
    }
211
212
    protected function processMutation(?Type $object):  GeneratedCollection
213
    {
214
        $collection = new GeneratedCollection();
215
        if (!$object) {
216
            return $collection;
217
        }
218
        if ($this->runPolicy) {
219
            $collection = (new PolicyGenerator($this->parser, 'Mutation', $object))->generate();
220
        }
221
        if ($this->runEvent) {
222
            $collection = (new EventGenerator($this->parser, 'Mutation', $object))->generate();
223
        }
224
        return $collection;
225
    }
226
227
    /**
228
     * Generates the DatabaseSeeder class.
229
     *
230
     * @return void
231
     */
232
    protected function createSeederClass(): void
233
    {
234
        $this->seederClass = new \Nette\PhpGenerator\ClassType('DatabaseSeeder');
235
        $this->seederClass->setExtends('Illuminate\Database\Seeder')
236
            ->addComment("This file was automatically generated by Modelarium.");
237
238
        $this->seederClass->addMethod('run')
239
                ->setPublic()
240
                ->addComment("Seed the application\'s database.\n@return void");
241
    }
242
243
    /**
244
     * Set the value of runMigration
245
     *
246
     * @param  bool  $runMigration
247
     *
248
     * @return  self
249
     */
250
    public function setRunMigration(bool $runMigration): self
251
    {
252
        $this->runMigration = $runMigration;
253
254
        return $this;
255
    }
256
257
    /**
258
     * Set the value of runSeed
259
     *
260
     * @param  bool  $runSeed
261
     *
262
     * @return  self
263
     */
264
    public function setRunSeed(bool $runSeed): self
265
    {
266
        $this->runSeed = $runSeed;
267
268
        return $this;
269
    }
270
271
    /**
272
     * Set the value of runFactory
273
     *
274
     * @param  bool  $runFactory
275
     *
276
     * @return  self
277
     */
278
    public function setRunFactory(bool $runFactory): self
279
    {
280
        $this->runFactory = $runFactory;
281
282
        return $this;
283
    }
284
285
    /**
286
     * Set the value of runModel
287
     *
288
     * @param  bool  $runModel
289
     *
290
     * @return  self
291
     */
292
    public function setRunModel(bool $runModel): self
293
    {
294
        $this->runModel = $runModel;
295
296
        return $this;
297
    }
298
299
    /**
300
     * Set the value of runPolicy
301
     *
302
     * @param  bool  $runPolicy
303
     *
304
     * @return  self
305
     */
306
    public function setRunPolicy(bool $runPolicy): self
307
    {
308
        $this->runPolicy = $runPolicy;
309
310
        return $this;
311
    }
312
313
    /**
314
     * Set the value of runEvent
315
     *
316
     * @param  bool  $runEvent
317
     *
318
     * @return  self
319
     */
320
    public function setRunEvent(bool $runEvent): self
321
    {
322
        $this->runEvent = $runEvent;
323
324
        return $this;
325
    }
326
}
327