Completed
Push — 2.0 ( a00819...62c098 )
by Peter
07:50 queued 05:05
created

GuzzleClient::setProxy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
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
    private $client;
20
21
    /**
22
     * @var string
23
     */
24
    private $api_prefix;
25
26
    /**
27
     * @var string
28
     */
29
    private $app_code;
30
31
    /**
32
     * @var ResponseRepair
33
     */
34
    private $response_repair;
35
36
    /**
37
     * @param Client $client
38
     * @param ResponseRepair $response_repair
39
     * @param string $api_prefix
40
     * @param string $api_client
41
     * @param string $api_clientver
42
     * @param string $api_protover
43
     * @param string $app_code
44
     */
45
    public function __construct(
46
        Client $client,
47
        ResponseRepair $response_repair,
48
        $api_prefix,
49
        $api_client,
50
        $api_clientver,
51
        $api_protover,
52
        $app_code
53
    ) {
54
        $this->client = $client;
55
        $this->app_code = $app_code;
56
        $this->response_repair = $response_repair;
57
        $this->api_prefix = $api_prefix.
58
            (strpos($api_prefix, '?') !== false ? '&' : '?').
59
            http_build_query([
60
                'client' => $api_client,
61
                'clientver' => $api_clientver,
62
                'protover' => $api_protover,
63
            ]);
64
    }
65
66
    /**
67
     * @param int $timeout
68
     *
69
     * @return GuzzleClient
70
     */
71
    public function setTimeout($timeout)
72
    {
73
        $this->client->setDefaultOption('timeout', $timeout);
74
75
        return $this;
76
    }
77
78
    /**
79
     * @param string $proxy
80
     *
81
     * @return GuzzleClient
82
     */
83
    public function setProxy($proxy)
84
    {
85
        $this->client->setDefaultOption('proxy', $proxy);
86
87
        return $this;
88
    }
89
90
    /**
91
     * @param string $request
92
     * @param array $params
93
     *
94
     * @return string
95
     */
96
    public function get($request, array $params = [])
97
    {
98
        $request = $this->client->get(
99
            $this->api_prefix.'&request='.$request.($params ? '&'.http_build_query($params) : '')
100
        );
101
102
        if ($this->app_code) {
103
            $request->setHeader('User-Agent', $this->app_code);
104
        }
105
106
        $response = $request->send();
107
108
        if ($response->isError()) {
109
            throw new \RuntimeException(sprintf(
110
                'Failed execute request "%s" to the server "%s"',
111
                $request,
112
                $this->client->getBaseUrl()
113
            ));
114
        }
115
116
        return $this->response_repair->repair(gzdecode($response->getBody(true))); // repair
117
    }
118
}
119