1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MediaMonks\SonataMediaBundle\Provider; |
4
|
|
|
|
5
|
|
|
use MediaMonks\SonataMediaBundle\Entity\Media; |
6
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
7
|
|
|
use Sonata\CoreBundle\Validator\ErrorElement; |
8
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
9
|
|
|
|
10
|
|
|
abstract class AbstractOembedProvider extends AbstractProvider implements OembedProviderInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $oembedData; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param Media $media |
19
|
|
|
* @param bool $providerReferenceUpdated |
20
|
|
|
* @throws \Exception |
21
|
|
|
*/ |
22
|
|
|
public function update(Media $media, $providerReferenceUpdated) |
23
|
|
|
{ |
24
|
|
|
if ($providerReferenceUpdated) { |
25
|
|
|
$media->setProviderReference($this->parseProviderReference($media->getProviderReference())); |
26
|
|
|
|
27
|
|
|
$data = $this->getOembedData($media->getProviderReference()); |
28
|
|
|
|
29
|
|
|
$media->setProviderMetaData($data); |
30
|
|
|
|
31
|
|
|
if (empty($media->getTitle()) && isset($data['title'])) { |
32
|
|
|
$media->setTitle($data['title']); |
33
|
|
|
} |
34
|
|
|
if (empty($media->getDescription()) && isset($data['description'])) { |
35
|
|
|
$media->setDescription($data['description']); |
36
|
|
|
} |
37
|
|
|
if (empty($media->getAuthorName()) && isset($data['author_name'])) { |
38
|
|
|
$media->setAuthorName($data['author_name']); |
39
|
|
|
} |
40
|
|
|
if (empty($media->getImage())) { |
41
|
|
|
$this->refreshImage($media); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
parent::update($media, $providerReferenceUpdated); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param FormMapper $formMapper |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function buildProviderCreateForm(FormMapper $formMapper) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$formMapper->add( |
54
|
|
|
'providerReference', |
55
|
|
|
TextType::class, |
56
|
|
|
['label' => sprintf('%s %s', $this->getTitle(), $this->getReferenceName())] |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param FormMapper $formMapper |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function buildProviderEditForm(FormMapper $formMapper) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$formMapper->add( |
66
|
|
|
'providerReference', |
67
|
|
|
TextType::class, |
68
|
|
|
['label' => sprintf('%s %s', $this->getTitle(), $this->getReferenceName())] |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param ErrorElement $errorElement |
74
|
|
|
* @param Media $media |
75
|
|
|
*/ |
76
|
|
|
public function validate(ErrorElement $errorElement, Media $media) |
77
|
|
|
{ |
78
|
|
|
try { |
79
|
|
|
$this->getOembedData($this->parseProviderReference($media->getProviderReference())); |
80
|
|
|
} |
81
|
|
|
catch (\Exception $e) { |
82
|
|
|
$errorElement->with('providerReference')->addViolation($e->getMessage()); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param \MediaMonks\SonataMediaBundle\Entity\Media $media |
88
|
|
|
*/ |
89
|
|
|
public function refreshImage(Media $media) |
90
|
|
|
{ |
91
|
|
|
$filename = sprintf('%s_%d.%s', sha1($media->getProviderReference()), time(), 'jpg'); |
92
|
|
|
$thumbnailUrl = $this->getImageUrl($media->getProviderReference()); |
93
|
|
|
|
94
|
|
|
$this->getFilesystem()->write( |
95
|
|
|
$filename, |
96
|
|
|
file_get_contents($thumbnailUrl) |
97
|
|
|
); |
98
|
|
|
$media->setImage($filename); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $id |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function getImageUrl($id) |
106
|
|
|
{ |
107
|
|
|
return $this->getOembedData($id)['thumbnail_url']; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param $id |
112
|
|
|
* @return mixed |
113
|
|
|
* @throws \Exception |
114
|
|
|
*/ |
115
|
|
|
protected function getOembedData($id) |
116
|
|
|
{ |
117
|
|
|
if (empty($this->oembedData)) { |
118
|
|
|
|
119
|
|
|
$this->disableErrorHandler(); |
120
|
|
|
$data = json_decode(file_get_contents($this->getOembedUrl($id)), true); |
121
|
|
|
$this->restoreErrorHandler(); |
122
|
|
|
|
123
|
|
|
if (empty($data['title'])) { |
124
|
|
|
throw new \Exception(sprintf('%s %s "%s" seems to be incorrect', $this->getTitle(), $this->getReferenceName(), $id)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$this->oembedData = $data; |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this->oembedData; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function getReferenceName() |
137
|
|
|
{ |
138
|
|
|
return 'Reference'; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public function supportsDownload() |
145
|
|
|
{ |
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
|
|
public function supportsEmbed() |
153
|
|
|
{ |
154
|
|
|
return true; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
|
|
public function supportsImage() |
161
|
|
|
{ |
162
|
|
|
return true; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
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.