1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ni-ju-san CMS. |
5
|
|
|
* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\LastFm\Service; |
13
|
|
|
|
14
|
|
|
use Core23\LastFm\Connection\ConnectionInterface; |
15
|
|
|
use Core23\LastFm\Connection\Session; |
16
|
|
|
use Core23\LastFm\Connection\SessionInterface; |
17
|
|
|
use Core23\LastFm\Exception\ApiException; |
18
|
|
|
use Core23\LastFm\Exception\NotFoundException; |
19
|
|
|
|
20
|
|
|
final class AuthService extends AbstractService |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $authUrl; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* AuthService constructor. |
29
|
|
|
* |
30
|
|
|
* @param ConnectionInterface $connection |
31
|
|
|
* @param string $authUrl |
32
|
|
|
*/ |
33
|
|
|
public function __construct(ConnectionInterface $connection, $authUrl = 'http://www.last.fm/api/auth/') |
34
|
|
|
{ |
35
|
|
|
parent::__construct($connection); |
36
|
|
|
|
37
|
|
|
$this->authUrl = $authUrl; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Creates a new session from a token. |
43
|
|
|
* |
44
|
|
|
* @param string $token |
45
|
|
|
* |
46
|
|
|
* @return SessionInterface|null |
47
|
|
|
* |
48
|
|
|
* @throws ApiException |
49
|
|
|
* @throws NotFoundException |
50
|
|
|
*/ |
51
|
|
|
public function createSession($token) |
52
|
|
|
{ |
53
|
|
|
$response = $this->signedCall('auth.getSession', array( |
54
|
|
|
'token' => $token, |
55
|
|
|
)); |
56
|
|
|
|
57
|
|
|
if ($response) { |
|
|
|
|
58
|
|
|
return new Session($response['session']['name'], $response['session']['key'], $response['session']['subscriber']); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Creates a new api token. |
66
|
|
|
* |
67
|
|
|
* @return string|false |
68
|
|
|
* |
69
|
|
|
* @throws ApiException |
70
|
|
|
* @throws NotFoundException |
71
|
|
|
*/ |
72
|
|
|
public function createToken() |
73
|
|
|
{ |
74
|
|
|
$response = $this->signedCall('auth.getToken'); |
75
|
|
|
|
76
|
|
|
if ($response) { |
|
|
|
|
77
|
|
|
return $response['token']; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Return the auth url. |
85
|
|
|
* |
86
|
|
|
* @param string $callbackUrl |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getAuthUrl($callbackUrl) |
91
|
|
|
{ |
92
|
|
|
return $this->authUrl . '?api_key=' . $this->connection->getApiKey() . '&cb=' . $callbackUrl; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.