Completed
Pull Request — develop (#241)
by ANTHONIUS
07:36
created

SolrAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
namespace Solr\Paginator\Adapter;
11
12
use Solr\Exception\ServerException;
13
use Solr\Filter\AbstractPaginationQuery;
14
use Zend\Paginator\Adapter\AdapterInterface;
15
use Zend\Stdlib\Parameters;
16
use Solr\Bridge\ResultConverter;
17
18
/**
19
 * Provide adapter for Solr type paginator
20
 *
21
 * @author  Anthonius Munthi <[email protected]>
22
 * @since   0.27
23
 * @package Solr\Paginator\Adapter
24
 */
25
class SolrAdapter implements AdapterInterface
26
{
27
    /**
28
     * @var \SolrClient
29
     */
30
    protected $client;
31
32
    /**
33
     * @var Parameters
34
     */
35
    protected $params;
36
37
    /**
38
     * @var int
39
     */
40
    protected $count;
41
42
    /**
43
     * @var \SolrQuery
44
     */
45
    protected $query;
46
47
    /**
48
     * Store current query response from solr server
49
     *
50
     * @var \SolrQueryResponse
51
     */
52
    protected $response;
53
54
    /**
55
     * @var AbstractPaginationQuery
56
     */
57
    protected $filter;
58
59
    /**
60
     * @var ResultConverter
61
     */
62
    protected $resultConverter;
63
64
    /**
65
     * SolrAdapter constructor.
66
     *
67
     * @param   \SolrClient                 $client
68
     * @param   AbstractPaginationQuery     $filter
69
     * @param   ResultConverter             $resultConverter
70
     * @param   array                       $params
71
     */
72
    public function __construct($client,$filter,$resultConverter,$params=array())
73
    {
74
        $this->client           = $client;
75
        $this->filter           = $filter;
76
        $this->resultConverter  = $resultConverter;
77
        $this->params           = $params;
0 ignored issues
show
Documentation Bug introduced by
It seems like $params of type array is incompatible with the declared type object<Zend\Stdlib\Parameters> of property $params.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
78
    }
79
    
80
    /**
81
     * @inheritdoc
82
     */
83
    public function getItems($offset, $itemCountPerPage)
84
    {
85
        return $this->resultConverter->convert(
86
            $this->filter,
87
            $this->getResponse($offset,$itemCountPerPage)
88
        );
89
    }
90
91
    /**
92
     * @inheritdoc
93
     * @return  mixed
94
     * @throws \Exception
95
     */
96
    public function count()
97
    {
98
        $response = $this->getResponse()->getArrayResponse();
99
        return $response['response']['numFound'];
100
    }
101
102
    /**
103
     * Process query into server
104
     *
105
     * @param   int     $offset
106
     * @param   int     $itemCountPerPage
107
     * @return  \SolrQueryResponse
108
     * @throws  \Exception
109
     */
110
    protected function getResponse($offset=0,$itemCountPerPage=5)
111
    {
112
        if(!is_object($this->response)){
113
            $query = new \SolrQuery();
114
            $query = $this->filter->filter($this->params,$query);
0 ignored issues
show
Unused Code introduced by
The call to AbstractPaginationQuery::filter() has too many arguments starting with $query.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
115
            $query->setStart($offset);
116
            $query->setRows($itemCountPerPage);
117
            try{
118
                $this->response = $this->client->query($query);
119
            }catch (\Exception $e){
120
                $message = 'Failed to process query';
121
                throw new ServerException($message,$e->getCode(),$e);
122
            }
123
        }
124
        return $this->response;
125
    }
126
}