Completed
Pull Request — master (#663)
by
unknown
02:22
created

InnerHitValue::getCount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 8
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchBundle\Result;
13
use ONGR\ElasticsearchBundle\Service\Manager;
14
15
/**
16
 * This is the class for plain aggregation result with nested aggregations support.
17
 */
18
class InnerHitValue
19
{
20
    /**
21
     * @var array
22
     */
23
    private $rawData;
24
25
    /**
26
     * @var Manager
27
     */
28
    private $manager;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param array   $rawData
34
     * @param Manager $manager
35
     */
36
    public function __construct($rawData, Manager $manager)
37
    {
38
        $this->rawData = $rawData;
39
        $this->manager = $manager;
40
    }
41
42
    /**
43
     * Returns array of specific inner hit objects
44
     *
45
     * @param string $name
46
     *
47
     * @return object[]
48
     */
49
    public function getValue($name)
50
    {
51
        if (!isset($this->rawData[$name])) {
52
            return null;
53
        }
54
55
        $hits = [];
56
57
        foreach ($this->rawData[$name]['hits']['hits'] as $hit) {
58
            $metadata = $this->manager
59
                ->getMetadataCollector()
60
                ->getMappings(
61
                    $this->manager->getConfig()['mappings']
62
                )[$hit['_type']]['aliases'][$hit['_nested']['field']];
63
            $object = $this->manager->getConverter()->assignArrayToObject(
64
                $hit['_source'],
65
                new $metadata['namespace'],
66
                $metadata['aliases']
67
            );
68
            $hits[] = $object;
69
        };
70
71
        return $hits;
72
    }
73
74
    /**
75
     * Returns the count of inner hits for a specific hit
76
     *
77
     * @param string $name
78
     *
79
     * @return integer
80
     */
81 View Code Duplication
    public function getCount($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
    {
83
        if (!isset($this->rawData[$name])) {
84
            return null;
85
        }
86
87
        return $this->rawData[$name]['hits']['total'];
88
    }
89
90
    /**
91
     * Returns inner hits for a specified inner hit
92
     *
93
     * @param string $name
94
     *
95
     * @return InnerHitValue[]|null
96
     */
97
    public function getInnerHits($name)
98
    {
99
        if (!isset($this->rawData[$name][0]['inner_hits'])) {
100
            return null;
101
        }
102
103
        $hits = [];
104
105
        foreach ($this->rawData[$name] as $hit) {
106
            $hits[] = new self($hit['inner_hits'], $this->manager);
107
        }
108
109
        return $hits;
110
    }
111
}
112