Completed
Push — master ( 5cd0eb...6996d7 )
by Simonas
65:54
created

SearchResponse   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 83
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getFilters() 0 4 1
A getResult() 0 4 1
A getUrlParameters() 0 4 1
A getSerializableData() 0 19 3
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\FilterManagerBundle\Search;
13
14
use JMS\Serializer\Serializer;
15
use ONGR\ElasticsearchBundle\Result\DocumentIterator;
16
use ONGR\FilterManagerBundle\Filter\ViewData;
17
use ONGR\FilterManagerBundle\SerializableInterface;
18
19
/**
20
 * This class holds full response of documents and filters data.
21
 */
22
class SearchResponse implements SerializableInterface
23
{
24
    /**
25
     * @var DocumentIterator Elasticsearch response object.
26
     */
27
    private $result;
28
29
    /**
30
     * @var ViewData[] View data from filters.
31
     */
32
    private $filters;
33
34
    /**
35
     * @var array Url Parameters represents current link to list state.
36
     */
37
    private $urlParameters;
38
39
    /**
40
     * @var Serializer
41
     */
42
    private $serializer;
43
44
    /**
45
     * @param ViewData[]       $filters
46
     * @param DocumentIterator $result
47
     * @param array            $urlParameters
48
     * @param Serializer       $serializer
49
     */
50
    public function __construct($filters, $result, $urlParameters, $serializer)
51
    {
52
        $this->filters = $filters;
53
        $this->result = $result;
54
        $this->urlParameters = $urlParameters;
55
        $this->serializer = $serializer;
56
    }
57
58
    /**
59
     * @return \ONGR\FilterManagerBundle\Filter\ViewData[]
60
     */
61
    public function getFilters()
62
    {
63
        return $this->filters;
64
    }
65
66
    /**
67
     * @return DocumentIterator
68
     */
69
    public function getResult()
70
    {
71
        return $this->result;
72
    }
73
74
    /**
75
     * @return array
76
     */
77
    public function getUrlParameters()
78
    {
79
        return $this->urlParameters;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getSerializableData()
86
    {
87
        $data = [
88
            'count' => $this->result->count(),
89
            'documents' => [],
90
            'filters' => [],
91
            'url_parameters' => $this->urlParameters,
92
        ];
93
94
        foreach ($this->result as $document) {
95
            $data['documents'][] = $this->serializer->toArray($document);
96
        }
97
98
        foreach ($this->filters as $name => $filter) {
99
            $data['filters'][$name] = $filter->getSerializableData();
100
        }
101
102
        return $data;
103
    }
104
}
105