Passed
Push — master ( b3d1c8...051467 )
by Bruno
07:53
created

FrontendGenerator::makeProps()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 16
rs 9.9332
cc 3
nc 3
nop 0
1
<?php declare(strict_types=1);
2
3
namespace Modelarium\Frontend;
4
5
use Formularium\Datatype;
6
use Formularium\Element;
0 ignored issues
show
Bug introduced by
The type Formularium\Element 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...
7
use Formularium\Field;
8
use Formularium\Model;
9
use Formularium\FrameworkComposer;
10
use Formularium\Frontend\Blade\Framework as FrameworkBlade;
11
use Formularium\Frontend\Vue\Framework as FrameworkVue;
12
use Modelarium\GeneratedCollection;
13
use Modelarium\GeneratedItem;
14
use Modelarium\GeneratorInterface;
15
use Modelarium\GeneratorNameTrait;
16
17
use function Safe\file_get_contents;
18
19
class FrontendGenerator implements GeneratorInterface
20
{
21
    use GeneratorNameTrait;
22
23
    /**
24
     * @var FrameworkComposer
25
     */
26
    protected $composer = null;
27
28
    /**
29
     * @var Model
30
     */
31
    protected $model = null;
32
33
    /**
34
     * @var GeneratedCollection
35
     */
36
    protected $collection;
37
38
    /**
39
     *
40
     * @var string
41
     */
42
    protected $stubDir = __DIR__ . '/stubs';
43
44
    /**
45
     * String substitution helpers
46
     *
47
     * @var string[]
48
     */
49
    protected $helpers = [];
50
51
    /**
52
     * Fields
53
     *
54
     * @var Field[]
55
     */
56
    protected $cardFields = [];
57
58
    public function __construct(FrameworkComposer $composer, Model $model)
59
    {
60
        $this->composer = $composer;
61
        $this->model = $model;
62
        $this->setName($model->getName());
63
        $this->buildHelpers();
64
    }
65
66
    public function generate(): GeneratedCollection
67
    {
68
        $this->collection = new GeneratedCollection();
69
70
        /**
71
         * @var FrameworkVue $vue
72
         */
73
        $vue = $this->composer->getByName('Vue');
74
        // $blade = FrameworkComposer::getByName('Blade');
75
76
        if ($vue !== null) {
77
            $this->makeVue($vue, 'Base', 'viewable');
78
            $this->makeVue($vue, 'Card', 'viewable');
79
            $this->makeVue($vue, 'List', 'viewable');
80
            $this->makeVue($vue, 'Show', 'viewable');
81
            $this->makeVue($vue, 'Form', 'editable');
82
            $this->makeVueRoutes();
83
            $this->makeVueIndex();
84
        }
85
86
        $this->makeGraphql();
87
88
        return $this->collection;
89
    }
90
91
    protected function buildHelpers(): void
92
    {
93
        $this->cardFields = $this->model->filterField(
0 ignored issues
show
Bug introduced by
The method filterField() does not exist on Formularium\Model. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
        /** @scrutinizer ignore-call */ 
94
        $this->cardFields = $this->model->filterField(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
94
            function (Field $field) {
95
                return $field->getRenderable('card', false);
96
            }
97
        );
98
99
        $this->helpers = [
100
            '{{ submitButton }}' => $this->composer->element('Button', [Element::LABEL => 'Submit']),
0 ignored issues
show
Bug introduced by
The method element() does not exist on Formularium\FrameworkComposer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
            '{{ submitButton }}' => $this->composer->/** @scrutinizer ignore-call */ element('Button', [Element::LABEL => 'Submit']),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
            '{{ props }}' => $this->makeProps()
102
        ];
103
    }
104
105
    protected function makeVue(FrameworkVue $vue, string $component, string $mode): void
106
    {
107
        $path = $this->model->getName() . '/' .
108
            $this->model->getName() . $component . '.vue';
109
110
        $stub = file_get_contents($this->stubDir . "/Vue{$component}.stub.vue");
111
112
        if ($mode == 'editable') {
113
            $vue->setEditableTemplate($this->template($stub, $this->helpers));
114
            $this->collection->push(
115
                new GeneratedItem(
116
                    GeneratedItem::TYPE_FRONTEND,
117
                    $this->model->editable($this->composer),
118
                    $path
119
                )
120
            );
121
        } else {
122
            $vue->setViewableTemplate($this->template($stub, $this->helpers));
123
            $this->collection->push(
124
                new GeneratedItem(
125
                    GeneratedItem::TYPE_FRONTEND,
126
                    $this->model->viewable($this->composer, []),
127
                    $path
128
                )
129
            );
130
        }
131
    }
132
133
    protected function makeProps(): string
134
    {
135
        $props = [
136
            'id: {
137
                type: [Number, String],
138
                required: true,
139
            }'
140
        ];
141
    
142
        foreach ($this->cardFields as $field) {
143
            $props[] = "{$field->getName()}: { 
144
                type: String," . // TODO
145
                ($field->getValidator(Datatype::REQUIRED, false) ? 'required: true,' : '') .
146
                '}';
147
        }
148
        return implode(",\n", $props);
149
    }
150
151
    protected function makeGraphql(): void
152
    {
153
        $cardFieldNames = array_map(
154
            function (Field $field) {
155
                return $field->getName();
156
            },
157
            $this->cardFields
158
        );
159
        $cardFieldParameters = implode("\n", $cardFieldNames);
160
161
        $listQuery = <<<EOF
162
query (\$page: Int!) {
163
    {$this->lowerNamePlural}(page: \$page) {
164
        data {
165
            id
166
            $cardFieldParameters
167
        }
168
      
169
        paginatorInfo {
170
            currentPage
171
            perPage
172
            total
173
            lastPage
174
        }
175
    }
176
}
177
EOF;
178
179
        $this->collection->push(
180
            new GeneratedItem(
181
                GeneratedItem::TYPE_FRONTEND,
182
                $listQuery,
183
                $this->model->getName() . '/queryList.graphql'
184
            )
185
        );
186
187
        $itemQuery = <<<EOF
188
query (\$id: ID!) {
189
    {$this->lowerName}(id: \$id) {
190
        id
191
        TODO
192
    }
193
}
194
EOF;
195
196
        $this->collection->push(
197
            new GeneratedItem(
198
                GeneratedItem::TYPE_FRONTEND,
199
                $itemQuery,
200
                $this->model->getName() . '/queryItem.graphql'
201
            )
202
        );
203
204
        // TODO: variables
205
        $createMutation = <<<EOF
206
mutation create(\$name: String!) {
207
    {$this->lowerName}Create(name: \$name) {
208
        TODO
209
    }
210
}
211
EOF;
212
        $this->collection->push(
213
            new GeneratedItem(
214
                GeneratedItem::TYPE_FRONTEND,
215
                $createMutation,
216
                $this->model->getName() . '/mutationCreate.graphql'
217
            )
218
        );
219
    }
220
221
    protected function makeVueIndex(): void
222
    {
223
        $path = $this->model->getName() . '/index.js';
224
        $name = $this->studlyName;
225
226
        $items = [
227
            'Card',
228
            'Form',
229
            'List',
230
            'Show',
231
        ];
232
233
        $import = array_map(
234
            function ($i) use ($name) {
235
                return "import {$name}$i from './{$name}$i.vue';";
236
            },
237
            $items
238
        );
239
240
        $export = array_map(
241
            function ($i) use ($name) {
242
                return "    {$name}$i,\n";
243
            },
244
            $items
245
        );
246
247
        $this->collection->push(
248
            new GeneratedItem(
249
                GeneratedItem::TYPE_FRONTEND,
250
                implode("\n", $import) . "\n" .
251
                "export {\n" .
252
                implode("\n", $export) . "\n};\n",
253
                $path
254
            )
255
        );
256
    }
257
258
    protected function makeVueRoutes(): void
259
    {
260
        $path = $this->model->getName() . '/routes.js';
261
        $stub = file_get_contents($this->stubDir . "/routes.stub.js");
262
263
        $this->collection->push(
264
            new GeneratedItem(
265
                GeneratedItem::TYPE_FRONTEND,
266
                $this->template($stub),
267
                $path
268
            )
269
        );
270
    }
271
}
272