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\Exception\TransformationException; |
14
|
|
|
use Pbxg33k\MusicInfo\Model\IMusicServiceEndpoint; |
15
|
|
|
use GuzzleHttp\Psr7\Uri; |
16
|
|
|
use Pbxg33k\MusicInfo\Exception\MethodNotImplementedException; |
17
|
|
|
use Pbxg33k\MusicInfo\Model\Track as TrackModel; |
18
|
|
|
use Pbxg33k\MusicInfo\Service\Spotify\Service as SpotifyService; |
19
|
|
|
|
20
|
|
|
class Track implements IMusicServiceEndpoint |
21
|
|
|
{ |
22
|
|
|
const DATA_SOURCE = 'spotify'; |
23
|
|
|
|
24
|
|
|
protected $parent; |
25
|
|
|
|
26
|
39 |
|
public function __construct(SpotifyService $apiService) |
27
|
|
|
{ |
28
|
39 |
|
$this->setParent($apiService); |
29
|
39 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param $apiService |
33
|
|
|
* |
34
|
|
|
* @return $this |
35
|
|
|
*/ |
36
|
39 |
|
public function setParent($apiService) |
37
|
|
|
{ |
38
|
39 |
|
$this->parent = $apiService; |
39
|
|
|
|
40
|
39 |
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Transform single item to model |
45
|
|
|
* |
46
|
|
|
* @param $raw |
47
|
|
|
* |
48
|
|
|
* @return TrackModel |
49
|
|
|
*/ |
50
|
2 |
|
public function transformSingle($raw) |
51
|
|
|
{ |
52
|
2 |
|
$object = new TrackModel; |
53
|
|
|
$object |
54
|
2 |
|
->setId($raw->id) |
55
|
2 |
|
->setName($raw->name) |
56
|
2 |
|
->setExplicit($raw->explicit) |
57
|
2 |
|
->setLength($raw->duration_ms % 60) |
58
|
2 |
|
->setPreviewUri(new Uri($raw->preview_url)) |
59
|
2 |
|
->setDataSource(self::DATA_SOURCE) |
60
|
2 |
|
->setRawData($raw); |
61
|
|
|
|
62
|
2 |
|
return $object; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $raw |
67
|
|
|
* |
68
|
|
|
* @return ArrayCollection |
69
|
|
|
* @throws \Exception |
70
|
|
|
*/ |
71
|
2 |
View Code Duplication |
public function transformCollection($raw) |
|
|
|
|
72
|
|
|
{ |
73
|
2 |
|
$collection = new ArrayCollection(); |
74
|
2 |
|
if (is_object($raw) && isset($raw->tracks)) { |
75
|
2 |
|
foreach ($raw->tracks->items as $track) { |
76
|
2 |
|
$collection->add($this->transformSingle($track)); |
77
|
2 |
|
} |
78
|
|
|
|
79
|
2 |
|
return $collection; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
throw new TransformationException('Expected object'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $raw |
87
|
|
|
* |
88
|
|
|
* @return ArrayCollection |
89
|
|
|
* @throws \Exception |
90
|
|
|
*/ |
91
|
2 |
|
public function transform($raw) |
92
|
|
|
{ |
93
|
2 |
|
return $this->transformCollection($raw); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return SpotifyService |
98
|
|
|
*/ |
99
|
2 |
|
public function getParent() |
100
|
|
|
{ |
101
|
2 |
|
return $this->parent; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param $arguments |
106
|
|
|
* |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
|
|
public function get($arguments) |
110
|
|
|
{ |
111
|
|
|
throw new MethodNotImplementedException; |
112
|
|
|
// TODO: Implement get() method. |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param $arguments |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
* |
120
|
|
|
* @throws MethodNotImplementedException |
121
|
|
|
*/ |
122
|
|
|
public function getComplete($arguments) |
123
|
|
|
{ |
124
|
|
|
throw new MethodNotImplementedException(); |
125
|
|
|
// TODO: Implement getComplete() method. |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param $id |
130
|
|
|
* |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
|
|
public function getById($id) |
134
|
|
|
{ |
135
|
|
|
return $this->getParent()->getApiClient()->getTrack($id); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param $name |
140
|
|
|
* |
141
|
|
|
* @return mixed |
142
|
|
|
*/ |
143
|
2 |
|
public function getByName($name) |
144
|
|
|
{ |
145
|
2 |
|
return $this->transform($this->getParent()->getApiClient()->search($name, 'track')); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param $guid |
150
|
|
|
* |
151
|
|
|
* @return mixed |
152
|
|
|
*/ |
153
|
|
|
public function getByGuid($guid) |
154
|
|
|
{ |
155
|
|
|
return $this->getParent()->getApiClient()->getTrack($guid); |
156
|
|
|
} |
157
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.