Completed
Push — master ( 7e9231...419bd7 )
by Guillaume
15:48
created

EntityManager::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Starkerxp\StructureBundle\Manager;
4
5
use Doctrine\ORM\EntityRepository;
6
use Starkerxp\StructureBundle\Entity\Entity;
7
8
class EntityManager implements ManagerInterface
9
{
10
    /**
11
     * @var array
12
     */
13
    private $arrayService = [];
14
15
    /**
16
     * @return mixed
17
     */
18
    public function insert(Entity $object)
19
    {
20
        if ($managerService = $this->getSupport($object)) {
21
            $managerService->insert($object);
22
23
            return $object;
24
        }
25
26
        return false;
27
    }
28
29
    public function update(Entity $object)
30
    {
31
        if ($managerService = $this->getSupport($object)) {
32
            $managerService->update($object);
33
34
            return $object;
35
        }
36
37
        return false;
38
    }
39
40
41
    public function delete(Entity $object)
42
    {
43
        if ($managerService = $this->getSupport($object)) {
44
            $managerService->delete($object);
45
46
            return $object;
47
        }
48
49
        return false;
50
    }
51
52
    /**
53
     * @param Entity $object
54
     *
55
     * @return bool|ManagerInterface
56
     */
57
    public function getSupport(Entity $object)
58
    {
59
        foreach ($this->arrayService as $service) {
60
            if ($service instanceof ManagerInterface && $service->getSupport($object)) {
61
                return $service;
62
            }
63
        }
64
65
        return false;
66
    }
67
68
    /**
69
     * @param ManagerInterface $service
70
     *
71
     * @return $this
72
     */
73
    public function addService(ManagerInterface $service)
74
    {
75
        $this->arrayService[] = $service;
76
77
        return $this;
78
    }
79
80
    /**
81
     * @return array
82
     */
83
    public function getServices()
84
    {
85
        return $this->arrayService;
86
    }
87
88
    /**
89
     * @param Entity $object
90
     *
91
     * @return bool|AbstractManager
92
     */
93
    public function getManager(Entity $object)
94
    {
95
        $managerService = $this->getSupport($object);
96
97
        return $managerService;
98
    }
99
100
    /**
101
     * @param Entity $object
102
     *
103
     * @return bool|EntityRepository
104
     */
105
    public function getRepository(Entity $object){
106
        if(!$manager = $this->getManager( $object)){
107
            return false;
108
        }
109
        return $manager->getRepository();
110
    }
111
}
112