1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MediaMonks\SonataMediaBundle\Provider; |
4
|
|
|
|
5
|
|
|
use MediaMonks\SonataMediaBundle\Entity\Media; |
6
|
|
|
use MediaMonks\SonataMediaBundle\Model\MediaInterface; |
7
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
8
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
|
11
|
|
|
class SoundCloudProvider extends AbstractProvider implements ProviderInterface |
12
|
|
|
{ |
13
|
|
|
const URL_OEMBED = 'http://soundcloud.com/oembed?format=json&url=https://soundcloud.com/%s'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param FormMapper $formMapper |
17
|
|
|
*/ |
18
|
|
|
public function buildProviderCreateForm(FormMapper $formMapper) |
19
|
|
|
{ |
20
|
|
|
$formMapper->add('providerReference', TextType::class, ['label' => 'SoundCloud URL']); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param FormMapper $formMapper |
25
|
|
|
*/ |
26
|
|
|
public function buildProviderEditForm(FormMapper $formMapper) |
27
|
|
|
{ |
28
|
|
|
$formMapper->add('providerReference', TextType::class, ['label' => 'SoundCloud URL']); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Media $media |
33
|
|
|
* @throws \Exception |
34
|
|
|
*/ |
35
|
|
|
public function update(Media $media) |
36
|
|
|
{ |
37
|
|
|
$currentSoundCloudId = $media->getProviderReference(); |
38
|
|
|
$media->setProviderReference($this->parseReference($media->getProviderReference())); |
39
|
|
|
|
40
|
|
|
if ($currentSoundCloudId !== $media->getProviderReference()) { |
41
|
|
|
$data = $this->getDataByReference($media->getProviderReference()); |
42
|
|
|
|
43
|
|
|
$data['embedUrl'] = $this->extractEmbedUrl($data); |
44
|
|
|
$media->setProviderMetaData($data); |
45
|
|
|
|
46
|
|
|
if (empty($media->getTitle())) { |
47
|
|
|
$media->setTitle($data['title']); |
48
|
|
|
} |
49
|
|
|
if (empty($media->getDescription())) { |
50
|
|
|
$media->setDescription($data['description']); |
51
|
|
|
} |
52
|
|
|
if (empty($media->getAuthorName())) { |
53
|
|
|
$media->setAuthorName($data['author_name']); |
54
|
|
|
} |
55
|
|
|
if (empty($media->getImage())) { |
56
|
|
|
$this->setImage($media, $data['thumbnail_url']); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
parent::update($media); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Media $media |
65
|
|
|
* @param $thumbnailUrl |
66
|
|
|
*/ |
67
|
|
|
public function setImage(Media $media, $thumbnailUrl) |
68
|
|
|
{ |
69
|
|
|
$filename = sprintf('%s_%d.%s', sha1($media->getProviderReference()), time(), 'jpg'); |
70
|
|
|
$this->getFilesystem()->write( |
71
|
|
|
$filename, |
72
|
|
|
file_get_contents($thumbnailUrl) |
73
|
|
|
); |
74
|
|
|
$media->setImage($filename); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param $reference |
79
|
|
|
* @return mixed |
80
|
|
|
* @throws \Exception |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
protected function getDataByReference($reference) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$this->disableErrorHandler(); |
85
|
|
|
$data = json_decode(file_get_contents(sprintf(self::URL_OEMBED, $reference)), true); |
86
|
|
|
$this->restoreErrorHandler(); |
87
|
|
|
|
88
|
|
|
if (empty($data['title'])) { |
89
|
|
|
throw new \Exception( |
90
|
|
|
sprintf('Could not get data from SoundCloud for id "%s", is the name correct?', $reference) |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $data; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param $value |
99
|
|
|
* @return string |
100
|
|
|
* @throws \Exception |
101
|
|
|
*/ |
102
|
|
|
protected function parseReference($value) |
103
|
|
|
{ |
104
|
|
|
if (strpos($value, 'soundcloud.com')) { |
105
|
|
|
$url = parse_url($value); |
106
|
|
|
|
107
|
|
|
return trim($url['path'], '/'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $value; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param array $data |
115
|
|
|
*/ |
116
|
|
|
protected function extractEmbedUrl(array $data) |
117
|
|
|
{ |
118
|
|
|
preg_match('/src="(.*)"/', $data['html'], $matches); |
119
|
|
|
$url = $matches[1]; |
120
|
|
|
|
121
|
|
|
$data = parse_url($url); |
122
|
|
|
parse_str($data['query'], $data); |
123
|
|
|
|
124
|
|
|
return $data['url']; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param MediaInterface $media |
129
|
|
|
* @param array $options |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
public function toArray(MediaInterface $media, array $options = []) |
133
|
|
|
{ |
134
|
|
|
return parent::toArray($media, $options) + [ |
135
|
|
|
'id' => $media->getProviderReference(), |
136
|
|
|
]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
public function getIcon() |
143
|
|
|
{ |
144
|
|
|
return 'soundcloud'; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public function getTitle() |
151
|
|
|
{ |
152
|
|
|
return 'SoundCloud'; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function getType() |
156
|
|
|
{ |
157
|
|
|
return 'audio'; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
public function getEmbedTemplate() |
164
|
|
|
{ |
165
|
|
|
return 'MediaMonksSonataMediaBundle:Provider:soundcloud_embed.html.twig'; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
|
|
public function supportsDownload() |
172
|
|
|
{ |
173
|
|
|
return false; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
|
|
public function supportsEmbed() |
180
|
|
|
{ |
181
|
|
|
return true; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return bool |
186
|
|
|
*/ |
187
|
|
|
public function supportsImage() |
188
|
|
|
{ |
189
|
|
|
return true; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
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.