Completed
Push — master ( e671cd...ead8f5 )
by Peter
01:56
created

Browser::wrapException()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

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