Passed
Push — master ( c8cb1c...8e013d )
by Radosław
02:15
created

Bing::populateItem()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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