|
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
|
|
|
|