1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bankiru\Api\Doctrine; |
4
|
|
|
|
5
|
|
|
use Bankiru\Api\Doctrine\Mapping\ApiMetadata; |
6
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
7
|
|
|
use ScayTrase\Api\Rpc\RpcClientInterface; |
8
|
|
|
|
9
|
|
|
class EntityRepository implements ObjectRepository |
10
|
|
|
{ |
11
|
|
|
/** @var ApiMetadata */ |
12
|
|
|
private $metadata; |
13
|
|
|
/** @var EntityManager */ |
14
|
|
|
private $manager; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* EntityRepository constructor. |
18
|
|
|
* |
19
|
|
|
* @param EntityManager $manager |
20
|
|
|
* @param string $className |
21
|
|
|
*/ |
22
|
16 |
|
public function __construct(EntityManager $manager, $className) |
23
|
|
|
{ |
24
|
16 |
|
$this->manager = $manager; |
25
|
16 |
|
$this->metadata = $this->manager->getClassMetadata($className); |
26
|
16 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Finds an object by its primary key / identifier. |
30
|
|
|
* |
31
|
|
|
* @param mixed $id The identifier. |
32
|
|
|
* |
33
|
|
|
* @return object The object. |
34
|
|
|
*/ |
35
|
11 |
|
public function find($id) |
36
|
|
|
{ |
37
|
11 |
|
return $this->manager->find($this->getClassName(), $id); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns the class name of the object managed by the repository. |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
15 |
|
public function getClassName() |
46
|
|
|
{ |
47
|
15 |
|
return $this->metadata->getReflectionClass()->getName(); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Finds all objects in the repository. |
52
|
|
|
* |
53
|
|
|
* @return array The objects. |
54
|
|
|
*/ |
55
|
2 |
|
public function findAll() |
56
|
|
|
{ |
57
|
2 |
|
return $this->findBy([]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Finds objects by a set of criteria. |
62
|
|
|
* |
63
|
|
|
* Optionally sorting and limiting details can be passed. An implementation may throw |
64
|
|
|
* an UnexpectedValueException if certain values of the sorting or limiting details are |
65
|
|
|
* not supported. |
66
|
|
|
* |
67
|
|
|
* @param array $criteria |
68
|
|
|
* @param array|null $orderBy |
69
|
|
|
* @param int|null $limit |
70
|
|
|
* @param int|null $offset |
71
|
|
|
* |
72
|
|
|
* @return array The objects. |
73
|
|
|
* |
74
|
|
|
* @throws \UnexpectedValueException |
75
|
|
|
*/ |
76
|
5 |
|
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
77
|
|
|
{ |
78
|
5 |
|
$persister = $this->manager->getUnitOfWork()->getEntityPersister($this->getClassName()); |
79
|
|
|
|
80
|
5 |
|
return $persister->loadAll($criteria, $orderBy, $limit, $offset); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Finds a single object by a set of criteria. |
85
|
|
|
* |
86
|
|
|
* @param array $criteria The criteria. |
87
|
|
|
* |
88
|
|
|
* @return object The object. |
89
|
|
|
*/ |
90
|
|
|
public function findOneBy(array $criteria) |
91
|
|
|
{ |
92
|
|
|
$objects = $this->findBy($criteria, [], 1); |
93
|
|
|
|
94
|
|
|
return array_shift($objects); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return EntityManager |
99
|
|
|
*/ |
100
|
|
|
public function getManager() |
101
|
|
|
{ |
102
|
|
|
return $this->manager; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return RpcClientInterface |
107
|
|
|
*/ |
108
|
1 |
|
protected function getClient() |
109
|
|
|
{ |
110
|
1 |
|
return $this->manager->getConfiguration()->getClientRegistry()->get($this->metadata->getClientName()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return ApiMetadata |
115
|
|
|
*/ |
116
|
1 |
|
protected function getMetadata() |
117
|
|
|
{ |
118
|
1 |
|
return $this->metadata; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Hydrates object from given data or merges it to already fetched object |
123
|
|
|
* |
124
|
|
|
* @param mixed $data |
125
|
|
|
* |
126
|
|
|
* @return object |
127
|
|
|
*/ |
128
|
1 |
|
protected function hydrateObject($data) |
129
|
|
|
{ |
130
|
1 |
|
return $this->getManager()->getUnitOfWork()->getOrCreateEntity($this->getClassName(), $data); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $alias |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
* @throws \OutOfBoundsException if no method exist |
138
|
|
|
*/ |
139
|
|
|
protected function getClientMethod($alias) |
140
|
|
|
{ |
141
|
|
|
return $this->getMetadata()->getMethodContainer()->getMethod($alias); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|