Passed
Branch master (837534)
by Oguzhan
04:07 queued 33s
created

Service::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

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