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