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