1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the Pbxg33k\MusicInfo package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* (c) 2017 Oguzhan uysal. All rights reserved |
9
|
|
|
******************************************************************************/ |
10
|
|
|
namespace Pbxg33k\MusicInfo\Service\Spotify\Endpoint; |
11
|
|
|
|
12
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
13
|
|
|
use Pbxg33k\MusicInfo\Model\IMusicServiceEndpoint; |
14
|
|
|
use GuzzleHttp\Psr7\Uri; |
15
|
|
|
use Pbxg33k\MusicInfo\Exception\MethodNotImplementedException; |
16
|
|
|
use Pbxg33k\MusicInfo\Model\Track as TrackModel; |
17
|
|
|
use Pbxg33k\MusicInfo\Service\Spotify\Service as SpotifyService; |
18
|
|
|
|
19
|
|
|
class Track implements IMusicServiceEndpoint |
20
|
|
|
{ |
21
|
|
|
const DATA_SOURCE = 'spotify'; |
22
|
|
|
|
23
|
|
|
protected $parent; |
24
|
|
|
|
25
|
34 |
|
public function __construct(SpotifyService $apiService) |
26
|
|
|
{ |
27
|
34 |
|
$this->setParent($apiService); |
28
|
34 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param $apiService |
32
|
|
|
* |
33
|
|
|
* @return $this |
34
|
|
|
*/ |
35
|
34 |
|
public function setParent($apiService) |
36
|
|
|
{ |
37
|
34 |
|
$this->parent = $apiService; |
38
|
|
|
|
39
|
34 |
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Transform single item to model |
44
|
|
|
* |
45
|
|
|
* @param $raw |
46
|
|
|
* |
47
|
|
|
* @return TrackModel |
48
|
|
|
*/ |
49
|
2 |
|
public function transformSingle($raw) |
50
|
|
|
{ |
51
|
2 |
|
$object = new TrackModel; |
52
|
|
|
$object |
53
|
2 |
|
->setId($raw->id) |
54
|
2 |
|
->setName($raw->name) |
55
|
2 |
|
->setExplicit($raw->explicit) |
56
|
2 |
|
->setLength($raw->duration_ms % 60) |
57
|
2 |
|
->setPreviewUri(new Uri($raw->preview_url)) |
58
|
2 |
|
->setDataSource(self::DATA_SOURCE) |
59
|
2 |
|
->setRawData($raw); |
60
|
|
|
|
61
|
2 |
|
return $object; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param $raw |
66
|
|
|
* |
67
|
|
|
* @return ArrayCollection |
68
|
|
|
* @throws \Exception |
69
|
|
|
*/ |
70
|
2 |
|
public function transformCollection($raw) |
71
|
|
|
{ |
72
|
2 |
|
$collection = new ArrayCollection(); |
73
|
2 |
|
if (is_object($raw) && isset($raw->tracks)) { |
74
|
2 |
|
foreach ($raw->tracks->items as $track) { |
75
|
2 |
|
$collection->add($this->transformSingle($track)); |
76
|
2 |
|
} |
77
|
|
|
|
78
|
2 |
|
return $collection; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
throw new \Exception('Transformation failed'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param $raw |
86
|
|
|
* |
87
|
|
|
* @return ArrayCollection |
88
|
|
|
* @throws \Exception |
89
|
|
|
*/ |
90
|
2 |
|
public function transform($raw) |
91
|
|
|
{ |
92
|
2 |
|
return $this->transformCollection($raw); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return SpotifyService |
97
|
|
|
*/ |
98
|
2 |
|
public function getParent() |
99
|
|
|
{ |
100
|
2 |
|
return $this->parent; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param $arguments |
105
|
|
|
* |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
|
|
public function get($arguments) |
109
|
|
|
{ |
110
|
|
|
throw new MethodNotImplementedException; |
111
|
|
|
// TODO: Implement get() method. |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param $arguments |
116
|
|
|
* |
117
|
|
|
* @return void |
118
|
|
|
* |
119
|
|
|
* @throws MethodNotImplementedException |
120
|
|
|
*/ |
121
|
|
|
public function getComplete($arguments) |
122
|
|
|
{ |
123
|
|
|
throw new MethodNotImplementedException(); |
124
|
|
|
// TODO: Implement getComplete() method. |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param $id |
129
|
|
|
* |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
|
|
public function getById($id) |
133
|
|
|
{ |
134
|
|
|
return $this->getParent()->getApiClient()->getTrack($id); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param $name |
139
|
|
|
* |
140
|
|
|
* @return mixed |
141
|
|
|
*/ |
142
|
2 |
|
public function getByName($name) |
143
|
|
|
{ |
144
|
2 |
|
return $this->transform($this->getParent()->getApiClient()->search($name, 'track')); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param $guid |
149
|
|
|
* |
150
|
|
|
* @return mixed |
151
|
|
|
*/ |
152
|
|
|
public function getByGuid($guid) |
153
|
|
|
{ |
154
|
|
|
return $this->getParent()->getApiClient()->getTrack($guid); |
155
|
|
|
} |
156
|
|
|
} |