Passed
Push — master ( 8ecff8...4c1113 )
by Andrea
08:27 queued 11s
created

DoctrineFieldReader::object2View()   B

Complexity

Conditions 9
Paths 16

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 9.0076

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 22
c 1
b 0
f 0
nc 16
nop 3
dl 0
loc 30
ccs 21
cts 22
cp 0.9545
crap 9.0076
rs 8.0555
1
<?php
2
3
namespace Cdf\BiCoreBundle\Utils\Entity;
4
5
use Exception;
6
use Doctrine\Common\Util\Inflector;
7
8
class DoctrineFieldReader
9
{
10
    private $tableprefix;
11
12 7
    public function __construct($tableprefix)
13
    {
14 7
        $this->tableprefix = $tableprefix;
15 7
    }
16
17 7
    public function getField2Object($fieldname, $object, $decodifiche = null)
18
    {
19 7
        $property = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $property is dead and can be removed.
Loading history...
20 7
        $field = '';
21 7
        $propertyfound = false;
22 7
        $subfields = explode('.', str_replace($this->tableprefix, '', $fieldname));
23 7
        foreach ($subfields as $field) {
24 7
            $property = $this->getObjectProperty($field, $object);
25 7
            if ($property) {
26 7
                $object = $object->$property();
27 7
                $propertyfound = true;
28
            }
29
        }
30 7
        if (!$propertyfound) {
31
            throw new Exception('Proprietà '.$field.' non trovata per '.$fieldname);
32
        }
33 7
        if ($decodifiche) {
34
            if (key_exists($object, $decodifiche)) {
35
                $object = $decodifiche[$object];
36
            }
37
        }
38
39 7
        return $object;
40
    }
41
42 7
    public function object2View($object, $type = null, $decodifiche = null)
43
    {
44 7
        $risposta = null;
45
46 7
        if ($decodifiche) {
47
            $type = 'string';
48
        }
49
50 7
        if (!is_null($object)) {
51 7
            switch (strtolower($this->getObjectType($type, $object))) {
52 7
                case 'array':
53 1
                    $risposta = print_r($object, true);
54 1
                    break;
55 7
                case 'date':
56 1
                    $risposta = $object->format('d/m/Y');
57 1
                    break;
58 7
                case 'datetime':
59 2
                    $risposta = $object->format('d/m/Y H:i');
60 2
                    break;
61 7
                case 'boolean':
62 5
                    $risposta = $object ? 'SI' : 'NO';
63 5
                    break;
64 7
                case 'string':
65
                default:
66 7
                    $risposta = $object;
67 7
                    break;
68
            }
69
        }
70
71 7
        return $risposta;
72
    }
73
74 7
    private function getObjectType($type, $object)
75
    {
76 7
        if (null === $type) {
77
            $tipo = is_object($object) ?
78
                    get_class($object) :
79
                    gettype($object);
80
        } else {
81 7
            $tipo = $type;
82
        }
83
84 7
        return $tipo;
85
    }
86
87 7
    private function getObjectProperty($field, $object)
88
    {
89 7
        $inf = new Inflector();
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Util\Inflector has been deprecated: The Inflector class is deprecated, use Doctrine\Common\Inflector\Inflector from doctrine/inflector package instead, ( Ignorable by Annotation )

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

89
        $inf = /** @scrutinizer ignore-deprecated */ new Inflector();
Loading history...
90 7
        $property = 'get'.$inf->camelize(ucfirst($field));
91 7
        if (method_exists($object, $property)) {
92 7
            return $property;
93
        }
94 7
        $property = 'get'.ucfirst($field);
95 7
        if (method_exists($object, $property)) {
96
            return $property;
97
        }
98 7
        $property = 'is'.ucfirst($field);
99 7
        if (method_exists($object, $property)) {
100 1
            return $property;
101
        }
102 7
        $property = 'has'.ucfirst($field);
103 7
        if (method_exists($object, $property)) {
104
            return $property;
105
        }
106 7
    }
107
}
108