-
Notifications
You must be signed in to change notification settings - Fork 1
/
EntityRepository.php
127 lines (109 loc) · 3.14 KB
/
EntityRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace Bankiru\Api\Doctrine;
use Bankiru\Api\Doctrine\Mapping\ApiMetadata;
use Bankiru\Api\Doctrine\Persister\CollectionMatcher;
use Bankiru\Api\Doctrine\Proxy\LazyCriteriaCollection;
use Bankiru\Api\Doctrine\Rpc\CrudsApiInterface;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Selectable;
use Doctrine\Common\Persistence\ObjectRepository;
use ScayTrase\Api\Rpc\RpcClientInterface;
class EntityRepository implements ObjectRepository, Selectable
{
/** @var ApiMetadata */
private $metadata;
/** @var EntityManager */
private $manager;
/**
* EntityRepository constructor.
*
* @param EntityManager $manager
* @param string $className
*/
public function __construct(EntityManager $manager, $className)
{
$this->manager = $manager;
$this->metadata = $this->manager->getClassMetadata($className);
}
/** {@inheritdoc} */
public function find($id)
{
return $this->manager->find($this->getClassName(), $id);
}
/** {@inheritdoc} */
public function getClassName()
{
return $this->metadata->getReflectionClass()->getName();
}
/** {@inheritdoc} */
public function findAll()
{
return $this->findBy([]);
}
/** {@inheritdoc} */
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
{
$persister = $this->manager->getUnitOfWork()->getEntityPersister($this->getClassName());
return $persister->loadAll($criteria, $orderBy, $limit, $offset);
}
/** {@inheritdoc} */
public function findOneBy(array $criteria)
{
$objects = $this->findBy($criteria, [], 1);
return array_shift($objects);
}
/**
* @return EntityManager
*/
public function getManager()
{
return $this->manager;
}
/** {@inheritdoc} */
public function matching(Criteria $criteria)
{
return new LazyCriteriaCollection(new CollectionMatcher($this->getManager(), $this->getApi()), $criteria);
}
/**
* @return RpcClientInterface
*/
protected function getClient()
{
return $this->manager->getConfiguration()->getClientRegistry()->get($this->metadata->getClientName());
}
/**
* @return CrudsApiInterface
*/
protected function getApi()
{
return $this->manager->getUnitOfWork()->getEntityPersister($this->metadata->getName())->getCrudsApi();
}
/**
* @return ApiMetadata
*/
protected function getMetadata()
{
return $this->metadata;
}
/**
* Hydrates object from given data or merges it to already fetched object
*
* @param mixed $data
*
* @return object
*/
protected function hydrateObject($data)
{
return $this->getManager()->getUnitOfWork()->getOrCreateEntity($this->getClassName(), $data);
}
/**
* @param string $alias
*
* @return string
* @throws \OutOfBoundsException if no method exist
*/
protected function getClientMethod($alias)
{
return $this->getMetadata()->getMethodContainer()->getMethod($alias);
}
}