Passed
Branch feature/spotify (f0f03e)
by Oguzhan
04:06
created

Service::artist()   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 1 Features 0
Metric Value
c 1
b 1
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 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 1
    public function init($config = [])
40
    {
41 1
        if(!$config) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $config of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
42 1
            $config = $this->getConfig();
43 1
        }
44
45 1
        $this->spotifySession = new Session($config['client_id'], $config['client_secret'], $config['redirect_uri']);
46 1
        $this->setApiClient(new SpotifyWebAPI());
47
48 1
        $this->requestCredentialsToken($config['scopes']);
49 1
        $this->setInitialized(true);
50
        
51 1
        $this->artist = new Artist($this);
52
53 1
        return $this;
54
    }
55
56
    /**
57
     * @param $scopes
58
     *
59
     * @return mixed
60
     */
61 1
    public function requestCredentialsToken($scopes)
62
    {
63 1
        $this->spotifySession->requestCredentialsToken($scopes);
64 1
        return $this->setAccessTokenFromSession();
65
    }
66
67
    /**
68
     * @param Request $request
69
     *
70
     * @return string
71
     */
72
    public function requestAccessToken(Request $request)
73
    {
74
        $this->spotifySession->requestAccessToken($request->query->get('code'));
75
        $this->setAccessTokenFromSession();
76
        return $this->getApiClient()->getAccessToken();
77
    }
78
79
    /**
80
     * @return bool
81
     */
82
    public function refreshTokens()
83
    {
84
        return $this->spotifySession->refreshAccessToken($this->spotifySession->getRefreshToken());
85
    }
86
87
    /**
88
     * @return mixed
89
     */
90 1
    protected function setAccessTokenFromSession()
91
    {
92 1
        return $this->getApiClient()->setAccessToken($this->spotifySession->getAccessToken());
93
    }
94
95
    /**
96
     * @return mixed
97
     */
98
    public function getArtist()
99
    {
100
        return $this->artist;
101
    }
102
103
    /**
104
     * @return Session
105
     */
106
    public function getSpotifySession()
107
    {
108
        return $this->spotifySession;
109
    }
110
111
    /**
112
     * @return boolean
113
     */
114
    public function isAuthorized()
115
    {
116
        return $this->authorized;
117
    }
118
119
    public function artist()
120
    {
121
        return $this->artist;
122
    }
123
124
}