Passed
Push — master ( 009dcf...195661 )
by Radosław
02:27
created

Bing::limitResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Radowoj\Searcher\SearchProvider;
4
5
use stdClass;
6
use Exception;
7
8
use GuzzleHttp\Client as GuzzleClient;
9
10
use Radowoj\Searcher\SearchResult\Collection;
11
use Radowoj\Searcher\SearchResult\ICollection;
12
use Radowoj\Searcher\SearchResult\Item;
13
14
15
class Bing extends SearchProvider implements ISearchProvider
16
{
17
    const URI = 'https://api.cognitive.microsoft.com/bing/v5.0/search?';
18
    const API_KEY_HEADER = 'Ocp-Apim-Subscription-Key';
19
20
    protected $apiKey = null;
21
22
    protected $guzzle = null;
23
24 5
    public function __construct(GuzzleClient $guzzle, string $apiKey)
25
    {
26 5
        $this->apiKey = $apiKey;
27 5
        $this->guzzle = $guzzle;
28 5
    }
29
30
31 4
    protected function searchRequest(string $query, int $limit, int $offset) : stdClass
32
    {
33
        $params = [
34 4
            'q' => $query,
35 4
            'count' => $limit,
36 4
            'offset' => $offset
37
        ];
38
39 4
        $queryString = http_build_query($params);
40
41 4
        $uri = self::URI . $queryString;
42
43 4
        $result = $this->guzzle->request(
44 4
            'GET',
45
            $uri, [
46
                'headers' => [
47 4
                    self::API_KEY_HEADER => $this->apiKey
48
                ]
49
            ]
50
        );
51
52 4
        $resultObject = json_decode($result->getBody());
53 4
        $this->validateResult($resultObject);
54 3
        $resultObject = $this->limitResult($resultObject, $limit);
55 3
        return $resultObject;
56
    }
57
58
59 4
    protected function validateResult(stdClass $result)
60
    {
61 4
        if (!isset($result->webPages->value) || !isset($result->webPages->totalEstimatedMatches)) {
62 1
            throw new Exception("Invalid Bing API response: " . print_r($result, 1));
63
        }
64 3
    }
65
66
67 3
    protected function limitResult(stdClass $result, $limit)
68
    {
69 3
        $result->webPages->value = array_slice($result->webPages->value, 0, $limit);
70 3
        return $result;
71
    }
72
73
74
    protected function getCollection(stdClass $result) : ICollection
75
    {
76 3
        $results = array_map(function($item) {
77
            return new Item([
78
                'url' => preg_match('/^https?:\/\//', $item->url)
79
                    ? $item->url
80
                    : "http://{$item->url}",
81
                'title' => $item->name,
82
                'description' => $item->snippet,
83
            ]);
84 3
        }, $result->webPages->value);
85
86 3
        return new Collection(
87
            $results,
88 3
            $result->webPages->totalEstimatedMatches
89
        );
90
    }
91
92
93
}
94