EntityUtility::getEntityProperties()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.9332
cc 4
nc 4
nop 2
crap 4
1
<?php
2
3
namespace Fi\CoreBundle\DependencyInjection;
4
5
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
6
use Doctrine\Common\Persistence\Proxy;
7
8
class EntityUtility
9
{
10
11
    private $container;
12
    private $em;
13
14 2
    public function __construct(Container $container)
15
    {
16 2
        $this->container = $container;
17 2
        $this->em = $this->container->get("doctrine")->getManager();
18 2
    }
19
20 1
    public function getTableFromEntity($entity)
21
    {
22 1
        $metadata = $this->em->getClassMetadata($entity);
23 1
        $tablename = $metadata->table["name"];
24
25 1
        return $tablename;
26
    }
27
28
    public function entityHasJoinTables($entityclass)
29
    {
30
        $jointables = $this->getEntityJoinTables($entityclass);
31
        return count($jointables) > 0 ? true : false;
32
    }
33
34 2
    public function getJoinTableField($entityclass, $field)
35
    {
36 2
        $joinfields = $this->getEntityJoinTables($entityclass);
37 2
        foreach ($joinfields as $joinfield) {
38 2
            if (count($joinfield) != 1) {
39
                return null;
40
            }
41 2
            $jointableentity = $this->getJoinTable($joinfield, $field);
42 2
            if ($jointableentity) {
43 2
                return $jointableentity;
44
            }
45
        }
46 2
        return null;
47
    }
48
49 2
    public function getJoinTableFieldProperty($entityclass, $field)
50
    {
51 2
        $joinfields = $this->getEntityJoinTables($entityclass);
52 2
        foreach ($joinfields as $joinfield) {
53 2
            if (count($joinfield) != 1) {
54
                return null;
55
            }
56 2
            $joinfieldname = $this->getJoinFieldName($joinfield, $field);
57 2
            if ($joinfieldname) {
58 2
                return $joinfieldname;
59
            }
60
        }
61 2
        return null;
62
    }
63
64 2
    public function getEntityProperties($fieldname, $objrecord)
65
    {
66 2
        $parametri = array('str' => $fieldname, 'primamaiuscola' => true);
67 2
        $getfieldname = \Fi\CoreBundle\Utils\GrigliaUtils::toCamelCase($parametri);
68 2
        if (!method_exists($objrecord, $getfieldname)) {
69 2
            $getfieldname = "has" . \Fi\CoreBundle\Utils\GrigliaUtils::toCamelCase($parametri);
70 2
            if (!method_exists($objrecord, $getfieldname)) {
71 2
                $getfieldname = "is" . \Fi\CoreBundle\Utils\GrigliaUtils::toCamelCase($parametri);
72 2
                if (!method_exists($objrecord, $getfieldname)) {
73 2
                    $getfieldname = "get" . \Fi\CoreBundle\Utils\GrigliaUtils::toCamelCase($parametri);
74
                }
75
            }
76
        }
77 2
        $setfieldname = "set" . \Fi\CoreBundle\Utils\GrigliaUtils::toCamelCase($parametri);
78
79 2
        return array("get" => $getfieldname, "set" => $setfieldname);
80
    }
81
82 1
    public function entityExists($className)
83
    {
84
85 1
        if (is_object($className)) {
86
            $className = ($className instanceof Proxy) ? get_parent_class($className) : get_class($className);
87
        }
88
89 1
        return !$this->em->getMetadataFactory()->isTransient($className);
90
    }
91
92 2
    public function getEntityJoinTables($entityclass)
93
    {
94 2
        $jointables = array();
95 2
        $metadata = $this->em->getClassMetadata($entityclass);
96 2
        $fielsassoc = $metadata->associationMappings;
97 2
        foreach ($fielsassoc as $tableassoc) {
98 2
            if ($tableassoc["inversedBy"]) {
99 2
                $jointables[$tableassoc["targetEntity"]] = array("entity" => $tableassoc);
100
            }
101
        }
102 2
        return $jointables;
103
    }
104
105 2
    private function getJoinFieldName($joinfield, $field)
106
    {
107 2
        $joinFieldentity = $joinfield["entity"];
108 2
        $joinColumns = $joinFieldentity["joinColumns"];
109 2
        foreach ($joinColumns as $joinColumn) {
110 2
            if ($field === $joinColumn["name"]) {
111 2
                $joinFieldName = $joinFieldentity["fieldName"];
112 2
                return $joinFieldName;
113
            }
114
        }
115 2
        return null;
116
    }
117
118 2
    private function getJoinTable($joinfield, $field)
119
    {
120 2
        $joinTableEntity = $joinfield["entity"];
121 2
        $joinColumns = $joinTableEntity["joinColumns"];
122 2
        foreach ($joinColumns as $joinColumn) {
123 2
            if ($field === $joinColumn["name"]) {
124 2
                return $joinTableEntity["targetEntity"];
125
            }
126
        }
127 2
        return null;
128
    }
129
}
130