1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace musa11971\TVDB; |
4
|
|
|
|
5
|
|
|
use musa11971\TVDB\Exceptions\TVDBNotFoundException; |
6
|
|
|
use musa11971\TVDB\Exceptions\TVDBUnauthorizedException; |
7
|
|
|
|
8
|
|
|
class TVDBRequestResponse { |
9
|
|
|
protected $info; |
10
|
|
|
protected $response; |
11
|
|
|
protected $usedToken; |
12
|
|
|
protected $name; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* TVDBRequestResponse constructor. |
16
|
|
|
* |
17
|
|
|
* @param array $info |
18
|
|
|
* @param string $response |
19
|
|
|
* @param string|null $usedToken |
20
|
|
|
* @param string|null $name |
21
|
|
|
*/ |
22
|
|
|
public function __construct($info, $response, $usedToken, $name = null) |
23
|
|
|
{ |
24
|
|
|
$this->info = $info; |
25
|
|
|
$this->response = $response; |
26
|
|
|
$this->usedToken = $usedToken; |
27
|
|
|
$this->name = $name; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Gets the response in JSON format |
32
|
|
|
* |
33
|
|
|
* @return mixed |
34
|
|
|
*/ |
35
|
|
|
public function json() { |
36
|
|
|
return json_decode($this->response); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Checks whether or not the TVDB request was successful |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
|
|
public function isSuccessful() { |
45
|
|
|
if($this->info['http_code'] === 200) |
46
|
|
|
return true; |
47
|
|
|
else return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Throws the appropriate exception if the response was not successful |
52
|
|
|
* |
53
|
|
|
* @throws TVDBUnauthorizedException |
54
|
|
|
* @throws TVDBNotFoundException |
55
|
|
|
*/ |
56
|
|
|
public function throwException() { |
57
|
|
|
// Unauthorized |
58
|
|
|
if($this->info['http_code'] === 401) { |
59
|
|
|
if($this->name == 'login') { |
60
|
|
|
throw new TVDBUnauthorizedException('401: Unable to retrieve a TVDB token with the given details. Check your config.'); |
61
|
|
|
} |
62
|
|
|
else { |
63
|
|
|
throw new TVDBUnauthorizedException( |
64
|
|
|
'401: Unauthorized request to ' . $this->info['url'] . '. ' . |
65
|
|
|
(($this->usedToken === null) ? '(no TVDB token was found)' : '(however a TVDB token was found)') |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
// Not found |
70
|
|
|
else if($this->info['http_code'] === 404) { |
71
|
|
|
if($this->name == 'get_series') { |
72
|
|
|
throw new TVDBNotFoundException('404: Unable to find the series - ' . $this->info['url']); |
73
|
|
|
} |
74
|
|
|
else if($this->name == 'get_series_actors') { |
75
|
|
|
throw new TVDBNotFoundException('404: Unable to find the actors - ' . $this->info['url']); |
76
|
|
|
} |
77
|
|
|
else if($this->name == 'get_series_episodes') { |
78
|
|
|
throw new TVDBNotFoundException('404: Unable to find the episodes - ' . $this->info['url']); |
79
|
|
|
} |
80
|
|
|
else if($this->name == 'get_episode') { |
81
|
|
|
throw new TVDBNotFoundException('404: Unable to find the episode - ' . $this->info['url']); |
82
|
|
|
} |
83
|
|
|
// Generic 404 exception |
84
|
|
|
// We're ignoring 'search_series' because this API endpoint returns 404 |
85
|
|
|
// .. when there are no search results - we don't want that. |
86
|
|
|
else if($this->name != 'search_series') { |
87
|
|
|
throw new TVDBNotFoundException('404: Not found. ' . $this->info['url']); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |