EntityRepository::find()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 10
cts 10
cp 1
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Pgs\ElasticOM;
4
5
use Elasticsearch\Common\Exceptions\Missing404Exception;
6
use Pgs\ElasticOM\Hydrator\Hydrator;
7
8
class EntityRepository
9
{
10
    /** @var Adapter */
11
    protected $adapter;
12
13
    /** @var string */
14
    protected $entityName;
15
16
    /** @var Hydrator */
17
    protected $hydrator;
18
19 4
    final public function __construct(Adapter $adapter, string $entityName, Hydrator $hydrator)
20
    {
21 4
        $this->adapter = $adapter;
22 4
        $this->entityName = $entityName;
23 4
        $this->hydrator = $hydrator;
24 4
    }
25
26 2
    public function find(string $id)
27
    {
28
        try {
29 2
            $data = $this->adapter->find($this->entityName, $id);
30 1
        } catch (Missing404Exception $e) {
31 1
            throw new Exception("$this->entityName with id '$id' not found");
32
        }
33
34 1
        $entity = ReflectionSingleton::getInstance($this->entityName)->newInstanceWithoutConstructor();
35
36 1
        $entityData = array_merge(
37 1
            $data['_source'],
38
            [
39 1
                $this->hydrator->getIdName($this->entityName) => $data['_id'],
40
            ]
41
        );
42
43 1
        $this->hydrator->hydrate($entityData, $entity);
44
45 1
        return $entity;
46
    }
47
48 1
    public function findBy(array $criteria, array $orderBy = [], int $limit = null, int $offset = null): array
49
    {
50 1
        $queryBuilder = $this->createQueryBuilder();
51
52 1
        foreach ($criteria as $key => $value) {
53 1
            $queryBuilder->setFilter($key, $value);
54
        }
55
56 1
        foreach ($orderBy as $key => $value) {
57 1
            $queryBuilder->addOrderBy($key, $value);
58
        }
59
60 1
        if ($limit !== null) {
61 1
            $queryBuilder->setMaxResults($limit);
62
        }
63
64 1
        if ($offset !== null) {
65 1
            $queryBuilder->setFirstResult($offset);
66
        }
67
68 1
        return $queryBuilder->getQuery()->getResult();
69
    }
70
71 1
    public function update($entity)
72
    {
73 1
        $property = ReflectionSingleton::getInstance(get_class($entity))
74 1
            ->getProperty($this->hydrator->getIdName($this->entityName));
75 1
        $property->setAccessible(true);
76
77 1
        $response = $this->adapter->update(
78 1
            $this->entityName,
79 1
            $this->hydrator->extract($entity),
80 1
            $property->getValue($entity)
81
        );
82
83 1
        $property->setValue($entity, $response['_id']);
84 1
    }
85
86 1
    final protected function createQueryBuilder(): QueryBuilder
87
    {
88 1
        return new QueryBuilder($this->adapter, $this->entityName, $this->hydrator);
89
    }
90
}
91