Completed
Pull Request — master (#14)
by Pavel
04:24
created

EntityRepository   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 57.69%

Importance

Changes 0
Metric Value
dl 0
loc 135
c 0
b 0
f 0
wmc 11
lcom 1
cbo 7
ccs 15
cts 26
cp 0.5769
rs 10

11 Methods

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