Completed
Pull Request — master (#18)
by
unknown
05:14
created

DoctrineEntityManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Managlea\Component\EntityManager;
6
7
8
use Doctrine\ORM\Decorator\EntityManagerDecorator;
9
use Doctrine\ORM\EntityManagerInterface as DoctrineEntityManagerInterface;
10
use Managlea\Component\EntityManagerInterface;
11
12
class DoctrineEntityManager extends EntityManagerDecorator implements EntityManagerInterface
13
{
14
    /**
15
     * DoctrineEntityManager constructor.
16
     * @param DoctrineEntityManagerInterface $wrapped
17
     */
18
    public function __construct(DoctrineEntityManagerInterface $wrapped)
19 14
    {
20
        parent::__construct($wrapped);
21 14
    }
22
23
    /**
24
     * {@inheritDoc}
25
     */
26
    public function get(string $objectName, int $id, array $criteria = array())
27 6
    {
28
        $repository = $this->getRepository($objectName);
29 6
30
        $criteria['id'] = $id;
31 5
        $entityObject = $repository->findOneBy($criteria);
32 5
33
        if (!$entityObject) {
34 5
            return false;
35 3
        }
36
37
        return $entityObject;
38 4
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function getCollection(string $objectName, array $filters = array(), int $limit = 20, int $offset = 0, array $order = null) : array
44 6
    {
45
        $repository = $this->getRepository($objectName);
46 6
        $collection = $repository->findBy($filters, $order, $limit, $offset);
47 5
48
        return $collection;
49 5
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function create(string $objectName, array $data)
55 2
    {
56
        $detachedEntity = self::createDetachedEntity($objectName, $data);
57 2
58
        $entity = $this->merge($detachedEntity);
59 2
        $this->persist($entity);
60 2
        $this->flush();
61 2
62
        return $entity;
63 2
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68
    public function update(string $objectName, int $id, array $data)
69 1
    {
70
        return $this->executeActionOnEntity('update', $objectName, $id, $data);
71 1
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    public function delete(string $objectName, int $id) : bool
77 1
    {
78
        return (bool)$this->executeActionOnEntity('remove', $objectName, $id);
79 1
    }
80
81
    /**
82
     * @param string $method
83
     * @param string $objectName
84
     * @param int $id
85
     * @param array $data
86
     * @return mixed
87
     */
88
    private function executeActionOnEntity(string $method, string $objectName, int $id, array $data = null)
89 2
    {
90
        $entityObject = $this->get($objectName, $id);
91 2
92
        if (!$entityObject) {
93 2
            return false;
94 2
        }
95
96
        if ($method == 'update' && is_array($data)) {
97 2
            self::updateEntityFromArray($entityObject, $data);
98 1
        } elseif ($method == 'remove') {
99 1
            $this->remove($entityObject);
100 1
        }
101
102
        $this->flush();
103 2
104
        if ($method == 'update') {
105 2
            return $entityObject;
106 1
        }
107
108
        return true;
109 1
    }
110
111
    /**
112
     * Creates new entity based on object name sets object
113
     * parameter from data
114
     * @param string $objectName
115
     * @param array $data
116
     * @return mixed
117
     */
118
    public static function createDetachedEntity(string $objectName, array $data)
119 3
    {
120
        $detachedEntity = new $objectName;
121 3
122
        self::updateEntityFromArray($detachedEntity, $data);
123 3
124
        return $detachedEntity;
125 3
    }
126
127
    /**
128
     * Updates Entity from data calling setters based
129
     * on data keys
130
     * @param object $entity
131
     * @param array $data
132
     * @return mixed
133
     */
134
    public static function updateEntityFromArray($entity, array $data)
135 4
    {
136
        foreach ($data as $field => $value) {
137 4
            $method = 'set' . self::formatStringToMethodName($field);
138 4
            if (method_exists($entity, $method)) {
139 4
                $entity->$method($value);
140 4
            }
141
        }
142
143
        return $entity;
144 4
    }
145
146
    /**
147
     * Generates camel-case method names from string
148
     * @param string $input
149
     * @return string
150
     */
151
    public static function formatStringToMethodName(string $input) : string
152 4
    {
153
        $methodName = implode('', array_map('ucfirst', explode('_', strtolower($input))));
154 4
155
        return $methodName;
156 4
    }
157
}
158