Completed
Pull Request — master (#1056)
by Christian
05:45
created

PaginateElasticaQuerySubscriber::setSorting()   D

Complexity

Conditions 9
Paths 18

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 61.734

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 33
ccs 2
cts 15
cp 0.1333
rs 4.909
cc 9
eloc 16
nc 18
nop 1
crap 61.734
1
<?php
2
3
namespace FOS\ElasticaBundle\Subscriber;
4
5
use FOS\ElasticaBundle\Paginator\PaginatorAdapterInterface;
6
use FOS\ElasticaBundle\Paginator\PartialResultsInterface;
7
use Knp\Component\Pager\Event\ItemsEvent;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\RequestStack;
11
12
class PaginateElasticaQuerySubscriber implements EventSubscriberInterface
13
{
14
    /**
15
     * @var Request
16
     */
17
    private $request;
18
19
    /**
20
     * @param RequestStack|Request $requestStack
21
     */
22
    public function setRequest($requestStack)
23
    {
24
        if ($requestStack instanceof Request) {
25
            $this->request = $requestStack;
26
        } elseif ($requestStack instanceof RequestStack) {
27
            $this->request = $requestStack->getMasterRequest();
28
        }
29
    }
30
31
    public function items(ItemsEvent $event)
32
    {
33
        if ($event->target instanceof PaginatorAdapterInterface) {
34
            // Add sort to query
35
            $this->setSorting($event);
36
37
            /** @var $results PartialResultsInterface */
38
            $results = $event->target->getResults($event->getOffset(), $event->getLimit());
39
40
            $event->count = $results->getTotalHits();
41
            $event->items = $results->toArray();
42
            $facets = $results->getFacets();
43
            if (null != $facets) {
44
                $event->setCustomPaginationParameter('facets', $facets);
45
            }
46
            $aggregations = $results->getAggregations();
47
            if (null != $aggregations) {
48
                $event->setCustomPaginationParameter('aggregations', $aggregations);
49
            }
50
51
            $event->stopPropagation();
52
        }
53
    }
54
55
    /**
56
     * Adds knp paging sort to query.
57
     *
58
     * @param ItemsEvent $event
59
     */
60
    protected function setSorting(ItemsEvent $event)
61
    {
62
        $options = $event->options;
63
        $sortField = $this->request->get($options['sortFieldParameterName']);
64
65
        if (!$sortField && isset($options['defaultSortFieldName'])) {
66
            $sortField = $options['defaultSortFieldName'];
67
        }
68
69
        if (!empty($sortField)) {
70
            // determine sort direction
71
            $dir = 'asc';
72
            $sortDirection = $this->request->get($options['sortDirectionParameterName']);
73
74
            if (!$sortDirection && isset($options['defaultSortDirection'])) {
75
                $sortDirection = $options['defaultSortDirection'];
76
            }
77
78
            if ('desc' === strtolower($sortDirection)) {
79
                $dir = 'desc';
80
            }
81
82
            // check if the requested sort field is in the sort whitelist
83
            if (isset($options['sortFieldWhitelist']) && !in_array($sortField, $options['sortFieldWhitelist'])) {
84
                throw new \UnexpectedValueException(sprintf('Cannot sort by: [%s] this field is not in whitelist', $sortField));
85 4
            }
86
87
            // set sort on active query
88 4
            $event->target->getQuery()->setSort(array(
89
                $sortField => array('order' => $dir),
90
            ));
91
        }
92
    }
93
94
    public static function getSubscribedEvents()
95
    {
96
        return array(
97
            'knp_pager.items' => array('items', 1),
98
        );
99
    }
100
}
101