Completed
Pull Request — master (#186)
by
unknown
64:03
created

Pager::preProcessSearch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 1
nc 1
nop 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\Filter\Widget\Pager;
13
14
use ONGR\ElasticsearchDSL\Search;
15
use ONGR\ElasticsearchBundle\Result\DocumentIterator;
16
use ONGR\FilterManagerBundle\Filter\FilterInterface;
17
use ONGR\FilterManagerBundle\Filter\FilterState;
18
use ONGR\FilterManagerBundle\Filter\Helper\ViewDataFactoryInterface;
19
use ONGR\FilterManagerBundle\Filter\ViewData;
20
use ONGR\FilterManagerBundle\Filter\Widget\AbstractSingleRequestValueFilter;
21
use ONGR\FilterManagerBundle\Search\SearchRequest;
22
use ONGR\FilterManagerBundle\Pager\PagerService;
23
use ONGR\FilterManagerBundle\Pager\Adapters\CountAdapter;
24
use Symfony\Component\HttpFoundation\Request;
25
26
/**
27
 * Provides basic functionality for pagination.
28
 */
29
class Pager extends AbstractSingleRequestValueFilter implements FilterInterface, ViewDataFactoryInterface
30
{
31
    /**
32
     * @var int
33
     */
34
    private $countPerPage;
35
36
    /**
37
     * @var int
38
     */
39
    private $maxPages;
40
41
    /**
42
     * @return int
43
     */
44
    public function getMaxPages()
45
    {
46
        return $this->maxPages;
47
    }
48
49
    /**
50
     * @param int $maxPages
51
     */
52
    public function setMaxPages($maxPages)
53
    {
54
        $this->maxPages = $maxPages;
55
    }
56
57
    /**
58
     * Sets count per page.
59
     *
60
     * @param int $count
61
     */
62
    public function setCountPerPage($count)
63
    {
64
        $this->countPerPage = $count;
65
    }
66
67
    /**
68
     * Returns count per page.
69
     *
70
     * @return int
71
     */
72
    public function getCountPerPage()
73
    {
74
        return $this->countPerPage;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getState(Request $request)
81
    {
82
        $state = parent::getState($request);
83
        // Reset pager with any filter.
84
        $state->setUrlParameters([]);
85
        $page = (integer)$state->getValue();
86
        $state->setValue($page < 1 ? 1 : $page);
87
88
        return $state;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null)
95
    {
96
        if ($state && $state->isActive()) {
97
            $search->setFrom($this->countPerPage * ($state->getValue() - 1));
98
        }
99
100
        $search->setSize($this->countPerPage);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function preProcessSearch(Search $search, Search $relatedSearch, FilterState $state = null)
107
    {
108
        // Nothing to do here.
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function createViewData()
115
    {
116
        return new ViewData\PagerAwareViewData();
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function getViewData(DocumentIterator $result, ViewData $data)
123
    {
124
        /** @var ViewData\PagerAwareViewData $data */
125
        $data->setPager(
126
            new PagerService(
127
                new CountAdapter($result->count()),
128
                array_filter(
129
                    [
130
                        'page' => $data->getState()->getValue(),
131
                        'limit' => $this->getCountPerPage(),
132
                        'max_pages' => $this->getMaxPages(),
133
                    ]
134
                )
135
            )
136
        );
137
138
        return $data;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function isRelated()
145
    {
146
        return false;
147
    }
148
}
149