Passed
Push — master ( b6a873...a57dbe )
by Radosław
02:08
created

Google::searchRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
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 5
    public function __construct(GuzzleClient $guzzle, string $apiKey, string $cx)
28
    {
29 5
        $this->apiKey = $apiKey;
30 5
        $this->cx = $cx;
31 5
        $this->guzzle = $guzzle;
32 5
    }
33
34
35 4
    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 4
            'key' => $this->apiKey,
39 4
            'q' => urlencode($query),
40 4
            'cx' => $this->cx,
41
        ];
42
43 4
        if ($offset) {
44 1
            $params['start'] = $offset;
45
        }
46
47 4
        $paramsMerged = [];
48
49 4
        foreach($params as $key => $value) {
50 4
            $paramsMerged[] = "{$key}={$value}";
51
        }
52
53 4
        return implode('&', $paramsMerged);
54
    }
55
56
57 4
    protected function searchRequest(string $query, int $limit, int $offset) : stdClass
58
    {
59 4
        $uri = self::URI . $this->getSearchQueryString($query, $limit, $offset);
60
61 4
        $result = $this->guzzle->request('GET', $uri);
62 4
        return json_decode($result->getBody());
63
    }
64
65
66 3
    protected function enforceLimit(stdClass $result, int $limit) : stdClass
67
    {
68 3
        $result->items = array_slice($result->items, 0, $limit);
69 3
        return $result;
70
    }
71
72
73 4
    protected function validateRequestResult(stdClass $result)
74
    {
75 4
        if (!isset($result->searchInformation->totalResults) || !isset($result->items)) {
76 1
            throw new Exception("Invalid Google API response: " . print_r($result, 1));
77
        }
78 3
    }
79
80
81
    protected function populateCollection(stdClass $result) : ICollection
82
    {
83 3
        $results = array_map(function($item) {
84
            return new Item([
85
                'url' => $item->link,
86
                'title' => $item->title,
87
                'description' => $item->snippet,
88
            ]);
89 3
        }, $result->items);
90
91
92 3
        return new Collection(
93
            $results,
94 3
            $result->searchInformation->totalResults
95
        );
96
97
        return new Collection();
0 ignored issues
show
Unused Code introduced by
return new \Radowoj\Sear...rchResult\Collection(); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
98
    }
99
100
}
101