Completed
Push — feature/spotify ( 015a1d...74fcff )
by Oguzhan
02:19
created

Service::testArtistSearchWithServiceAsString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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