Passed
Push — master ( 381043...c8d0fd )
by Radosław
02:18
created

Bing   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 97.56%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 0
loc 95
ccs 40
cts 41
cp 0.9756
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A searchRequest() 0 23 1
A validateRequestResult() 0 6 3
A enforceLimit() 0 8 2
A extractResults() 0 6 2
A extractTotalMatches() 0 6 2
A populateCollection() 0 17 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
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 6
    public function __construct(GuzzleClient $guzzle, string $apiKey)
25
    {
26 6
        $this->apiKey = $apiKey;
27 6
        $this->guzzle = $guzzle;
28 6
    }
29
30
31 5
    protected function searchRequest(string $query, int $limit, int $offset) : stdClass
32
    {
33
        $params = [
34 5
            'q' => $query,
35 5
            'count' => $limit,
36 5
            'offset' => $offset
37
        ];
38
39 5
        $queryString = http_build_query($params);
40
41 5
        $uri = self::URI . $queryString;
42
43 5
        $result = $this->guzzle->request(
44 5
            'GET',
45
            $uri, [
46
                'headers' => [
47 5
                    self::API_KEY_HEADER => $this->apiKey
48
                ]
49
            ]
50
        );
51
52 5
        return json_decode($result->getBody());
53
    }
54
55
56 5
    protected function validateRequestResult(stdClass $result)
57
    {
58 5
        if (!isset($result->_type) || $result->_type !== 'SearchResponse') {
59 1
            throw new Exception("Invalid Bing API response: " . print_r($result, 1));
60
        }
61 4
    }
62
63
64 4
    protected function enforceLimit(stdClass $result, int $limit) : stdClass
65
    {
66 4
        if (!isset($result->webPages->value)) {
67
            return $result;
68
        }
69 4
        $result->webPages->value = array_slice($result->webPages->value, 0, $limit);
70 4
        return $result;
71
    }
72
73
74 4
    protected function extractResults(stdClass $result) : array
75
    {
76 4
        return isset($result->webPages->value)
77 4
            ? $result->webPages->value
78 4
            : [];
79
    }
80
81
82 4
    protected function extractTotalMatches(stdClass $result) : int
83
    {
84 4
        return isset($result->webPages->totalEstimatedMatches)
85 4
            ? $result->webPages->totalEstimatedMatches
86 4
            : 0;
87
    }
88
89
90
    protected function populateCollection(stdClass $result) : ICollection
91
    {
92 4
        $results = array_map(function($item) {
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 4
        }, $this->extractResults($result));
101
102 4
        return new Collection(
103
            $results,
104 4
            $this->extractTotalMatches($result)
105
        );
106
    }
107
108
109
}
110