Issues (150)

src/Blocks/FormRequest.php (2 issues)

private methods are used.

Unused Code Minor
1
<?php
2
3
namespace SoliDry\Blocks;
4
5
use SoliDry\Controllers\BaseCommand;
6
use SoliDry\Extension\BaseFormRequest;
7
use SoliDry\Extension\JSONApiInterface;
8
use SoliDry\Helpers\Console;
9
use SoliDry\Helpers\MethodOptions;
10
use SoliDry\ApiGenerator;
11
use SoliDry\Helpers\Classes;
12
use SoliDry\Helpers\MigrationsHelper;
13
use SoliDry\Types\ConfigInterface;
14
use SoliDry\Types\CustomsInterface;
15
use SoliDry\Types\DefaultInterface;
16
use SoliDry\Types\DirsInterface;
17
use SoliDry\Types\HTTPMethodsInterface;
18
use SoliDry\Types\MethodsInterface;
19
use SoliDry\Types\FromRequestInterface;
20
use SoliDry\Types\PhpInterface;
21
use SoliDry\Types\ApiInterface;
22
23
/**
24
 * Class FormRequest
25
 *
26
 * @package SoliDry\Blocks
27
 * @property ApiGenerator generator
28
 */
29
class FormRequest extends FormRequestModel
30
{
31
    use ContentManager;
32
33
    /**
34
     * @var string
35
     */
36
    protected string $sourceCode = '';
37
38
    /**
39
     * @var string
40
     */
41
    protected string $resourceCode = '';
42
43
    /**
44
     * @var BaseCommand
45
     */
46
    protected BaseCommand $generator;
47
48
    /**
49
     * @var array|\string[][]
50
     */
51
    private array $additionalProps = [
52
        'id' => [
53
            'type' => 'integer',
54
        ],
55
    ];
56
57
    /**
58
     * @var string
59
     */
60
    private string $className;
61
62
    /**
63
     * FormRequest constructor.
64
     * @param BaseCommand $generator
65
     */
66
    public function __construct(BaseCommand $generator)
67
    {
68
        $this->generator = $generator;
69
        $this->className = Classes::getClassName($this->generator->objectName);
70
    }
71
72
    /**
73
     * @param $generator
74
     */
75
    public function setCodeState($generator)
76
    {
77
        $this->generator = $generator;
78
    }
79
80
    /**
81
     * @param null $relationTypes
82
     */
83
    private function setProps($relationTypes = null)
84
    {
85
        $this->setAdditionalProps();
86
        // properties creation
87
        $this->setPropsContent();
88
        // related props
89
        $this->setRelationTypes($relationTypes);
90
    }
91
92
    /**
93
     *  Sets an additional props e.g.: id
94
     */
95
    private function setAdditionalProps(): void
96
    {
97
        // additional props
98
        if (!empty($this->additionalProps)) {
99
            foreach ($this->additionalProps as $prop => $propVal) {
100
                $this->createProperty($prop, PhpInterface::PHP_MODIFIER_PUBLIC);
101
            }
102
        }
103
    }
104
105
    /**
106
     *  Sets props values
107
     */
108
    private function setPropsContent(): void
109
    {
110
        $this->setComment(CustomsInterface::CUSTOM_TYPES_ATTRIBUTES);
111
112
        /** @var array $objectProps */
113
        $objectProps = $this->generator->types[$this->generator->objectProps[ApiInterface::RAML_ATTRS]][ApiInterface::RAML_PROPS];
114
        foreach ($objectProps as $propKey => $propVal) {
115
116
            if (is_array($propVal)) {
117
                $this->createProperty($propKey, PhpInterface::PHP_MODIFIER_PUBLIC);
118
119
                if (empty($propVal[ApiInterface::RAML_FACETS][ConfigInterface::BIT_MASK]) === false) {
120
                    $this->setComment(ConfigInterface::BIT_MASK);
121
122
                    foreach ($propVal[ApiInterface::RAML_FACETS][ConfigInterface::BIT_MASK] as $flag => $bit) {
123
                        $this->createProperty($flag, PhpInterface::PHP_MODIFIER_PUBLIC, $bit);
124
                    }
125
                }
126
            }
127
        }
128
    }
129
130
    /**
131
     * Sets relation types
132
     *
133
     * @param $relationTypes
134
     */
135
    private function setRelationTypes($relationTypes): void
136
    {
137
        // related props
138
        if ($relationTypes !== null) {
139
            $this->sourceCode .= PhpInterface::TAB_PSR4 . PhpInterface::COMMENT . ' Relations' . PHP_EOL;
140
141
            foreach ($relationTypes as $attrKey => $attrVal) {
142
                // determine attr
143
                if ($attrKey !== ApiInterface::RAML_ID && $attrKey !== ApiInterface::RAML_TYPE) {
144
                    $this->createProperty($attrKey, PhpInterface::PHP_MODIFIER_PUBLIC);
145
                }
146
            }
147
            $this->sourceCode .= PHP_EOL;
148
        }
149
    }
150
151
    /**
152
     *  Sets all rules for this FormRequest
153
     */
154
    private function constructRules(): void
155
    {
156
        // Authorize method - defaults to false
157
        $methodOptions = new MethodOptions();
158
        $methodOptions->setName(PhpInterface::PHP_AUTHORIZE);
159
        $methodOptions->setReturnType(PhpInterface::PHP_TYPES_BOOL);
160
        $this->startMethod($methodOptions);
161
        $this->setMethodReturn(PhpInterface::PHP_TYPES_BOOL_TRUE);
162
        $this->endMethod();
163
164
        // Rules method
165
        $methodOptions->setName(PhpInterface::PHP_RULES);
166
        $methodOptions->setReturnType(PhpInterface::PHP_TYPES_ARRAY);
167
        $this->startMethod($methodOptions);
168
        // attrs validation
169
        $this->startArray();
170
        // gather Types and constraints
171
        $this->setPropertyFilters();
172
        $this->endArray();
173
        $this->endMethod();
174
    }
175
176
    /**
177
     * Sets all relations for Entity via FormRequests
178
     *
179
     * @param $relationTypes
180
     */
181
    private function constructRelations($relationTypes): void
182
    {
183
        $methodOptions = new MethodOptions();
184
        $methodOptions->setName(MethodsInterface::RELATIONS);
185
        $methodOptions->setReturnType(PhpInterface::PHP_TYPES_ARRAY);
186
        $this->startMethod($methodOptions);
187
        // attrs validation
188
        $this->startArray();
189
        if (empty($relationTypes) === false) {
190
            $rel = empty($relationTypes[ApiInterface::RAML_TYPE]) ? $relationTypes :
191
                $relationTypes[ApiInterface::RAML_TYPE];
192
193
            $rels = explode(PhpInterface::PIPE, str_replace('[]', '', $rel));
194
            foreach ($rels as $k => $rel) {
195
                $this->setRelations(MigrationsHelper::getTableName(trim(str_replace(CustomsInterface::CUSTOM_TYPES_RELATIONSHIPS, '', $rel))));
196
                if (empty($rels[$k + 1]) === false) {
197
                    $this->sourceCode .= PHP_EOL;
198
                }
199
            }
200
        }
201
        $this->endArray();
202
        $this->endMethod(1);
203
    }
204
205
    /**
206
     * @param $relationTypes
207
     */
208
    private function setRelations($relationTypes): void
209
    {
210
        $this->setTabs(3);
211
        $this->sourceCode .= PhpInterface::QUOTES . $relationTypes . PhpInterface::QUOTES . PhpInterface::COMMA;
212
    }
213
214
    /**
215
     *  Sets content of *FormRequest
216
     */
217
    private function setContent(): void
0 ignored issues
show
The method setContent() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
218
    {
219
        $this->setTag();
220
        $this->setNamespace(
221
            $this->generator->httpDir .
222
            PhpInterface::BACKSLASH .
223
            $this->generator->formRequestDir
224
        );
225
226
        $baseFullForm = BaseFormRequest::class;
227
        $baseFormName = Classes::getName($baseFullForm);
228
        $this->setUse($baseFullForm, false, true);
229
        $this->startClass($this->className . DefaultInterface::FORM_REQUEST_POSTFIX, $baseFormName);
230
231
        $this->setComment(DefaultInterface::PROPS_START);
232
        if (empty($this->generator->objectProps[ApiInterface::RAML_RELATIONSHIPS][ApiInterface::RAML_TYPE]) === false
233
            && empty($this->generator->types[$this->generator->objectProps[ApiInterface::RAML_RELATIONSHIPS][ApiInterface::RAML_TYPE]]) === false) {
234
            $this->setProps(
235
                $this->generator->types[$this->generator->objectProps[ApiInterface::RAML_RELATIONSHIPS][ApiInterface::RAML_TYPE]]
236
                [ApiInterface::RAML_PROPS][ApiInterface::RAML_DATA][ApiInterface::RAML_ITEMS]
237
            );
238
        } else {
239
            $this->setProps();
240
        }
241
        $this->setComment(DefaultInterface::PROPS_END);
242
        $this->sourceCode .= PHP_EOL;
243
        $this->setComment(DefaultInterface::METHOD_START);
244
        $this->constructRules();
245
        $relTypes = empty($this->generator->objectProps[ApiInterface::RAML_RELATIONSHIPS][ApiInterface::RAML_TYPE])
246
            ? [] : $this->generator->objectProps[ApiInterface::RAML_RELATIONSHIPS][ApiInterface::RAML_TYPE];
247
        $this->constructRelations($relTypes);
248
        $this->setComment(DefaultInterface::METHOD_END);
249
        // create closing brace
250
        $this->endClass();
251
    }
252
253
    /**
254
     *  Sets content of *FormRequest
255
     */
256
    private function resetContent(): void
0 ignored issues
show
The method resetContent() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
257
    {
258
        $this->setBeforeProps($this->getEntityFile($this->generator->formatRequestsPath(),
259
            DefaultInterface::FORM_REQUEST_POSTFIX));
260
        $this->setComment(DefaultInterface::PROPS_START, 0);
261
        if (empty($this->generator->objectProps[ApiInterface::RAML_RELATIONSHIPS][ApiInterface::RAML_TYPE]) === false
262
            && empty($this->generator->types[$this->generator->objectProps[ApiInterface::RAML_RELATIONSHIPS][ApiInterface::RAML_TYPE]]) === false) {
263
            $this->setProps(
264
                $this->generator->types[$this->generator->objectProps[ApiInterface::RAML_RELATIONSHIPS][ApiInterface::RAML_TYPE]]
265
                [ApiInterface::RAML_PROPS][ApiInterface::RAML_DATA][ApiInterface::RAML_ITEMS]
266
            );
267
        } else {
268
            $this->setProps();
269
        }
270
        $this->setAfterProps(DefaultInterface::METHOD_START);
271
        $this->setComment(DefaultInterface::METHOD_START, 0);
272
        $this->constructRules();
273
        $relTypes = empty($this->generator->objectProps[ApiInterface::RAML_RELATIONSHIPS][ApiInterface::RAML_TYPE])
274
            ? [] : $this->generator->objectProps[ApiInterface::RAML_RELATIONSHIPS][ApiInterface::RAML_TYPE];
275
        $this->constructRelations($relTypes);
276
        $this->setAfterMethods();
277
    }
278
279
    /**
280
     *  Creates an ApiRequestToken Requests class
281
     */
282
    public function createAccessToken(): void
283
    {
284
        if (empty($this->generator->types[CustomsInterface::CUSTOM_TYPES_QUERY_PARAMS][ApiInterface::RAML_PROPS]
285
            [JSONApiInterface::PARAM_ACCESS_TOKEN][ApiInterface::RAML_KEY_DEFAULT]) === false
286
        ) {
287
            $this->setAccessTokenContent();
288
            $fileForm = FileManager::getModulePath($this->generator, true) . $this->generator->formRequestDir
289
                . PhpInterface::SLASH . JSONApiInterface::CLASS_API_ACCESS_TOKEN
290
                . PhpInterface::PHP_EXT;
291
292
            $isCreated = FileManager::createFile(
293
                $fileForm, $this->sourceCode,
294
                FileManager::isRegenerated($this->generator->options)
295
            );
296
297
            if ($isCreated) {
298
                Console::out($fileForm . PhpInterface::SPACE . Console::CREATED, Console::COLOR_GREEN);
299
            }
300
        }
301
    }
302
303
    /**
304
     *  Sets content for ApiRequestToken Requests class
305
     */
306
    private function setAccessTokenContent(): void
307
    {
308
        $this->setTag();
309
        $this->sourceCode .= PhpInterface::PHP_NAMESPACE . PhpInterface::SPACE .
310
            DirsInterface::APPLICATION_DIR . PhpInterface::BACKSLASH . $this->generator->httpDir .
311
            PhpInterface::BACKSLASH . $this->generator->formRequestDir
312
            . PhpInterface::SEMICOLON . PHP_EOL . PHP_EOL;
313
314
        $this->setUse(PhpInterface::CLASS_CLOSURE, false, true);
315
        $this->startClass(JSONApiInterface::CLASS_API_ACCESS_TOKEN);
316
        $methodOptions = new MethodOptions();
317
        $methodOptions->setName(FromRequestInterface::METHOD_HANDLE);
318
        $methodOptions->setParams([
319
            FromRequestInterface::METHOD_PARAM_REQUEST,
320
            PhpInterface::CLASS_CLOSURE => FromRequestInterface::METHOD_PARAM_NEXT,
321
        ]);
322
        $this->startMethod($methodOptions);
323
        $this->setHandleMethodContent();
324
        $this->endMethod();
325
        $this->endClass();
326
    }
327
328
    /**
329
     *  Sets content for ApiRequestToken Requests class handle() method
330
     */
331
    private function setHandleMethodContent(): void
332
    {
333
        $this->setTabs(2);
334
        $this->sourceCode .= PhpInterface::IF . PhpInterface::SPACE . PhpInterface::OPEN_PARENTHESES
335
            . PhpInterface::OPEN_PARENTHESES . PhpInterface::PHP_TYPES_STRING . PhpInterface::CLOSE_PARENTHESES
336
            . PhpInterface::SPACE . PhpInterface::DOLLAR_SIGN . FromRequestInterface::METHOD_PARAM_REQUEST
337
            . PhpInterface::ARROW . JSONApiInterface::PARAM_ACCESS_TOKEN . PhpInterface::SPACE . PhpInterface::EXCLAMATION
338
            . PhpInterface::EQUALS . PhpInterface::EQUALS . PhpInterface::SPACE
339
            . PhpInterface::OPEN_PARENTHESES . PhpInterface::PHP_TYPES_STRING . PhpInterface::CLOSE_PARENTHESES
340
            . PhpInterface::SPACE . MethodsInterface::CONFIG . PhpInterface::OPEN_PARENTHESES . PhpInterface::QUOTES
341
            . $this->generator->version . PhpInterface::DOT . ConfigInterface::QUERY_PARAMS . PhpInterface::DOT
342
            . JSONApiInterface::PARAM_ACCESS_TOKEN . PhpInterface::QUOTES
343
            . PhpInterface::CLOSE_PARENTHESES . PhpInterface::CLOSE_PARENTHESES . PHP_EOL;
344
        $this->setTabs(2);
345
        $this->sourceCode .= PhpInterface::OPEN_BRACE . PHP_EOL;
346
        // response body
347
        $this->setTabs(3);
348
        $this->sourceCode .= MethodsInterface::HEADER . PhpInterface::OPEN_PARENTHESES . PhpInterface::QUOTES
349
            . HTTPMethodsInterface::HTTP_11 . PhpInterface::SPACE . JSONApiInterface::HTTP_RESPONSE_CODE_ACCESS_FORBIDDEN
350
            . PhpInterface::SPACE . JSONApiInterface::FORBIDDEN
351
            . PhpInterface::QUOTES . PhpInterface::CLOSE_PARENTHESES
352
            . PhpInterface::SEMICOLON . PHP_EOL;
353
        $this->setTabs(3);
354
        $this->setEchoString('Access forbidden.');
355
        $this->setTabs(3);
356
        $this->sourceCode .= PhpInterface::DIE . PhpInterface::SEMICOLON . PHP_EOL;
357
        $this->setTabs(2);
358
        $this->sourceCode .= PhpInterface::CLOSE_BRACE . PHP_EOL . PHP_EOL;
359
        $this->setMethodReturn(PhpInterface::DOLLAR_SIGN . FromRequestInterface::METHOD_PARAM_NEXT
360
            . PhpInterface::OPEN_PARENTHESES . PhpInterface::DOLLAR_SIGN . FromRequestInterface::METHOD_PARAM_REQUEST
361
            . PhpInterface::CLOSE_PARENTHESES);
362
    }
363
}