Auth::getRequestToken()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 2
nop 0
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 4
rs 10
c 0
b 0
f 0
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
    protected $logger = null;
41
    /**
42
     * Request token
43
     * @var string
44
     */
45
    private $request_token = null;
46
    /**
47
     * Session Id
48
     * @var string
49
     */
50
    private $session_id = null;
51
52
    /**
53
     * Constructor
54
     * @param TmdbInterface $tmdb
55
     */
56 129
    public function __construct(TmdbInterface $tmdb)
57
    {
58 129
        $this->tmdb   = $tmdb;
59 129
        $this->logger = $tmdb->getLogger();
60 129
    }
61
62
    /**
63
     * Connect and valid request token
64
     * @param string $request_token
65
     * @param  string|null $redirect_url Redirection url after connection (optional)
66
     * @return bool
67
     */
68 6
    public function connect(string $request_token, ?string $redirect_url = null) : bool
69
    {
70 6
        $url = "https://www.themoviedb.org/authenticate/$request_token";
71 6
        if (!is_null($redirect_url)) {
72 6
            if (!filter_var($redirect_url, FILTER_VALIDATE_URL)) {
73 3
                throw new IncorrectParamException('Invalid redirect Url');
74
            }
75 3
            $url .= "?redirect_to=$redirect_url";
76
        }
77 3
        header("Location: $url");
78 3
        return true;
79
    }
80
81
    /**
82
     * Get a new request token
83
     * @return string
84
     */
85 6
    public function getRequestToken() : string
86
    {
87 6
        $data = $this->tmdb->getRequest('authentication/token/new', []);
88
89 6
        if (!isset($data->success) || $data->success != 'true' || !isset($data->request_token)) {
90 3
            throw new InvalidResponseException("Getting request token failed");
91
        }
92 3
        $this->request_token = $data->request_token;
93
94 3
        return $this->request_token;
95
    }
96
97
    /**
98
     * Create a new session Auth
99
     * @param string $request_token
100
     * @return string
101
     */
102 114
    public function createSession(string $request_token) : string
103
    {
104 114
        $data = $this->tmdb->getRequest('authentication/session/new', ['request_token' => $request_token]);
105
106 114
        if (!isset($data->success) || $data->success != 'true' || !isset($data->session_id)) {
107 3
            throw new InvalidResponseException("Creating session failed");
108
        }
109 111
        $this->session_id = $data->session_id;
110 111
        return $this->session_id;
111
    }
112
113
    /**
114
     * Magical getter
115
     * @param  string $name Name of the variable to get
116
     * @return mixed       Value of the variable getted
117
     */
118 6
    public function __get(string $name)
119
    {
120 4
        switch ($name) {
121 6
            case 'request_token':
122 6
          case 'session_id':
123 3
              return $this->$name;
124
            default:
125 3
              throw new NotFoundException();
126
        }
127
    }
128
}
129