Completed
Push — master ( 62c9f8...42303c )
by Peter
01:50
created

Browser::request()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 6
nc 1
nop 3
crap 2
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\ShikimoriBrowserBundle\Service;
12
13
use GuzzleHttp\Client as HttpClient;
14
15
class Browser
16
{
17
    /**
18
     * @var HttpClient
19
     */
20
    private $client;
21
22
    /**
23
     * @var ErrorDetector
24
     */
25
    private $detector;
26
27
    /**
28
     * @var string
29
     */
30
    private $host;
31
32
    /**
33
     * @var string
34
     */
35
    private $prefix;
36
37
    /**
38
     * @var string
39
     */
40
    private $app_client;
41
42
    /**
43
     * @param HttpClient    $client
44
     * @param ErrorDetector $detector
45
     * @param string        $host
46
     * @param string        $prefix
47
     * @param string        $app_client
48
     */
49 10
    public function __construct(HttpClient $client, ErrorDetector $detector, $host, $prefix, $app_client)
50
    {
51 10
        $this->client = $client;
52 10
        $this->detector = $detector;
53 10
        $this->host = $host;
54 10
        $this->prefix = $prefix;
55 10
        $this->app_client = $app_client;
56 10
    }
57
58
    /**
59
     * @param string $resource
60
     * @param array  $options
61
     *
62
     * @return array
63
     */
64 2
    public function get($resource, array $options = [])
65
    {
66 2
        return $this->request('GET', $resource, $options);
67
    }
68
69
    /**
70
     * @param string $resource
71
     * @param array  $options
72
     *
73
     * @return array
74
     */
75 2
    public function post($resource, array $options = [])
76
    {
77 2
        return $this->request('POST', $resource, $options);
78
    }
79
80
    /**
81
     * @param string $resource
82
     * @param array  $options
83
     *
84
     * @return array
85
     */
86 2
    public function put($resource, array $options = [])
87
    {
88 2
        return $this->request('PUT', $resource, $options);
89
    }
90
91
    /**
92
     * @param string $resource
93
     * @param array  $options
94
     *
95
     * @return array
96
     */
97 2
    public function patch($resource, array $options = [])
98
    {
99 2
        return $this->request('PATCH', $resource, $options);
100
    }
101
102
    /**
103
     * @param string $resource
104
     * @param array  $options
105
     *
106
     * @return array
107
     */
108 2
    public function delete($resource, array $options = [])
109
    {
110 2
        return $this->request('DELETE', $resource, $options);
111
    }
112
113
    /**
114
     * @param string $method
115
     * @param string $path
116
     * @param array  $options
117
     *
118
     * @return array
119
     */
120 10
    private function request($method, $path = '', array $options = [])
121
    {
122 10
        $options['headers'] = array_merge(
123
            [
124 10
                'User-Agent' => $this->app_client,
125
            ],
126 10
            isset($options['headers']) ? $options['headers'] : []
127
        );
128
129 10
        $response = $this->client->request($method, $this->host.$this->prefix.$path, $options);
130
131 10
        return $this->detector->detect($response);
132
    }
133
}
134