Completed
Push — master ( 214c62...9adc1b )
by Pavel
03:02
created

EntityRepository   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 64%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 0
loc 135
ccs 16
cts 25
cp 0.64
rs 10
c 0
b 0
f 0

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 4 1
A findOneBy() 0 6 1
A getManager() 0 4 1
A createLazyCollection() 0 4 1
A getClient() 0 4 1
A getMetadata() 0 4 1
A hydrateObject() 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 13
    public function __construct(EntityManager $manager, $className)
25
    {
26 13
        $this->manager  = $manager;
27 13
        $this->metadata = $this->manager->getClassMetadata($className);
28 13
    }
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 10
    public function getClassName()
48
    {
49 10
        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
        return $this->createLazyCollection($criteria, $orderBy, $limit, $offset)->toArray();
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
     * @param array      $criteria
107
     * @param array|null $orderBy
108
     * @param int|null   $limit
109
     * @param int|null   $offset
110
     *
111
     * @return Collection
112
     */
113 3
    public function createLazyCollection(array $criteria, array $orderBy = null, $limit = null, $offset = null)
114
    {
115 3
        return new ApiCollection($this->manager, $this->metadata, [$criteria, $orderBy, $limit, $offset]);
116
    }
117
118
    /**
119
     * @return RpcClientInterface
120
     */
121 1
    protected function getClient()
122
    {
123 1
        return $this->manager->getConfiguration()->getRegistry()->get($this->metadata->getClientName());
124
    }
125
126
    /**
127
     * @return ApiMetadata
128
     */
129 1
    protected function getMetadata()
130
    {
131 1
        return $this->metadata;
132
    }
133
134
    /**
135
     * Hydrates object from given data or merges it to already fetched object
136
     *
137
     * @param mixed $data
138
     *
139
     * @return object
140
     */
141
    protected function hydrateObject($data)
142
    {
143
        return $this->getManager()->getUnitOfWork()->getOrCreateEntity($this->getClassName(), $data);
144
    }
145
}
146