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