Completed
Pull Request — develop (#241)
by ANTHONIUS
08:03
created

SolrAdapter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 6
c 3
b 0
f 1
lcom 1
cbo 3
dl 0
loc 102
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getItems() 0 7 1
A count() 0 5 1
A getResponse() 0 16 3
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
6
 * @license   MIT
7
 */
8
9
namespace Solr\Paginator\Adapter;
10
11
use Solr\Exception\ServerException;
12
use Solr\Filter\AbstractPaginationQuery;
13
use Zend\Paginator\Adapter\AdapterInterface;
14
use Zend\Stdlib\Parameters;
15
use Solr\Bridge\ResultConverter;
16
17
class SolrAdapter implements AdapterInterface
18
{
19
    /**
20
     * @var \SolrClient
21
     */
22
    protected $client;
23
24
    /**
25
     * @var Parameters
26
     */
27
    protected $params;
28
29
    /**
30
     * @var int
31
     */
32
    protected $count;
33
34
    /**
35
     * @var \SolrQuery
36
     */
37
    protected $query;
38
39
    /**
40
     * Store current query response from solr server
41
     *
42
     * @var \SolrQueryResponse
43
     */
44
    protected $response;
45
46
    /**
47
     * @var AbstractPaginationQuery
48
     */
49
    protected $filter;
50
51
    /**
52
     * @var ResultConverter
53
     */
54
    protected $resultConverter;
55
56
    /**
57
     * SolrAdapter constructor.
58
     *
59
     * @param   \SolrClient                 $client
60
     * @param   AbstractPaginationQuery     $filter
61
     * @param   ResultConverter             $resultConverter
62
     * @param   array                       $params
63
     */
64
    public function __construct($client,$filter,$resultConverter,$params=array())
65
    {
66
        $this->client           = $client;
67
        $this->filter           = $filter;
68
        $this->resultConverter  = $resultConverter;
69
        $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...
70
    }
71
    
72
    /**
73
     * @inheritdoc
74
     */
75
    public function getItems($offset, $itemCountPerPage)
76
    {
77
        return $this->resultConverter->convert(
78
            $this->filter,
79
            $this->getResponse($offset,$itemCountPerPage)
80
        );
81
    }
82
83
    /**
84
     * @inheritdoc
85
     * @return  mixed
86
     * @throws \Exception
87
     */
88
    public function count()
89
    {
90
        $response = $this->getResponse()->getArrayResponse();
91
        return $response['response']['numFound'];
92
    }
93
94
    /**
95
     * Process query into server
96
     *
97
     * @param   int     $offset
98
     * @param   int     $itemCountPerPage
99
     * @return  \SolrQueryResponse
100
     * @throws  \Exception
101
     */
102
    protected function getResponse($offset=0,$itemCountPerPage=5)
103
    {
104
        if(!is_object($this->response)){
105
            $query = new \SolrQuery();
106
            $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...
107
            $query->setStart($offset);
108
            $query->setRows($itemCountPerPage);
109
            try{
110
                $this->response = $this->client->query($query);
111
            }catch (\Exception $e){
112
                $message = 'Failed to process query';
113
                throw new ServerException($message,$e->getCode(),$e);
114
            }
115
        }
116
        return $this->response;
117
    }
118
}