Response::initKeywordsData()   D
last analyzed

Complexity

Conditions 9
Paths 6

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
rs 4.909
cc 9
eloc 19
nc 6
nop 0
1
<?php
2
namespace KWTClient\Response;
3
4
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
5
6
class Response implements ResponseInterface
7
{
8
    /**
9
     * @var ResponseInterface
10
     */
11
    protected $response;
12
13
    /**
14
     * @var array
15
     */
16
    protected $keywords = [];
17
18
    /**
19
     * @var array | null
20
     */
21
    protected $decodedRaw = null;
22
23
    /**
24
     * @var array
25
     */
26
    protected $queries = [];
27
28
    /**
29
     * KeywordsResponse constructor.
30
     *
31
     * @param PsrResponseInterface $response
32
     */
33
    public function __construct(PsrResponseInterface $response)
34
    {
35
        $this->response = $response;
0 ignored issues
show
Documentation Bug introduced by
It seems like $response of type object<Psr\Http\Message\ResponseInterface> is incompatible with the declared type object<KWTClient\Response\ResponseInterface> of property $response.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
    }
37
38
    protected function initKeywordsData()
39
    {
40
        if (!empty($this->decodedRaw)) {
41
            return;
42
        }
43
44
        $contents = $this->response->getBody()->getContents();
0 ignored issues
show
Bug introduced by
The method getBody() does not seem to exist on object<KWTClient\Response\ResponseInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
        if (empty($contents)) {
46
            return;
47
        }
48
49
        $this->decodedRaw = \GuzzleHttp\json_decode($contents);
0 ignored issues
show
Documentation Bug introduced by
It seems like \GuzzleHttp\json_decode($contents) of type * is incompatible with the declared type array of property $decodedRaw.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
50
        if ($this->decodedRaw->results) {
51
            foreach ($this->decodedRaw->results as $k=>$res) {
52
                if ($k == '_empty_') {
53
                    $this->keywords[] = ['kw'=>$res[0]->string,
54
                                         'vol'=>intval($res[0]->volume)];
55
                    continue;
56
                }
57
58
                foreach ($res as $keyword) {
59
                    $volume = 0;
60
                    if (!empty($keyword->volume)) {
61
                        $volume = intval($keyword->volume) > 0 ? intval($keyword->volume) : 0;
62
                    }
63
                    $this->keywords[] = ['kw'=>$keyword->string,
64
                                         'vol'=>$volume];
65
                }
66
            }
67
        }
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    public function getKeywords()
74
    {
75
       $this->initKeywordsData();
76
       return $this->keywords;
77
    }
78
}
79