|
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 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 $reports; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $details; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
private $app_client; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param HttpClient $client |
|
49
|
|
|
* @param ErrorDetector $detector |
|
50
|
|
|
* @param string $host |
|
51
|
|
|
* @param string $reports |
|
52
|
|
|
* @param string $details |
|
53
|
|
|
* @param string $app_client |
|
54
|
|
|
*/ |
|
55
|
4 |
|
public function __construct(HttpClient $client, ErrorDetector $detector, $host, $reports, $details, $app_client) |
|
56
|
|
|
{ |
|
57
|
4 |
|
$this->client = $client; |
|
58
|
4 |
|
$this->detector = $detector; |
|
59
|
4 |
|
$this->host = $host; |
|
60
|
4 |
|
$this->reports = $reports; |
|
61
|
4 |
|
$this->details = $details; |
|
62
|
4 |
|
$this->app_client = $app_client; |
|
63
|
4 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param int $id |
|
67
|
|
|
* @param array $options |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
2 |
|
public function reports($id, array $options = []) |
|
72
|
|
|
{ |
|
73
|
2 |
|
$options['query']['id'] = $id; |
|
74
|
|
|
|
|
75
|
2 |
|
return $this->request($this->host.$this->reports, $options); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param array $options |
|
80
|
|
|
* |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
2 |
|
public function details(array $options) |
|
84
|
|
|
{ |
|
85
|
2 |
|
return $this->request($this->host.$this->details, $options); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $url |
|
90
|
|
|
* @param array $options |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
4 |
|
private function request($url, array $options) |
|
95
|
|
|
{ |
|
96
|
4 |
|
if ($this->app_client) { |
|
97
|
4 |
|
$options['headers'] = array_merge( |
|
98
|
4 |
|
['User-Agent' => $this->app_client], |
|
99
|
4 |
|
isset($options['headers']) ? $options['headers'] : [] |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
4 |
|
$response = $this->client->request('GET', $url, $options); |
|
104
|
|
|
|
|
105
|
4 |
|
return $this->detector->detect($response); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|