Query::getResult()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Pgs\ElasticOM;
4
5
use Pgs\ElasticOM\Hydrator\Hydrator;
6
7
final class Query
8
{
9
    /** @var Adapter */
10
    private $adapter;
11
12
    /** @var array */
13
    private $body;
14
15
    /** @var string */
16
    private $entityName;
17
18
    /** @var Hydrator */
19
    private $hydrator;
20
21 1
    public function __construct(Adapter $adapter, string $entityName, array $body, Hydrator $hydrator)
22
    {
23 1
        $this->adapter = $adapter;
24 1
        $this->body = $body;
25 1
        $this->entityName = $entityName;
26 1
        $this->hydrator = $hydrator;
27 1
    }
28
29 1
    public function getResult(): array
30
    {
31 1
        return array_map(
32 1
            function (array $data) {
33 1
                $entity = ReflectionSingleton::getInstance($this->entityName)->newInstanceWithoutConstructor();
34 1
                $entityData = array_merge(
35 1
                    $data['_source'],
36
                    [
37 1
                        $this->hydrator->getIdName($this->entityName) => $data['_id'],
38
                    ]
39
                );
40
41 1
                $this->hydrator->hydrate($entityData, $entity);
42
43 1
                return $entity;
44 1
            },
45 1
            $this->adapter->findBy($this->entityName, $this->body)['hits']['hits']
46
        );
47
    }
48
}
49