Completed
Push — 2.0 ( 9cfd8f...0c0b7d )
by Peter
03:41
created

GuzzleClient   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
c 3
b 0
f 0
lcom 1
cbo 4
dl 0
loc 115
ccs 36
cts 36
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 24 2
A setTimeout() 0 6 1
A setProxy() 0 6 1
A get() 0 23 3
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
namespace AnimeDb\Bundle\AniDbBrowserBundle\Service\Client;
10
11
use AnimeDb\Bundle\AniDbBrowserBundle\Util\ResponseRepair;
12
use Guzzle\Http\Client;
13
14
class GuzzleClient implements ClientInterface
15
{
16
    /**
17
     * @var Client
18
     */
19
    protected $client;
20
21
    /**
22
     * @var ResponseRepair
23
     */
24
    protected $repair;
25
26
    /**
27
     * @var string
28
     */
29
    protected $api_prefix;
30
31
    /**
32
     * @var string
33
     */
34
    protected $app_code;
35
36
    /**
37
     * @var array
38
     */
39
    protected $request_params = [];
40
41
    /**
42
     * @param Client $client
43
     * @param ResponseRepair $repair
44
     * @param string $api_prefix
45
     * @param string $api_client
46
     * @param string $api_clientver
47
     * @param string $api_protover
48
     * @param string $app_code
49
     */
50 50
    public function __construct(
51
        Client $client,
52
        ResponseRepair $repair,
53
        $api_prefix,
54
        $api_client,
55
        $api_clientver,
56
        $api_protover,
57
        $app_code
58
    ) {
59 50
        $this->client = $client;
60 50
        $this->app_code = $app_code;
61 50
        $this->repair = $repair;
62
63 50
        $query = [];
64 50
        if ($api_prefix) {
65 36
            parse_str(parse_url($api_prefix, PHP_URL_QUERY), $query);
66 36
            $this->api_prefix = parse_url($api_prefix, PHP_URL_PATH);
0 ignored issues
show
Documentation Bug introduced by
It seems like parse_url($api_prefix, PHP_URL_PATH) can also be of type false. However, the property $api_prefix is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
67 36
        }
68 50
        $this->request_params = [
69 50
            'client' => $api_client,
70 50
            'clientver' => $api_clientver,
71 50
            'protover' => $api_protover,
72 50
        ] + $query;
73 50
    }
74
75
    /**
76
     * @param int $timeout
77
     *
78
     * @return GuzzleClient
79
     */
80 1
    public function setTimeout($timeout)
81
    {
82 1
        $this->client->setDefaultOption('timeout', $timeout);
83
84 1
        return $this;
85
    }
86
87
    /**
88
     * @param string $proxy
89
     *
90
     * @return GuzzleClient
91
     */
92 1
    public function setProxy($proxy)
93
    {
94 1
        $this->client->setDefaultOption('proxy', $proxy);
95
96 1
        return $this;
97
    }
98
99
    /**
100
     * @param string $request
101
     * @param array $params
102
     *
103
     * @return string
104
     */
105 48
    public function get($request, array $params = [])
106
    {
107 48
        $request = $this->client->get(
108 48
            $this->api_prefix,
109 48
            null,
110
            [
111 48
                'query' => $this->request_params + ['request' => $request] + $params,
112 48
                'headers' => $this->app_code ? ['User-Agent' => $this->app_code] : [],
113
            ]
114 48
        );
115
116 48
        $response = $request->send();
117
118 48
        if ($response->isError()) {
119 24
            throw new \RuntimeException(sprintf(
120 24
                'Failed execute request "%s" to the server "%s"',
121 24
                $request,
122 24
                $this->client->getBaseUrl()
123 24
            ));
124
        }
125
126 24
        return $this->repair->repair(gzdecode($response->getBody(true))); // repair
127
    }
128
}
129