Passed
Push — master ( 18146c...908f69 )
by Andrea
16:21 queued 12s
created

ApiManagerUtil::castDateTime()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 14
rs 9.9666
ccs 0
cts 10
cp 0
crap 20
1
<?php
2
3
/**
4
 * This is an utility class in order to remove complex computations from ApiManager.
5
 */
6
7
8
namespace Cdf\BiCoreBundle\Utils\Api;
9
10
use \DateTime;
11
12
class ApiManagerUtil
13
{
14
15
    /**
16
     * Map first object transformed into the second where possible,
17
     * attempting to map each field of first into field of the second.
18
     */
19
    public function mapData(mixed $modelEntity, mixed $controllerItem): mixed
20
    {
21
        $setters = $controllerItem::setters();
22
        $getters = $modelEntity::getters();
23
24
        foreach ($setters as $setterKey => $setterMethod) {
25
            if (isset($getters[$setterKey])) {
26
                $getMethod = $getters[$setterKey];
27
                $controllerItem->$setterMethod($modelEntity->$getMethod());
28
            }
29
        }
30
31
        return $controllerItem;
32
    }
33
34
35
    /**
36
     * Return value of string when format type is datetime, otherwise null
37
     */
38
    private function castDateTime(mixed $oldvalue, ?string $formatType): ?DateTime
39
    {
40
        $value = null;
41
        if ($formatType == 'datetime') {
42
            if (!empty($oldvalue)) {
43
                $oldvalue = str_replace('/', '-', $oldvalue);
44
                $time = strtotime($oldvalue);
45
                $value = new DateTime();
46
                if ($time != false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $time of type integer to the boolean false. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
47
                    $value->setTimestamp($time);
48
                }
49
            }
50
        }
51
        return $value;
52
    }
53
54
55
    /**
56
     * Try to insert in automatic way the conversion to a BiCore known value
57
     */
58
    public function getValueOfData(?string $fieldType, ?string $formatType, ?string $oldvalue): mixed
59
    {
60
        $value = $oldvalue;
61
62
        switch ($fieldType) {
63
            case null:
64
                break;
65
            case 'int':
66
                $value = (int)$value;
67
                break;
68
            case 'double':
69
                $value = (double)$value;
70
                break;
71
            case 'bool':
72
                $value = (bool)$value;
73
                break;
74
            case 'string':
75
                $value = $this->castDateTime($formatType, $oldvalue);
76
                break;
77
        }
78
        return $value;
79
    }
80
81
    /**
82
     * It prepares entity values so that they can be used with types compliant with BiCoreBundle.
83
     * For example it transforms a date that arrive in string format into a DateTime.
84
     */
85
    public function setupApiValues(mixed $entityout): mixed
86
    {
87
        $fieldMappings = $entityout::swaggerTypes();
88
        $formatMappings = $entityout::swaggerFormats();
89
        $setters = $entityout::setters();
90
        $getters = $entityout::getters();
91
92
        foreach ($fieldMappings as $fieldName => $fieldType) {
93
                $setvalue = $setters[$fieldName];
94
                $getvalue = $getters[$fieldName];
95
                $newvalue = $this->getValueOfData($fieldType, $formatMappings[$fieldName], $entityout->$getvalue());
96
                $entityout->$setvalue($newvalue);
97
        }
98
        return $entityout;
99
    }
100
}
101