SpotifyApiAuth::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace SpotifyApiConnect\Application;
4
5
use SpotifyApiConnect\Application\SpotifyWebApiPhp\SessionInterface;
6
use SpotifyApiConnect\Message;
7
8
class SpotifyApiAuth implements SpotifyApiAuthInterface
9
{
10
    /**
11
     * @var SessionInterface
12
     */
13
    private $session;
14
15
    /**
16
     * @param SessionInterface $session
17
     */
18
    public function __construct(SessionInterface $session)
19
    {
20
        $this->session = $session;
21
    }
22
23
    /**
24
     * @return string
25
     */
26
    public function getAuthorizeUrlForPlaylistModifyPublic(): string
27
    {
28
        $options = [
29
            'scope' => [
30
                'playlist-modify-public',
31
            ],
32
        ];
33
        return $this->session->getAuthorizeUrl($options);
34
    }
35
36
    /**
37
     * @param string $code
38
     * @return string
39
     */
40
    public function getRefreshTokenByCode(string $code): string
41
    {
42
        if ($this->session->requestAccessToken($code) !== true) {
43
            throw new \RuntimeException(Message::ERROR_GET_REQUEST_TOKEN_BY_CODE);
44
        }
45
        return $this->session->getRefreshToken();
46
    }
47
48
    /**
49
     * @param string $refreshAccessToken
50
     * @return string
51
     */
52
    public function getAccessByRefreshToken(string $refreshAccessToken) : string
53
    {
54
        if( $this->session->refreshAccessToken($refreshAccessToken) !== true ) {
55
            throw new \RuntimeException(Message::ERROR_GET_REFRESH_TOKEN_BY_CODE);
56
        }
57
        return $this->session->getAccessToken();
58
    }
59
60
61
}