Completed
Push — master ( ecc9ea...9a8ba1 )
by
unknown
01:28
created

ResourceHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Managlea\Component;
6
7
8
class ResourceHandler implements ResourceHandlerInterface
9
{
10
    /**
11
     * @var EntityManagerFactoryInterface
12
     */
13
    private $entityManagerFactory;
14
    /**
15
     * @var ResourceMapperInterface
16
     */
17
    private $resourceMapper;
18
    /**
19
     * @var EntityManagerInterface
20
     */
21
    private $entityManager;
22
    /**
23
     * @var string mapped object name for resource
24
     */
25
    private $objectName;
26
27
    /**
28
     * ResourceHandler constructor.
29
     * @param EntityManagerFactoryInterface $entityManagerFactory
30
     * @param ResourceMapperInterface $resourceMapper
31
     */
32 6
    public function __construct(EntityManagerFactoryInterface $entityManagerFactory, ResourceMapperInterface $resourceMapper)
33
    {
34 6
        $this->entityManagerFactory = $entityManagerFactory;
35 6
        $this->resourceMapper = $resourceMapper;
36 6
    }
37
38
    /**
39
     * @param string $resourceName
40
     * @return EntityManagerInterface
41
     * @throws \Exception
42
     */
43 5
    private function getEntityManagerForResource(string $resourceName) : EntityManagerInterface
44
    {
45 5
        $entityManagerName = $this->resourceMapper->getEntityManagerName($resourceName);
46 5
        $entityManager = $this->entityManagerFactory->create($entityManagerName);
47
48 5
        return $entityManager;
49
    }
50
51
    /**
52
     * @param string $resourceName
53
     * @return string
54
     */
55 5
    private function getObjectNameForResource(string $resourceName) : string
56
    {
57 5
        return $this->resourceMapper->getObjectName($resourceName);
58
    }
59
60
    /**
61
     * @param string $resourceName
62
     * @throws \Exception
63
     */
64 5
    private function setup(string $resourceName)
65
    {
66 5
        $this->entityManager = $this->getEntityManagerForResource($resourceName);
67 5
        $this->objectName = $this->getObjectNameForResource($resourceName);
68 5
    }
69
70
    /**
71
     * @param string $resourceName
72
     * @param int $id
73
     * @param array|null $filters
74
     * @return mixed
75
     * @throws \Exception
76
     */
77 1
    public function getSingle(string $resourceName, int $id, array $filters = array())
78
    {
79 1
        $this->setup($resourceName);
80
81 1
        $resource = $this->entityManager->get($this->objectName, $id, $filters);
82
83 1
        return $resource;
84
    }
85
86
    /**
87
     * @param string $resourceName
88
     * @param array $filters
89
     * @param int $limit
90
     * @param int $offset
91
     * @return array
92
     */
93 1
    public function getCollection(string $resourceName, array $filters = array(), int $limit = 20, int $offset = 0) : array
94
    {
95 1
        $this->setup($resourceName);
96
97 1
        $collection = $this->entityManager->getCollection($this->objectName, $filters, $limit, $offset);
98
99 1
        return $collection;
100
    }
101
102
    /**
103
     * @param string $resourceName
104
     * @param array $data
105
     * @return mixed
106
     */
107 1
    public function postSingle(string $resourceName, array $data)
108
    {
109 1
        $this->setup($resourceName);
110
111 1
        $res = $this->entityManager->create($this->objectName, $data);
112
113 1
        return $res;
114
    }
115
116
    /**
117
     * @param string $resourceName
118
     * @param int $id
119
     * @param array $data
120
     * @return mixed
121
     */
122 1
    public function putSingle(string $resourceName, int $id, array $data)
123
    {
124 1
        $this->setup($resourceName);
125
126 1
        $res = $this->entityManager->update($this->objectName, $id, $data);
127
128 1
        return $res;
129
    }
130
131
    /**
132
     * @param string $resourceName
133
     * @param int $id
134
     * @return bool
135
     */
136 1
    public function deleteSingle(string $resourceName, int $id) : bool
137
    {
138 1
        $this->setup($resourceName);
139
140 1
        $res = $this->entityManager->delete($this->objectName, $id);
141
142 1
        return $res;
143
    }
144
}
145