Passed
Push — master ( 6ac985...96b623 )
by Bruno
09:22 queued 14s
created

Processor::processStrings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
18
class Processor extends ModelariumProcessor
19
{
20
    /**
21
     * @var Parser
22
     */
23
    protected $parser = null;
24
25
    /**
26
     * @var bool
27
     */
28
    protected $runMigration = true;
29
30
    /**
31
     * @var bool
32
     */
33
    protected $runSeed = true;
34
35
    /**
36
     * @var bool
37
     */
38
    protected $runFactory = true;
39
40
    /**
41
     * @var bool
42
     */
43
    protected $runModel = true;
44
45
    /**
46
     * @var bool
47
     */
48
    protected $runPolicy = true;
49
50
    /**
51
     * @var bool
52
     */
53
    protected $runEvent = true;
54
55
    /**
56
     * Returns directives defined by Modelarium.
57
     *
58
     * @return string
59
     * @throws \Safe\Exceptions\FilesystemException
60
     */
61
    public static function getDirectives(): string
62
    {
63
        return \Safe\file_get_contents(__DIR__ . '/Graphql/definitions.graphql');
64
    }
65
66
    /**
67
     *
68
     * @param array $data
69
     * @return GeneratedCollection
70
     */
71
    public function processFiles(array $files): GeneratedCollection
72
    {
73
        $this->parser = Parser::fromFiles($files);
74
        return $this->process();
75
    }
76
77
    /**
78
     *
79
     * @param string $data
80
     * @return GeneratedCollection
81
     */
82
    public function processString(string $data): GeneratedCollection
83
    {
84
        $this->parser = Parser::fromString($data);
85
        return $this->process();
86
    }
87
88
    /**
89
     *
90
     * @param string[] $data
91
     * @return GeneratedCollection
92
     */
93
    public function processStrings(array $data): GeneratedCollection
94
    {
95
        $this->parser = Parser::fromStrings($data);
96
        return $this->process();
97
    }
98
99
    /**
100
     *
101
     * @param string $data
102
     * @return GeneratedCollection
103
     */
104
    public function process(): GeneratedCollection
105
    {
106
        $schema = $this->parser->getSchema();
107
        $typeMap = $schema->getTypeMap();
108
109
        $this->collection = new GeneratedCollection();
110
        foreach ($typeMap as $name => $object) {
111
            if ($object instanceof ObjectType) {
112
                if ($name === 'Query') {
113
                    continue;
114
                }
115
                if ($name === 'Mutation') {
116
                    continue;
117
                }
118
                $g = $this->processType((string)$name, $object);
119
                $this->collection = $this->collection->merge($g);
120
            }
121
        }
122
123
        $this->collection->merge($this->processMutation($schema->getMutationType()));
124
125
        return $this->collection;
126
    }
127
128
    protected function processType(string $name, ObjectType $object): GeneratedCollection
129
    {
130
        $collection = new GeneratedCollection();
131
        if (str_starts_with($name, '__')) {
132
            // internal type
133
            return $collection;
134
        }
135
136
        if ($this->runMigration) {
137
            $collection = $collection->merge((new MigrationGenerator($this->parser, $name, $object))->generate());
138
        }
139
        if ($this->runSeed) {
140
            $collection = $collection->merge((new SeedGenerator($this->parser, $name, $object))->generate());
141
        }
142
        if ($this->runFactory) {
143
            $collection = $collection->merge((new FactoryGenerator($this->parser, $name, $object))->generate());
144
        }
145
        if ($this->runModel) {
146
            $collection = $collection->merge((new ModelGenerator($this->parser, $name, $object))->generate());
147
        }
148
        return $collection;
149
    }
150
151
    protected function processMutation(?Type $object):  GeneratedCollection
152
    {
153
        $collection = new GeneratedCollection();
154
        if (!$object) {
155
            return $collection;
156
        }
157
        if ($this->runPolicy) {
158
            $collection = (new PolicyGenerator($this->parser, 'Mutation', $object))->generate();
159
        }
160
        if ($this->runEvent) {
161
            $collection = (new EventGenerator($this->parser, 'Mutation', $object))->generate();
162
        }
163
        return $collection;
164
    }
165
166
    /**
167
     * Set the value of runMigration
168
     *
169
     * @param  bool  $runMigration
170
     *
171
     * @return  self
172
     */
173
    public function setRunMigration(bool $runMigration): self
174
    {
175
        $this->runMigration = $runMigration;
176
177
        return $this;
178
    }
179
180
    /**
181
     * Set the value of runSeed
182
     *
183
     * @param  bool  $runSeed
184
     *
185
     * @return  self
186
     */
187
    public function setRunSeed(bool $runSeed): self
188
    {
189
        $this->runSeed = $runSeed;
190
191
        return $this;
192
    }
193
194
    /**
195
     * Set the value of runFactory
196
     *
197
     * @param  bool  $runFactory
198
     *
199
     * @return  self
200
     */
201
    public function setRunFactory(bool $runFactory): self
202
    {
203
        $this->runFactory = $runFactory;
204
205
        return $this;
206
    }
207
208
    /**
209
     * Set the value of runModel
210
     *
211
     * @param  bool  $runModel
212
     *
213
     * @return  self
214
     */
215
    public function setRunModel(bool $runModel): self
216
    {
217
        $this->runModel = $runModel;
218
219
        return $this;
220
    }
221
222
    /**
223
     * Set the value of runPolicy
224
     *
225
     * @param  bool  $runPolicy
226
     *
227
     * @return  self
228
     */
229
    public function setRunPolicy(bool $runPolicy): self
230
    {
231
        $this->runPolicy = $runPolicy;
232
233
        return $this;
234
    }
235
236
    /**
237
     * Set the value of runEvent
238
     *
239
     * @param  bool  $runEvent
240
     *
241
     * @return  self
242
     */
243
    public function setRunEvent(bool $runEvent): self
244
    {
245
        $this->runEvent = $runEvent;
246
247
        return $this;
248
    }
249
}
250