NOSQLApiHelper   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 13
eloc 45
c 1
b 1
f 0
dl 0
loc 63
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C parseForm() 0 44 12
A generateId() 0 6 1
1
<?php
2
namespace NOSQL\Services\Helpers;
3
4
use NOSQL\Dto\CollectionDto;
5
use NOSQL\Services\Base\NOSQLBase;
6
use PSFS\base\dto\Field;
7
use PSFS\base\dto\Form;
8
use PSFS\base\types\helpers\ApiHelper;
9
10
/**
11
 * Class NOSQLApiHelper
12
 * @package NOSQL\Services\Helpers
13
 */
14
class NOSQLApiHelper extends ApiHelper {
15
16
    /**
17
     * @return Field
18
     * @throws \PSFS\base\exception\GeneratorException
19
     */
20
    private static function generateId() {
21
        $field = new Field('_id', t('Id'), Field::HIDDEN_TYPE);
22
        $field->readonly = true;
23
        $field->readonly = true;
24
        $field->pk = true;
25
        return $field;
26
    }
27
28
    /**
29
     * @param CollectionDto $collectionDto
30
     * @return Form
31
     * @throws \PSFS\base\exception\GeneratorException
32
     */
33
    public static function parseForm(CollectionDto $collectionDto) {
34
        $form = new Form(false);
35
        $form->addField(self::generateId());
36
        foreach($collectionDto->properties as $property) {
37
            $values = null;
38
            $data = [];
39
            $url = null;
40
            switch ($property->type) {
41
                case NOSQLBase::NOSQL_TYPE_INTEGER:
42
                case NOSQLBase::NOSQL_TYPE_DOUBLE:
43
                case NOSQLBase::NOSQL_TYPE_LONG:
44
                    $type = Field::NUMBER_TYPE;
45
                    break;
46
                case NOSQLBase::NOSQL_TYPE_BOOLEAN:
47
                    $type = Field::SWITCH_TYPE;
48
                    break;
49
                case NOSQLBase::NOSQL_TYPE_ARRAY:
50
                    $type = Field::COMBO_TYPE;
51
                    break;
52
                case NOSQLBase::NOSQL_TYPE_DATE:
53
                    $type = Field::TIMESTAMP;
54
                    break;
55
                case NOSQLBase::NOSQL_TYPE_OBJECT:
56
                    $type = Field::TEXTAREA_TYPE;
57
                    break;
58
                case NOSQLBase::NOSQL_TYPE_ENUM:
59
                    $type = Field::COMBO_TYPE;
60
                    $enumValues = explode('|', $property->enum);
0 ignored issues
show
Bug introduced by
$property->enum of type array is incompatible with the type string expected by parameter $string of explode(). ( Ignorable by Annotation )

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

60
                    $enumValues = explode('|', /** @scrutinizer ignore-type */ $property->enum);
Loading history...
61
                    foreach($enumValues as $value) {
62
                        $data[] = [
63
                            $property->name => $value,
64
                            'Label' => t($value),
65
                        ];
66
                    };
67
                    break;
68
                default:
69
                    $type = Field::TEXT_TYPE;
70
                    break;
71
            }
72
            $field = new Field($property->name, $property->description ?: $property->name, $type, $values, $data, $url, $property->required);
73
            $field->pk = false;
74
            $form->addField($field);
75
        }
76
        return $form;
77
    }
78
}