Passed
Branch account (d43697)
by vincent
02:17
created

Auth::getRequestToken()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 2
nop 0
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
    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;
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 1
        $this->request_token_expiration = \DateTime::createFromFormat('Y-m-d H:i:s e', $data->expires_at);
0 ignored issues
show
Documentation Bug introduced by
It seems like \DateTime::createFromFor... e', $data->expires_at) can also be of type false. However, the property $request_token_expiration is declared as type object<DateTime>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
99
100 1
        return $this->request_token;
101
    }
102
103
    /**
104
     * Create a new session Auth
105
     * @param string $request_token
106
     * @return string
107
     */
108 38
    public function createSession(string $request_token) : string
109
    {
110 38
        $data = $this->tmdb->getRequest('authentication/session/new', ['request_token' => $request_token]);
111
112 38
        if (!isset($data->success) || $data->success != 'true' || !isset($data->session_id)) {
113 1
            throw new InvalidResponseException("Creating session failed");
114
        }
115 37
        $this->session_id = $data->session_id;
116 37
        return $this->session_id;
117
    }
118
119
    /**
120
     * Magical getter
121
     * @param  string $name Name of the variable to get
122
     * @return mixed       Value of the variable getted
123
     */
124 2
    public function __get(string $name)
125
    {
126
        switch ($name) {
127 2
          case 'request_token':
128 2
          case 'session_id':
129 1
              return $this->$name;
130
          default:
131 1
              throw new NotFoundException();
132
      }
133
    }
134
}
135