Completed
Push — develop ( 7865e0...678432 )
by
unknown
65:19
created

DoctrineFieldReader::object2View()   C

Complexity

Conditions 13
Paths 24

Size

Total Lines 41
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 13.5489

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 33
nc 24
nop 3
dl 0
loc 41
ccs 23
cts 27
cp 0.8519
crap 13.5489
rs 6.6166
c 1
b 0
f 0

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

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

101
        $property = 'get'./** @scrutinizer ignore-deprecated */ Inflector::camelize(ucfirst($field));
Loading history...
102 7
        if (method_exists($object, $property)) {
103
            return $property;
104
        }
105 7
        $property = 'get'.ucfirst($field);
106
        if (method_exists($object, $property)) {
107
            return $property;
108
        }
109
        $property = 'is'.ucfirst($field);
110
        if (method_exists($object, $property)) {
111
            return $property;
112
        }
113
        $property = 'has'.ucfirst($field);
114
        if (method_exists($object, $property)) {
115
            return $property;
116
        }
117
    }
118
}
119