Completed
Pull Request — master (#278)
by Kristof
08:40
created

PullParsingSearchService::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 4
1
<?php
2
3
namespace CultuurNet\UDB3\Search;
4
5
use CultuurNet\Search\Parameter;
6
use CultuurNet\UDB3\Iri\IriGeneratorInterface;
7
use CultuurNet\UDB3\SearchAPI2;
8
use Psr\Log\LoggerInterface;
9
use Psr\Log\NullLogger;
10
11
/**
12
 * SearchServiceInterface implementation that uses Search API 2 and parses
13
 * results with a XML pull parser, decreasing the memory usage and improving
14
 * speed.
15
 */
16
class PullParsingSearchService implements SearchServiceInterface
17
{
18
    /**
19
     * @var SearchAPI2\ResultSetPullParser
20
     */
21
    protected $pullParser;
22
23
    /**
24
     * @var SearchAPI2\SearchServiceInterface
25
     */
26
    protected $searchAPI2;
27
28
    /**
29
     * @var IriGeneratorInterface
30
     */
31
    protected $eventIriGenerator;
32
33
    /**
34
     * @var IriGeneratorInterface
35
     */
36
    protected $placeIriGenerator;
37
38
    /**
39
     * @var LoggerInterface
40
     */
41
    protected $resultParserLogger;
42
43
    /**
44
     * @var bool
45
     */
46
    protected $includePrivateItems;
47
48
    /**
49
     * @param SearchAPI2\SearchServiceInterface $search
50
     * @param IriGeneratorInterface $eventIriGenerator
51
     * @param IriGeneratorInterface $placeIriGenerator
52
     * @param LoggerInterface|null $resultParserLogger
53
     */
54
    public function __construct(
55
        SearchAPI2\SearchServiceInterface $search,
56
        IriGeneratorInterface $eventIriGenerator,
57
        IriGeneratorInterface $placeIriGenerator,
58
        LoggerInterface $resultParserLogger = null
59
    ) {
60
        $this->searchAPI2 = $search;
61
        $this->eventIriGenerator = $eventIriGenerator;
62
        $this->placeIriGenerator = $placeIriGenerator;
63
        $this->resultParserLogger = $resultParserLogger ? $resultParserLogger : new NullLogger();
64
65
        $this->includePrivateItems = true;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function search($query, $limit = 30, $start = 0, $sort = null)
72
    {
73
        $response = $this->executeSearch($query, $limit, $start, $sort);
74
75
        $parser = $this->getPullParser();
76
77
        return $parser->getResultSet($response->getBody(true));
78
    }
79
80
    /**
81
     * @return PullParsingSearchService
82
     */
83
    public function doNotIncludePrivateItems()
84
    {
85
        $copy = clone $this;
86
        $copy->includePrivateItems = false;
87
        return $copy;
88
    }
89
90
    /**
91
     * Finds items matching an arbitrary query.
92
     *
93
     *  @param string $query
94
     *   An arbitrary query.
95
     * @param int $limit
96
     *   How many items to retrieve.
97
     * @param int $start
98
     *   Offset to start from.
99
     * @param string $sort
100
     *   Sorting to use.
101
     *
102
     * @return \Guzzle\Http\Message\Response
103
     */
104
    private function executeSearch($query, $limit, $start, $sort = null)
105
    {
106
        $qParam = new Parameter\Query($query);
107
        $groupParam = new Parameter\Group();
108
        $startParam = new Parameter\Start($start);
109
        $limitParam = new Parameter\Rows($limit);
110
        $typeParam = new Parameter\FilterQuery('type:event OR (type:actor AND category_id:8.15.0.0.0)');
111
112
        $params = array(
113
            $qParam,
114
            $groupParam,
115
            $limitParam,
116
            $startParam,
117
            $typeParam,
118
        );
119
120
        if ($this->includePrivateItems) {
121
            $privateParam = new Parameter\FilterQuery('private:*');
122
123
            $params[] = $privateParam;
124
        }
125
126
        if ($sort) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $sort of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
127
            $params[] = new Parameter\Parameter('sort', $sort);
128
        }
129
130
        $response = $this->searchAPI2->search($params);
0 ignored issues
show
Documentation introduced by
$params is of type array<integer,object<Cul...h\Parameter\Parameter>>, but the function expects a array<integer,object<Cul...h\Parameter\Parameter>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
131
132
        return $response;
133
    }
134
135
    /**
136
     * @return SearchAPI2\ResultSetPullParser
137
     */
138
    protected function getPullParser()
139
    {
140
        if (!$this->pullParser) {
141
            $this->pullParser = new SearchAPI2\ResultSetPullParser(
142
                new \XMLReader(),
143
                $this->eventIriGenerator,
144
                $this->placeIriGenerator
145
            );
146
            $this->pullParser->setLogger($this->resultParserLogger);
147
        }
148
        return $this->pullParser;
149
    }
150
}
151