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
|
|
|
* @param EntityManagerFactoryInterface $entityManagerFactory |
29
|
|
|
* @param ResourceMapperInterface $resourceMapper |
30
|
|
|
* @return ResourceHandlerInterface |
31
|
|
|
*/ |
32
|
7 |
|
public static function initialize(EntityManagerFactoryInterface $entityManagerFactory, ResourceMapperInterface $resourceMapper) |
33
|
|
|
{ |
34
|
7 |
|
$resourceHandler = new self(); |
35
|
|
|
|
36
|
7 |
|
$resourceHandler->entityManagerFactory = $entityManagerFactory; |
37
|
7 |
|
$resourceHandler->resourceMapper = $resourceMapper; |
38
|
|
|
|
39
|
7 |
|
return $resourceHandler; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $resourceName |
44
|
|
|
* @return EntityManagerInterface |
45
|
|
|
* @throws \Exception |
46
|
|
|
*/ |
47
|
6 |
|
private function getEntityManagerForResource(string $resourceName) : EntityManagerInterface |
48
|
|
|
{ |
49
|
6 |
|
$entityManagerName = $this->resourceMapper->getEntityManagerName($resourceName); |
50
|
6 |
|
$entityManager = $this->entityManagerFactory->create($entityManagerName); |
51
|
|
|
|
52
|
6 |
|
if (!$entityManager instanceof EntityManagerInterface) { |
53
|
1 |
|
throw new \Exception('Entity manager not instance of EntityManagerInterface'); |
54
|
|
|
} |
55
|
|
|
|
56
|
5 |
|
return $entityManager; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $resourceName |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
5 |
|
private function getObjectNameForResource(string $resourceName) : string |
64
|
|
|
{ |
65
|
5 |
|
return $this->resourceMapper->getObjectName($resourceName); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $resourceName |
70
|
|
|
* @throws \Exception |
71
|
|
|
*/ |
72
|
6 |
|
private function setup(string $resourceName) |
73
|
|
|
{ |
74
|
6 |
|
$this->entityManager = $this->getEntityManagerForResource($resourceName); |
75
|
5 |
|
$this->objectName = $this->getObjectNameForResource($resourceName); |
76
|
5 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $resourceName |
80
|
|
|
* @param int $id |
81
|
|
|
* @param array|null $filters |
82
|
|
|
* @return mixed |
83
|
|
|
* @throws \Exception |
84
|
|
|
*/ |
85
|
2 |
|
public function getSingle(string $resourceName, int $id, array $filters = array()) |
86
|
|
|
{ |
87
|
2 |
|
$this->setup($resourceName); |
88
|
|
|
|
89
|
1 |
|
$resource = $this->entityManager->get($this->objectName, $id, $filters); |
90
|
|
|
|
91
|
1 |
|
return $resource; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $resourceName |
96
|
|
|
* @param array $filters |
97
|
|
|
* @param int $limit |
98
|
|
|
* @param int $offset |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
1 |
|
public function getCollection(string $resourceName, array $filters = array(), int $limit = 20, int $offset = 0) |
102
|
|
|
{ |
103
|
1 |
|
$this->setup($resourceName); |
104
|
|
|
|
105
|
1 |
|
$collection = $this->entityManager->getCollection($this->objectName, $filters, $limit, $offset); |
106
|
|
|
|
107
|
1 |
|
return $collection; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $resourceName |
112
|
|
|
* @param array $data |
113
|
|
|
* @return mixed |
114
|
|
|
*/ |
115
|
1 |
|
public function postSingle(string $resourceName, array $data) |
116
|
|
|
{ |
117
|
1 |
|
$this->setup($resourceName); |
118
|
|
|
|
119
|
1 |
|
$res = $this->entityManager->create($this->objectName, $data); |
120
|
|
|
|
121
|
1 |
|
return $res; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $resourceName |
126
|
|
|
* @param int $id |
127
|
|
|
* @param array $data |
128
|
|
|
* @return mixed |
129
|
|
|
*/ |
130
|
1 |
|
public function putSingle(string $resourceName, int $id, array $data) |
131
|
|
|
{ |
132
|
1 |
|
$this->setup($resourceName); |
133
|
|
|
|
134
|
1 |
|
$res = $this->entityManager->update($this->objectName, $id, $data); |
135
|
|
|
|
136
|
1 |
|
return $res; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $resourceName |
141
|
|
|
* @param int $id |
142
|
|
|
* @return mixed |
143
|
|
|
*/ |
144
|
1 |
|
public function deleteSingle(string $resourceName, int $id) |
145
|
|
|
{ |
146
|
1 |
|
$this->setup($resourceName); |
147
|
|
|
|
148
|
1 |
|
$res = $this->entityManager->delete($this->objectName, $id); |
149
|
|
|
|
150
|
1 |
|
return $res; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|