Completed
Push — master ( 82f14f...636df4 )
by Kristof
10s
created

ResultsGenerator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 1
cbo 7
dl 0
loc 126
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A withSorting() 0 6 1
A getSorting() 0 4 1
A withPageSize() 0 6 1
A getPageSize() 0 4 1
B search() 0 38 4
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
0 ignored issues
show
Unused Code introduced by
The parameter $pageSize is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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;
0 ignored issues
show
Documentation Bug introduced by
The property $pageSize was declared of type integer, but $pageSize is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
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