DoctrineFieldReader::object2View()   C
last analyzed

Complexity

Conditions 13
Paths 24

Size

Total Lines 41
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 16.4272

Importance

Changes 0
Metric Value
cc 13
eloc 33
c 0
b 0
f 0
nc 24
nop 3
dl 0
loc 41
ccs 24
cts 33
cp 0.7272
crap 16.4272
rs 6.6166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Cdf\BiCoreBundle\Utils\Entity;
4
5
use Exception;
6
use Doctrine\Inflector\Inflector;
7
use Doctrine\Inflector\NoopWordInflector;
8
use Cdf\BiCoreBundle\Utils\FieldType\FieldTypeUtils;
9
10
class DoctrineFieldReader
11
{
12
13
    private string $tableprefix;
14
15 7
    public function __construct(string $tableprefix)
16
    {
17 7
        $this->tableprefix = $tableprefix;
18
    }
19
20
    /**
21
     *
22
     * @param string $fieldname
23
     * @param mixed $object
24
     * @param array<mixed> $decodifiche
25
     * @return mixed|null
26
     * @throws Exception
27
     */
28 7
    public function getField2Object(string $fieldname, $object, $decodifiche = null)
29
    {
30 7
        $property = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $property is dead and can be removed.
Loading history...
31 7
        $field = '';
32 7
        $propertyfound = false;
33 7
        $subfields = explode('.', str_replace($this->tableprefix, '', $fieldname));
34 7
        foreach ($subfields as $field) {
35 7
            if ($object == null) {
36 1
                return null;
37
            }
38 7
            $property = $this->getObjectProperty($field, $object);
39 7
            if ($property) {
40 7
                $object = $object->$property();
41 7
                $propertyfound = true;
42
            }
43
        }
44 7
        if (!$propertyfound) {
45
            throw new Exception('Proprietà ' . $field . ' non trovata per ' . $fieldname);
46
        }
47 7
        if ($decodifiche) {
48 1
            if (key_exists($object, $decodifiche)) {
49 1
                $object = $decodifiche[$object];
50
            }
51
        }
52
53 7
        return $object;
54
    }
55
56
    /**
57
     *
58
     * @param mixed $object
59
     * @param string $type
60
     * @param array<mixed> $decodifiche
61
     * @return mixed|null
62
     * @throws Exception
63
     */
64 7
    public function object2View($object, ?string $type = null, $decodifiche = null)
65
    {
66 7
        $risposta = null;
67
68 7
        if ($decodifiche) {
69
            $type = 'string';
70
        }
71
72 7
        if (!is_null($object)) {
73 7
            switch (strtolower($this->getObjectType($type, $object))) {
74 7
                case 'array':
75 1
                    $risposta = print_r($object, true);
76 1
                    break;
77 7
                case 'date':
78 1
                    $risposta = $object->format(FieldTypeUtils::getEnvVar("DATE_FORMAT", "d/m/Y"));
79 1
                    break;
80 7
                case 'datetime':
81 2
                    $risposta = $object->format(FieldTypeUtils::getEnvVar("DATETIME_FORMAT", "d/m/Y H:i"));
82 2
                    break;
83 7
                case 'string2datetime':
84
                    $time = strtotime($object);
85
                    $risposta = date(FieldTypeUtils::getEnvVar("DATETIME_FORMAT", "d/m/Y H:i"), $time);
86
                    break;
87 7
                case 'string2date':
88
                    $time = strtotime($object);
89
                    $risposta = date(FieldTypeUtils::getEnvVar("DATE_FORMAT", "d/m/Y"), $time);
90
                    break;
91 7
                case 'string2bool':
92
                    $risposta = $object ? 'SI' : 'NO';
93
                    break;
94 7
                case 'boolean':
95 5
                    $risposta = $object ? 'SI' : 'NO';
96 5
                    break;
97 7
                case 'string':
98
                default:
99 7
                    $risposta = $object;
100 7
                    break;
101
            }
102
        }
103
104 7
        return $risposta;
105
    }
106
107
    /**
108
     *
109
     * @param string|null $type
110
     * @param mixed $object
111
     * @return string
112
     */
113 7
    private function getObjectType(?string $type, $object)
114
    {
115 7
        if (null === $type) {
116
            $tipo = is_object($object) ?
117
                    get_class($object) :
118
                    gettype($object);
119
        } else {
120 7
            $tipo = $type;
121
        }
122
123 7
        return $tipo;
124
    }
125
126
    /**
127
     *
128
     * @param string $field
129
     * @param mixed $object
130
     * @return string|null
131
     */
132 7
    private function getObjectProperty(string $field, $object)
133
    {
134 7
        $inflector = new Inflector(new NoopWordInflector(), new NoopWordInflector());
135
136 7
        $property = 'get' . $inflector->camelize(ucfirst($field));
137 7
        if (method_exists($object, $property)) {
138 7
            return $property;
139
        }
140 7
        $property = 'get' . ucfirst($field);
141 7
        if (method_exists($object, $property)) {
142
            return $property;
143
        }
144 7
        $property = 'is' . ucfirst($field);
145 7
        if (method_exists($object, $property)) {
146 1
            return $property;
147
        }
148 7
        $property = 'has' . ucfirst($field);
149 7
        if (method_exists($object, $property)) {
150
            return $property;
151
        }
152
153 7
        return null;
154
    }
155
}
156