1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: PBX_g33k |
5
|
|
|
* Date: 30-May-16 |
6
|
|
|
* Time: 16:30 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Pbxg33k\MusicInfo\Service\Spotify\Endpoint; |
10
|
|
|
|
11
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
12
|
|
|
use Pbxg33k\MusicInfo\Exception\MethodNotImplementedException; |
13
|
|
|
use Pbxg33k\MusicInfo\Model\IMusicServiceEndpoint; |
14
|
|
|
use Pbxg33k\MusicInfo\Service\Spotify\Service as SpotifyService; |
15
|
|
|
use Pbxg33k\MusicInfo\Model\Artist as ArtistModel; |
16
|
|
|
|
17
|
|
|
class Artist implements IMusicServiceEndpoint |
18
|
|
|
{ |
19
|
|
|
const DATA_SOURCE = 'spotify'; |
20
|
|
|
/** |
21
|
|
|
* @var SpotifyService |
22
|
|
|
*/ |
23
|
|
|
protected $parent; |
24
|
|
|
|
25
|
1 |
|
public function __construct(SpotifyService $apiService) |
26
|
|
|
{ |
27
|
1 |
|
$this->parent = $apiService; |
28
|
1 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return mixed |
32
|
|
|
*/ |
33
|
|
|
public function setParent($parent) |
34
|
|
|
{ |
35
|
|
|
$this->parent = $parent; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return SpotifyService |
40
|
|
|
*/ |
41
|
|
|
public function getParent() |
42
|
|
|
{ |
43
|
|
|
return $this->parent; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param $arguments |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
* |
51
|
|
|
* @throws MethodNotImplementedException |
52
|
|
|
*/ |
53
|
|
|
public function get($arguments) |
54
|
|
|
{ |
55
|
|
|
throw new MethodNotImplementedException; |
56
|
|
|
// TODO: Implement get() method. |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $arguments |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
* |
64
|
|
|
* @throws MethodNotImplementedException |
65
|
|
|
*/ |
66
|
|
|
public function getComplete($arguments) |
67
|
|
|
{ |
68
|
|
|
throw new MethodNotImplementedException; |
69
|
|
|
// TODO: Implement getComplete() method. |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param $id |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
public function getById($id) |
78
|
|
|
{ |
79
|
|
|
return $this->getById($id); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $name |
84
|
|
|
* |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
|
|
public function getByName($name) |
88
|
|
|
{ |
89
|
|
|
return $this->transform($this->getParent()->getApiClient()->search($name, 'artist')); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param $guid |
94
|
|
|
* |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
public function getByGuid($guid) |
98
|
|
|
{ |
99
|
|
|
return $this->getParent()->getApiClient()->getArtist($guid); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param $raw |
104
|
|
|
* |
105
|
|
|
* @return ArtistModel |
106
|
|
|
*/ |
107
|
|
|
public function transformSingle($raw) |
108
|
|
|
{ |
109
|
|
|
$object = new ArtistModel; |
110
|
|
|
$object |
111
|
|
|
->setId($raw->id) |
112
|
|
|
->setName($raw->name) |
113
|
|
|
->setType($raw->type) |
114
|
|
|
->setUri($raw->external_urls->spotify) |
115
|
|
|
->setRawData($raw) |
116
|
|
|
->setDataSource(self::DATA_SOURCE); |
117
|
|
|
|
118
|
|
|
return $object; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param $raw |
123
|
|
|
* |
124
|
|
|
* @return ArrayCollection |
125
|
|
|
* @throws \Exception |
126
|
|
|
*/ |
127
|
|
|
public function transformCollection($raw) |
128
|
|
|
{ |
129
|
|
|
$collection = new ArrayCollection(); |
130
|
|
|
if(is_object($raw) && isset($raw->artists)) { |
131
|
|
|
foreach($raw->artists->items as $artist) { |
132
|
|
|
$collection->add($this->transformSingle($artist)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $collection; |
136
|
|
|
} |
137
|
|
|
throw new \Exception('Transform failed'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param $raw |
142
|
|
|
* |
143
|
|
|
* @return ArrayCollection |
144
|
|
|
* @throws \Exception |
145
|
|
|
*/ |
146
|
|
|
public function transform($raw) |
147
|
|
|
{ |
148
|
|
|
return $this->transformCollection($raw); |
149
|
|
|
} |
150
|
|
|
} |