1 | <?php |
||
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 | 6 | public static function initialize(EntityManagerFactoryInterface $entityManagerFactory, ResourceMapperInterface $resourceMapper) |
|
41 | |||
42 | /** |
||
43 | * @param string $resourceName |
||
44 | * @return EntityManagerInterface |
||
45 | * @throws \Exception |
||
46 | */ |
||
47 | 5 | private function getEntityManagerForResource(string $resourceName) : EntityManagerInterface |
|
48 | { |
||
49 | 5 | $entityManagerName = $this->resourceMapper->getEntityManagerName($resourceName); |
|
50 | 5 | $entityManager = $this->entityManagerFactory->create($entityManagerName); |
|
51 | |||
52 | 5 | if (!$entityManager instanceof EntityManagerInterface) { |
|
53 | 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 |
|
67 | |||
68 | /** |
||
69 | * @param string $resourceName |
||
70 | * @throws \Exception |
||
71 | */ |
||
72 | 5 | private function setup(string $resourceName) |
|
77 | |||
78 | /** |
||
79 | * @param string $resourceName |
||
80 | * @param int $id |
||
81 | * @param array|null $filters |
||
82 | * @return mixed |
||
83 | * @throws \Exception |
||
84 | */ |
||
85 | 1 | public function getSingle(string $resourceName, int $id, array $filters = array()) |
|
93 | |||
94 | /** |
||
95 | * @param string $resourceName |
||
96 | * @param array $filters |
||
97 | * @param int $limit |
||
98 | * @param int $offset |
||
99 | * @return array |
||
100 | */ |
||
101 | 1 | public function getCollection(string $resourceName, array $filters = array(), int $limit = 20, int $offset = 0) : array |
|
109 | |||
110 | /** |
||
111 | * @param string $resourceName |
||
112 | * @param array $data |
||
113 | * @return mixed |
||
114 | */ |
||
115 | 1 | public function postSingle(string $resourceName, array $data) |
|
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) |
|
138 | |||
139 | /** |
||
140 | * @param string $resourceName |
||
141 | * @param int $id |
||
142 | * @return bool |
||
143 | */ |
||
144 | 1 | public function deleteSingle(string $resourceName, int $id) : bool |
|
152 | } |
||
153 |