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 searchRequest(string $query, int $limit, int $offset) : stdClass
|
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 |
|
$uri = self::URI . implode('&', $paramsMerged);
|
54
|
|
|
|
55
|
4 |
|
$result = $this->guzzle->request('GET', $uri);
|
56
|
4 |
|
$resultObject = json_decode($result->getBody());
|
57
|
|
|
|
58
|
4 |
|
$this->validateResult($resultObject);
|
59
|
3 |
|
$resultObject = $this->limitResult($resultObject, $limit);
|
60
|
3 |
|
return $resultObject;
|
61
|
|
|
}
|
62
|
|
|
|
63
|
|
|
|
64
|
3 |
|
protected function limitResult(stdClass $result, $limit)
|
65
|
|
|
{
|
66
|
3 |
|
$result->items = array_slice($result->items, 0, $limit);
|
67
|
3 |
|
return $result;
|
68
|
|
|
}
|
69
|
|
|
|
70
|
|
|
|
71
|
4 |
|
protected function validateResult(stdClass $result)
|
72
|
|
|
{
|
73
|
4 |
|
if (!isset($result->searchInformation->totalResults) || !isset($result->items)) {
|
74
|
1 |
|
throw new Exception("Invalid Google API response: " . print_r($result, 1));
|
75
|
|
|
}
|
76
|
3 |
|
}
|
77
|
|
|
|
78
|
|
|
protected function getCollection(stdClass $result) : ICollection
|
79
|
|
|
{
|
80
|
3 |
|
$results = array_map(function($item) {
|
81
|
|
|
return new Item([
|
82
|
|
|
'url' => $item->link,
|
83
|
|
|
'title' => $item->title,
|
84
|
|
|
'description' => $item->snippet,
|
85
|
|
|
]);
|
86
|
3 |
|
}, $result->items);
|
87
|
|
|
|
88
|
|
|
|
89
|
3 |
|
return new Collection(
|
90
|
|
|
$results,
|
91
|
3 |
|
$result->searchInformation->totalResults
|
92
|
|
|
);
|
93
|
|
|
|
94
|
|
|
return new Collection();
|
|
|
|
|
95
|
|
|
}
|
96
|
|
|
|
97
|
|
|
}
|
98
|
|
|
|
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
orexit
statements that have been added for debug purposes.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.