Passed
Push — master ( a74354...254a60 )
by Andrea
18:29 queued 11s
created

ModelUtils::getAttributes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 1
dl 0
loc 16
ccs 0
cts 12
cp 0
crap 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Cdf\BiCoreBundle\Utils\Entity;
4
5
use Cdf\BiCoreBundle\Utils\String\StringUtils;
6
use Cdf\BiCoreBundle\Utils\Api\ApiUtils;
7
use App\ApiModels\ModelsClaimExt;
0 ignored issues
show
Bug introduced by
The type App\ApiModels\ModelsClaimExt 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 Swagger\Insurance\Model\ModelsEvent;
0 ignored issues
show
Bug introduced by
The type Swagger\Insurance\Model\ModelsEvent 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...
9
use DateTime;
10
use function count;
11
12
class ModelUtils
13
{
14
15
    /**
16
     * Return the array of types and formats
17
     */
18
    public function getAttributes($controllerItem): array
19
    {
20
        $myInstance = new $controllerItem();
21
        $fieldMappings = $myInstance::swaggerTypes();
22
        $formatMappings = $myInstance::swaggerFormats();
23
        $outcomes = array();
24
        foreach ($fieldMappings as $fieldName => $fieldType) {
25
            if (\str_contains($fieldType, 'Swagger')) {
26
                $fieldName .= '_enum';
27
                $outcomes[$fieldName]['format'] = 'int';
28
            } else {
29
                $outcomes[$fieldName]['format'] = $formatMappings[$fieldName];
30
            }
31
            $outcomes[$fieldName]['type'] = $fieldType;
32
        }
33
        return $outcomes;
34
    }
35
36
    /**
37
     * Return the entity columns for the grid display
38
     */
39
    public function getEntityColumns($entity)
40
    {
41
        $myInstance = new $entity();
42
        $fieldMappings = $myInstance::swaggerTypes();
43
        $formatMappings = $myInstance::swaggerFormats();
44
45
        //dump($fieldMappings);
46
        $colonne = array();
47
        foreach ($fieldMappings as $fieldName => $fieldType) {
48
            if (\str_contains($fieldName, 'NOTenum')) {
49
                //dump("Trovato enum ".$fieldName);
50
            } else {
51
                $colonne[$fieldName]['fieldName'] = $fieldName;
52
                //$colonne[$fieldName]['type'] = $formatMappings[$fieldName];
53
                $colonne[$fieldName]['type'] = $this->getTypeOfData($fieldType, $formatMappings[$fieldName]);
54
                $colonne[$fieldName]['entityClass'] = $entity;
55
                $colonne[$fieldName]['columnName'] = $fieldName;
56
                if ($fieldName == 'id') {
57
                    $colonne[$fieldName]['id'] = true;
58
                } else {
59
                    $colonne[$fieldName]['id'] = false;
60
                }
61
            }
62
        }
63
        //dump("colonne");
64
        return $colonne;
65
    }
66
67
68
    /**
69
     * Try to insert in automatic way the conversion to a BiCore known type
70
     */
71
    private function getTypeOfData($fieldType, $formatType): String
72
    {
73
        $type = $formatType;
74
        if ($fieldType == 'bool') {
75
            $type = 'string2bool';
76
        } elseif ($formatType == null) {
77
            $type = 'string';
78
        } elseif ($fieldType != $formatType) {
79
            if ($formatType == 'datetime') {
80
                $type = 'string2'.$type;
81
            }
82
        }
83
        return $type;
84
    }
85
86
    /**
87
     * It prepares entity values so that they can be used with types compliant with BiCoreBundle.
88
     * For example it transforms a date that arrive in string format into a DateTime.
89
     */
90
    public function setApiValues($entityout)
91
    {
92
    
93
        $fieldMappings = $entityout::swaggerTypes();
94
        $formatMappings = $entityout::swaggerFormats();
95
        $setters = $entityout::setters();
96
        $getters = $entityout::getters();
97
98
        foreach ($fieldMappings as $fieldName => $fieldType) {
99
            /*if ( \str_contains( $fieldType ,'Swagger') ) {
100
                $setvalue = $setters[$fieldName];
101
                $getvalue = $getters[$fieldName];
102
            }
103
            else {*/
104
                $setvalue = $setters[$fieldName];
105
                $getvalue = $getters[$fieldName];
106
                $newvalue = $this->getValueOfData($fieldType, $formatMappings[$fieldName], $entityout->$getvalue());
107
                $entityout->$setvalue($newvalue);
108
            /*}*/
109
        }
110
        return $entityout;
111
    }
112
113
    public function getControllerItem($modelEntity, $controllerItemClass)
114
    {
115
        $controllerItem = new $controllerItemClass();
116
        $setters = $controllerItem::setters();
117
        $getters = $modelEntity::getters();
118
119
        foreach ($setters as $setterKey => $setterMethod) {
120
            if (isset($getters[$setterKey])) {
121
                $getMethod = $getters[$setterKey];
122
                $controllerItem->$setterMethod($modelEntity->$getMethod());
123
            }
124
        }
125
126
        return $controllerItem;
127
    }
128
129
    /**
130
     * Try to insert in automatic way the conversion to a BiCore known value
131
     */
132
    private function getValueOfData($fieldType, $formatType, $oldvalue)
133
    {
134
        $value = $oldvalue;
135
        if ($formatType == null) {
136
        } elseif ($fieldType != $formatType) {
137
            if ($formatType == 'datetime') {
138
                $time = strtotime($oldvalue);
139
                $value = new DateTime();
140
                $value->setTimestamp($time);
141
            }
142
        }
143
        return $value;
144
    }
145
}
146