Passed
Push — master ( eab5c4...c49531 )
by Andrea
09:04 queued 11s
created

DoctrineFieldReader::object2View()   C

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