Completed
Pull Request — master (#30)
by vincent
03:53
created

Auth::createSession()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 2
nop 1
crap 4
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 43
    public function __construct(TmdbInterface $tmdb)
57
    {
58 43
        $this->tmdb   = $tmdb;
59 43
        $this->logger = $tmdb->getLogger();
60 43
    }
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 2
    public function connect(string $request_token, ?string $redirect_url = null) : bool
69
    {
70 2
        $url = "https://www.themoviedb.org/authenticate/$request_token";
71 2
        if (!is_null($redirect_url)) {
72 2
            if (!filter_var($redirect_url, FILTER_VALIDATE_URL)) {
73 1
                throw new IncorrectParamException('Invalid redirect Url');
74
            }
75 1
            $url .= "?redirect_to=$redirect_url";
76
        }
77 1
        header("Location: $url");
78 1
        return true;
79
    }
80
81
    /**
82
     * Get a new request token
83
     * @return string
84
     */
85 2
    public function getRequestToken() : string
86
    {
87 2
        $data = $this->tmdb->getRequest('authentication/token/new', []);
88
89 2
        if (!isset($data->success) || $data->success != 'true' || !isset($data->request_token)) {
90 1
            throw new InvalidResponseException("Getting request token failed");
91
        }
92 1
        $this->request_token            = $data->request_token;
93
94 1
        return $this->request_token;
95
    }
96
97
    /**
98
     * Create a new session Auth
99
     * @param string $request_token
100
     * @return string
101
     */
102 38
    public function createSession(string $request_token) : string
103
    {
104 38
        $data = $this->tmdb->getRequest('authentication/session/new', ['request_token' => $request_token]);
105
106 38
        if (!isset($data->success) || $data->success != 'true' || !isset($data->session_id)) {
107 1
            throw new InvalidResponseException("Creating session failed");
108
        }
109 37
        $this->session_id = $data->session_id;
110 37
        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 2
    public function __get(string $name)
119
    {
120
        switch ($name) {
121 2
          case 'request_token':
122 2
          case 'session_id':
123 1
              return $this->$name;
124
          default:
125 1
              throw new NotFoundException();
126
      }
127
    }
128
}
129