Passed
Push — master ( 3df97c...1b6e74 )
by Peter
03:23
created

Browser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 5
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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\AnimeNewsNetworkBrowserBundle\Service;
12
13
use GuzzleHttp\Client;
14
15
class Browser
16
{
17
    /**
18
     * @var Client
19
     */
20
    private $client;
21
22
    /**
23
     * @var string
24
     */
25
    private $host;
26
27
    /**
28
     * @var string
29
     */
30
    private $reports;
31
32
    /**
33
     * @var string
34
     */
35
    private $details;
36
37
    /**
38
     * @var string
39
     */
40
    private $app_client;
41
42
    /**
43
     * @param Client $client
44
     * @param string $host
45
     * @param string $reports
46
     * @param string $details
47
     * @param string $app_client
48
     */
49 4
    public function __construct(Client $client, $host, $reports, $details, $app_client)
50
    {
51 4
        $this->client = $client;
52 4
        $this->host = $host;
53 4
        $this->reports = $reports;
54 4
        $this->details = $details;
55 4
        $this->app_client = $app_client;
56 4
    }
57
58
    /**
59
     * @param int   $id
60
     * @param array $options
61
     *
62
     * @return string
63
     */
64 2
    public function reports($id, array $options = [])
65
    {
66 2
        $options['id'] = $id;
67
68 2
        return $this->request($this->host.$this->reports, $options);
69
    }
70
71
    /**
72
     * @param array $options
73
     *
74
     * @return string
75
     */
76 2
    public function details(array $options)
77
    {
78 2
        return $this->request($this->host.$this->details, $options);
79
    }
80
81
    /**
82
     * @param string $url
83
     * @param array  $options
84
     *
85
     * @return string
86
     */
87 4
    private function request($url, array $options)
88
    {
89 4
        if ($this->app_client) {
90 4
            $options['headers'] = array_merge(
91 4
                ['User-Agent' => $this->app_client],
92 4
                isset($options['headers']) ? $options['headers'] : []
93
            );
94
        }
95
96 4
        $response = $this->client->request('GET', $url, $options);
97
98 4
        return $response->getBody()->getContents();
99
    }
100
}
101