|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Search; |
|
4
|
|
|
|
|
5
|
|
|
use CultuurNet\UDB3\Offer\OfferIdentifierInterface; |
|
6
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
7
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
8
|
|
|
use Psr\Log\NullLogger; |
|
9
|
|
|
|
|
10
|
|
|
class ResultsGenerator implements LoggerAwareInterface, ResultsGeneratorInterface |
|
11
|
|
|
{ |
|
12
|
|
|
use LoggerAwareTrait; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Default sorting method because it's ideal for getting consistent paging |
|
16
|
|
|
* results. |
|
17
|
|
|
*/ |
|
18
|
|
|
const SORT_CREATION_DATE_ASC = 'creationdate asc'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var SearchServiceInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $searchService; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $sorting; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var int |
|
32
|
|
|
*/ |
|
33
|
|
|
private $pageSize; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param SearchServiceInterface $searchService |
|
37
|
|
|
* @param string $sorting |
|
38
|
|
|
* @param int $pageSize |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct( |
|
41
|
|
|
SearchServiceInterface $searchService, |
|
42
|
|
|
$sorting = self::SORT_CREATION_DATE_ASC, |
|
43
|
|
|
$pageSize = 10 |
|
|
|
|
|
|
44
|
|
|
) { |
|
45
|
|
|
$this->searchService = $searchService; |
|
46
|
|
|
$this->sorting = $sorting; |
|
47
|
|
|
$this->pageSize = 10; |
|
48
|
|
|
|
|
49
|
|
|
// Set a default logger so we don't need to check if a logger is set |
|
50
|
|
|
// when we actually try to log something. This can easily be overridden |
|
51
|
|
|
// from outside as this method is public. |
|
52
|
|
|
$this->setLogger(new NullLogger()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $sorting |
|
57
|
|
|
* @return ResultsGenerator |
|
58
|
|
|
*/ |
|
59
|
|
|
public function withSorting($sorting) |
|
60
|
|
|
{ |
|
61
|
|
|
$c = clone $this; |
|
62
|
|
|
$c->sorting = $sorting; |
|
63
|
|
|
return $c; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getSorting() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->sorting; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $pageSize |
|
76
|
|
|
* @return ResultsGenerator |
|
77
|
|
|
*/ |
|
78
|
|
|
public function withPageSize($pageSize) |
|
79
|
|
|
{ |
|
80
|
|
|
$c = clone $this; |
|
81
|
|
|
$c->pageSize = $pageSize; |
|
|
|
|
|
|
82
|
|
|
return $c; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return int |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getPageSize() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->pageSize; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $query |
|
95
|
|
|
* @return \Iterator |
|
96
|
|
|
*/ |
|
97
|
|
|
public function search($query) |
|
98
|
|
|
{ |
|
99
|
|
|
$currentPage = 0; |
|
100
|
|
|
$ids = []; |
|
101
|
|
|
|
|
102
|
|
|
do { |
|
103
|
|
|
$results = $this->searchService->search( |
|
104
|
|
|
$query, |
|
105
|
|
|
$this->pageSize, |
|
106
|
|
|
$this->pageSize * $currentPage, |
|
107
|
|
|
$this->sorting |
|
108
|
|
|
); |
|
109
|
|
|
|
|
110
|
|
|
$total = $results->getTotalItems()->toNative(); |
|
111
|
|
|
|
|
112
|
|
|
foreach ($results->getItems() as $item) { |
|
113
|
|
|
$id = $item->getId(); |
|
114
|
|
|
|
|
115
|
|
|
if (!isset($ids[$id])) { |
|
116
|
|
|
// Store result id with current page in case we run into |
|
117
|
|
|
// the same id again later. |
|
118
|
|
|
$ids[$id] = $currentPage; |
|
119
|
|
|
yield $id => $item; |
|
120
|
|
|
} else { |
|
121
|
|
|
$this->logger->error( |
|
122
|
|
|
'query_duplicate_event', |
|
123
|
|
|
array( |
|
124
|
|
|
'query' => $query, |
|
125
|
|
|
'error' => "Found duplicate offer {$id} on page {$currentPage}, " . |
|
126
|
|
|
"occurred first time on page {$ids[$id]}." |
|
127
|
|
|
) |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$currentPage++; |
|
133
|
|
|
} while ($currentPage < ceil($total / $this->pageSize)); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.