1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Tmdb package. |
4
|
|
|
* |
5
|
|
|
* (c) Vincent Faliès <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @author Vincent Faliès <[email protected]> |
11
|
|
|
* @copyright Copyright (c) 2017 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
namespace VfacTmdb; |
16
|
|
|
|
17
|
|
|
use VfacTmdb\Exceptions\NotFoundException; |
18
|
|
|
use VfacTmdb\Exceptions\IncorrectParamException; |
19
|
|
|
use VfacTmdb\Interfaces\TmdbInterface; |
20
|
|
|
use VfacTmdb\Interfaces\AuthInterface; |
21
|
|
|
use VfacTmdb\Exceptions\InvalidResponseException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Auth class |
25
|
|
|
* @package Tmdb |
26
|
|
|
* @author Vincent Faliès <[email protected]> |
27
|
|
|
* @copyright Copyright (c) 2017 |
28
|
|
|
*/ |
29
|
|
|
class Auth implements AuthInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Tmdb object |
33
|
|
|
* @var TmdbInterface |
34
|
|
|
*/ |
35
|
|
|
private $tmdb = null; |
36
|
|
|
/** |
37
|
|
|
* Logger object |
38
|
|
|
* @var \Psr\Log\LoggerInterface |
39
|
|
|
*/ |
40
|
|
|
private $logger = null; |
41
|
|
|
/** |
42
|
|
|
* Request token |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $request_token = null; |
46
|
|
|
/** |
47
|
|
|
* Expiration date of request token |
48
|
|
|
* @var \DateTime |
49
|
|
|
*/ |
50
|
|
|
private $request_token_expiration = null; |
51
|
|
|
/** |
52
|
|
|
* Session Id |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
private $session_id = null; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Constructor |
59
|
|
|
* @param TmdbInterface $tmdb |
60
|
|
|
*/ |
61
|
43 |
|
public function __construct(TmdbInterface $tmdb) |
62
|
|
|
{ |
63
|
43 |
|
$this->tmdb = $tmdb; |
64
|
43 |
|
$this->logger = $tmdb->getLogger(); |
65
|
43 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Connect and valid request token |
69
|
|
|
* @param string $request_token |
70
|
|
|
* @param string|null $redirect_url Redirection url after connection (optional) |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
2 |
|
public function connect(string $request_token, ?string $redirect_url = null) : bool |
74
|
|
|
{ |
75
|
2 |
|
$url = "https://www.themoviedb.org/authenticate/$request_token"; |
76
|
2 |
|
if (!is_null($redirect_url)) { |
77
|
2 |
|
if (!filter_var($redirect_url, FILTER_VALIDATE_URL)) { |
78
|
1 |
|
throw new IncorrectParamException('Invalid redirect Url'); |
79
|
|
|
} |
80
|
1 |
|
$url .= "?redirect_to=$redirect_url"; |
81
|
|
|
} |
82
|
1 |
|
header("Location: $url"); |
83
|
1 |
|
return true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get a new request token |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
2 |
|
public function getRequestToken() : string |
91
|
|
|
{ |
92
|
2 |
|
$data = $this->tmdb->getRequest('authentication/token/new', []); |
93
|
|
|
|
94
|
2 |
|
if (!isset($data->success) || $data->success != 'true' || !isset($data->request_token)) { |
95
|
1 |
|
throw new InvalidResponseException("Getting request token failed"); |
96
|
|
|
} |
97
|
1 |
|
$this->request_token = $data->request_token; |
98
|
|
|
|
99
|
1 |
|
$expiration = \DateTime::createFromFormat('Y-m-d H:i:s e', $data->expires_at); |
100
|
1 |
|
$this->request_token_expiration = ($expiration !== false) ? $expiration : null; |
101
|
|
|
|
102
|
1 |
|
return $this->request_token; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Create a new session Auth |
107
|
|
|
* @param string $request_token |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
38 |
|
public function createSession(string $request_token) : string |
111
|
|
|
{ |
112
|
38 |
|
$data = $this->tmdb->getRequest('authentication/session/new', ['request_token' => $request_token]); |
113
|
|
|
|
114
|
38 |
|
if (!isset($data->success) || $data->success != 'true' || !isset($data->session_id)) { |
115
|
1 |
|
throw new InvalidResponseException("Creating session failed"); |
116
|
|
|
} |
117
|
37 |
|
$this->session_id = $data->session_id; |
118
|
37 |
|
return $this->session_id; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Magical getter |
123
|
|
|
* @param string $name Name of the variable to get |
124
|
|
|
* @return mixed Value of the variable getted |
125
|
|
|
*/ |
126
|
2 |
|
public function __get(string $name) |
127
|
|
|
{ |
128
|
|
|
switch ($name) { |
129
|
2 |
|
case 'request_token': |
130
|
2 |
|
case 'session_id': |
131
|
1 |
|
return $this->$name; |
132
|
|
|
default: |
133
|
1 |
|
throw new NotFoundException(); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|