Passed
Push — master ( c8d0fd...c8cb1c )
by Radosław
02:14
created

Google::extractResults()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Radowoj\Searcher\SearchProvider;
4
5
6
use stdClass;
7
use Exception;
8
9
use GuzzleHttp\Client as GuzzleClient;
10
11
use Radowoj\Searcher\SearchResult\Collection;
12
use Radowoj\Searcher\SearchResult\ICollection;
13
use Radowoj\Searcher\SearchResult\Item;
14
15
16
17
class Google extends SearchProvider implements ISearchProvider
18
{
19
    const URI = 'https://www.googleapis.com/customsearch/v1?';
20
21
    protected $apiKey = null;
22
23
    protected $cx = null;
24
25
    protected $guzzle = null;
26
27 6
    public function __construct(GuzzleClient $guzzle, string $apiKey, string $cx)
28
    {
29 6
        $this->apiKey = $apiKey;
30 6
        $this->cx = $cx;
31 6
        $this->guzzle = $guzzle;
32 6
    }
33
34
35 5
    protected function getSearchQueryString(string $query, int $limit, int $offset)
0 ignored issues
show
Unused Code introduced by
The parameter $limit 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...
36
    {
37
        $params = [
38 5
            'key' => $this->apiKey,
39 5
            'q' => urlencode($query),
40 5
            'cx' => $this->cx,
41
        ];
42
43 5
        if ($offset) {
44 1
            $params['start'] = $offset;
45
        }
46
47 5
        $paramsMerged = [];
48
49 5
        foreach($params as $key => $value) {
50 5
            $paramsMerged[] = "{$key}={$value}";
51
        }
52
53 5
        return implode('&', $paramsMerged);
54
    }
55
56
57 5
    protected function searchRequest(string $query, int $limit, int $offset) : stdClass
58
    {
59 5
        $uri = self::URI . $this->getSearchQueryString($query, $limit, $offset);
60
61 5
        $result = $this->guzzle->request('GET', $uri);
62 5
        return json_decode($result->getBody());
63
    }
64
65
66 4
    protected function enforceLimit(stdClass $result, int $limit) : stdClass
67
    {
68 4
        if (!isset($result->items)) {
69
            return $result;
70
        }
71 4
        $result->items = array_slice($result->items, 0, $limit);
72 4
        return $result;
73
    }
74
75
76 5
    protected function validateRequestResult(stdClass $result)
77
    {
78 5
        if (!isset($result->kind) || $result->kind !== "customsearch#search") {
79 1
            throw new Exception("Invalid Google API response: " . print_r($result, 1));
80
        }
81 4
    }
82
83
84 4
    protected function extractResults(stdClass $result) : array
85
    {
86 4
        return isset($result->items)
87 4
            ? $result->items
88 4
            : [];
89
    }
90
91
92 4
    protected function extractTotalMatches(stdClass $result) : int
93
    {
94 4
        return isset($result->searchInformation->totalResults)
95 4
            ? $result->searchInformation->totalResults
96 4
            : 0;
97
    }
98
99
100
    protected function populateCollection(stdClass $result) : ICollection
101
    {
102 4
        $results = array_map(function($item) {
103 1
            return new Item([
104 1
                'url' => $item->link,
105 1
                'title' => $item->title,
106 1
                'description' => $item->snippet,
107
            ]);
108 4
        }, $this->extractResults($result));
109
110
111 4
        return new Collection(
112
            $results,
113 4
            $this->extractTotalMatches($result)
114
        );
115
116
        return new Collection();
117
    }
118
119
}
120