Completed
Push — master ( f2414e...49803b )
by Peter
05:04
created

Browser::options()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 14
cts 14
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 1
nop 1
crap 3
1
<?php
2
3
/**
4
 * AnimeDb package.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
9
 */
10
11
namespace AnimeDb\Bundle\AniDbBrowserBundle\Service;
12
13
use AnimeDb\Bundle\AniDbBrowserBundle\Util\ErrorDetector;
14
use AnimeDb\Bundle\AniDbBrowserBundle\Util\ResponseRepair;
15
use GuzzleHttp\Client as HttpClient;
16
17
class Browser
18
{
19
    /**
20
     * @var HttpClient
21
     */
22
    private $client;
23
24
    /**
25
     * @var ResponseRepair
26
     */
27
    private $repair;
28
29
    /**
30
     * @var ErrorDetector
31
     */
32
    private $detector;
33
34
    /**
35
     * @var string
36
     */
37
    private $api_host;
38
39
    /**
40
     * @var string
41
     */
42
    private $api_prefix;
43
44
    /**
45
     * @var int
46
     */
47
    private $api_protover;
48
49
    /**
50
     * @var int
51
     */
52
    private $app_version;
53
54
    /**
55
     * @var string
56
     */
57
    private $app_client;
58
59
    /**
60
     * @var string
61
     */
62
    private $app_code;
63
64
    /**
65
     * @param HttpClient     $client
66
     * @param ResponseRepair $repair
67
     * @param ErrorDetector  $detector
68
     * @param string         $api_host
69
     * @param string         $api_prefix
70
     * @param int            $api_protover
71
     * @param int            $app_version
72
     * @param string         $app_client
73
     * @param string         $app_code
74
     */
75 2
    public function __construct(
76
        HttpClient $client,
77
        ResponseRepair $repair,
78
        ErrorDetector $detector,
79
        $api_host,
80
        $api_prefix,
81
        $api_protover,
82
        $app_version,
83
        $app_client,
84
        $app_code
85
    ) {
86 2
        $this->client = $client;
87 2
        $this->repair = $repair;
88 2
        $this->detector = $detector;
89 2
        $this->api_host = $api_host;
90 2
        $this->api_prefix = $api_prefix;
91 2
        $this->api_protover = $api_protover;
92 2
        $this->app_version = $app_version;
93 2
        $this->app_client = $app_client;
94 2
        $this->app_code = $app_code;
95 2
    }
96
97
    /**
98
     * @param array $options
99
     *
100
     * @return string
101
     */
102 2
    public function get(array $options)
103
    {
104 2
        $options = $this->options($options);
105
106 2
        $response = $this->client->request('GET', $this->api_host.$this->api_prefix, $options);
107 2
        $content = $response->getBody()->getContents();
108
109 2
        $content = $this->repair->repair($content);
110 2
        $content = $this->detector->detect($content);
111
112 2
        return $content;
113
    }
114
115
    /**
116
     * @param array $options
117
     *
118
     * @return array
119
     */
120 2
    private function options(array $options = [])
121
    {
122 2
        $options['query'] = array_merge(
123
            [
124 2
                'protover' => $this->api_protover,
125 2
                'clientver' => $this->app_version,
126 2
                'client' => $this->app_client,
127 2
            ],
128 2
            isset($options['query']) ? $options['query'] : []
129 2
        );
130
131 2
        $options['headers'] = array_merge(
132
            [
133 2
                'User-Agent' => $this->app_code,
134 2
            ],
135 2
            isset($options['headers']) ? $options['headers'] : []
136 2
        );
137
138 2
        return $options;
139
    }
140
}
141