Passed
Push — feature/spotify ( ac6c48...53a5b1 )
by Oguzhan
07:37
created

Service::refreshTokens()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: PBX_g33k
5
 * Date: 30-May-16
6
 * Time: 13:01
7
 */
8
9
namespace Pbxg33k\MusicInfo\Service\Spotify;
10
11
use Pbxg33k\MusicInfo\Service\BaseService;
12
use Pbxg33k\MusicInfo\Service\Spotify\Endpoint\Artist;
13
use Pbxg33k\MusicInfo\Service\Spotify\Endpoint\Track;
14
use SpotifyWebAPI\Session;
15
use SpotifyWebAPI\SpotifyWebAPI;
16
use Symfony\Component\HttpFoundation\Request;
17
18
class Service extends BaseService
19
{
20
    /**
21
     * Is the client authorized via OAuth2.0
22
     *
23
     * @var bool
24
     */
25
    protected $authorized = false;
26
27
    /**
28
     * @var Session
29
     */
30
    protected $spotifySession;
31
32
    /**
33
     * @var Artist
34
     */
35
    protected $artist;
36
37
    /**
38
     * @var Track
39
     */
40
    protected $track;
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 11
    public function init($config = [])
46
    {
47 11
        if (empty($config)) {
48 11
            $config = $this->getConfig();
49 11
        }
50
51 11
        $this->spotifySession = new Session($config['client_id'], $config['client_secret'], $config['redirect_uri']);
52 11
        $this->setApiClient(new SpotifyWebAPI());
53
54 11
        $this->requestCredentialsToken($config['scopes']);
55 11
        $this->setInitialized(true);
56
57 11
        $this->artist = new Artist($this);
58 11
        $this->track = new Track($this);
59
60 11
        return $this;
61
    }
62
63
    /**
64
     * @param $scopes
65
     *
66
     * @return mixed
67
     */
68 11
    public function requestCredentialsToken($scopes)
69
    {
70 11
        $this->spotifySession->requestCredentialsToken($scopes);
71
72 11
        return $this->setAccessTokenFromSession();
73
    }
74
75
    /**
76
     * @param Request $request
77
     *
78
     * @return string
79
     */
80
    public function requestAccessToken(Request $request)
81
    {
82
        $this->spotifySession->requestAccessToken($request->query->get('code'));
83
        $this->setAccessTokenFromSession();
84
85
        return $this->getApiClient()->getAccessToken();
86
    }
87
88
    /**
89
     * @return bool
90
     */
91
    public function refreshTokens()
92
    {
93
        return $this->spotifySession->refreshAccessToken($this->spotifySession->getRefreshToken());
94
    }
95
96
    /**
97
     * @return mixed
98
     */
99 11
    protected function setAccessTokenFromSession()
100
    {
101 11
        return $this->getApiClient()->setAccessToken($this->spotifySession->getAccessToken());
102
    }
103
104
    /**
105
     * @return Artist
106
     */
107 1
    public function getArtist()
108
    {
109 1
        return $this->artist;
110
    }
111
112
    /**
113
     * @return Session
114
     */
115 1
    public function getSpotifySession()
116
    {
117 1
        return $this->spotifySession;
118
    }
119
120
    /**
121
     * @return boolean
122
     */
123
    public function isAuthorized()
124
    {
125
        return $this->authorized;
126
    }
127
128 5
    public function artist()
129
    {
130 5
        return $this->artist;
131
    }
132
133 2
    public function track()
134
    {
135 2
        return $this->track;
136
    }
137
138
}