1 | <?php |
||
10 | abstract class AbstractOembedProvider extends AbstractProvider implements OembedProviderInterface, EmbeddableProviderInterface |
||
11 | { |
||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | protected $oembedDataCache; |
||
16 | |||
17 | /** |
||
18 | * @param AbstractMedia $media |
||
19 | * @param bool $providerReferenceUpdated |
||
20 | * @throws \Exception |
||
21 | */ |
||
22 | public function update(AbstractMedia $media, $providerReferenceUpdated) |
||
23 | { |
||
24 | if ($providerReferenceUpdated) { |
||
25 | $media->setProviderReference($this->parseProviderReference($media->getProviderReference())); |
||
26 | $this->updateMediaObject($media); |
||
27 | } |
||
28 | |||
29 | parent::update($media, $providerReferenceUpdated); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @param AbstractMedia $media |
||
34 | */ |
||
35 | protected function updateMediaObject(AbstractMedia $media) |
||
36 | { |
||
37 | $data = $this->getOembedDataCache($media->getProviderReference()); |
||
38 | |||
39 | $media->setProviderMetaData($data); |
||
40 | |||
41 | if (empty($media->getTitle()) && isset($data['title'])) { |
||
42 | $media->setTitle($data['title']); |
||
43 | } |
||
44 | if (empty($media->getDescription()) && isset($data['description'])) { |
||
45 | $media->setDescription($data['description']); |
||
46 | } |
||
47 | if (empty($media->getAuthorName()) && isset($data['author_name'])) { |
||
48 | $media->setAuthorName($data['author_name']); |
||
49 | } |
||
50 | |||
51 | if (empty($media->getImage())) { |
||
52 | $this->refreshImage($media); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param FormMapper $formMapper |
||
58 | */ |
||
59 | public function buildProviderCreateForm(FormMapper $formMapper) |
||
60 | { |
||
61 | $formMapper->add( |
||
62 | 'providerReference', |
||
63 | TextType::class, |
||
64 | ['label' => $this->getReferenceLabel()] |
||
65 | ); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param FormMapper $formMapper |
||
70 | */ |
||
71 | public function buildProviderEditFormBefore(FormMapper $formMapper) |
||
72 | { |
||
73 | $formMapper->add( |
||
74 | 'providerReference', |
||
75 | TextType::class, |
||
76 | ['label' => $this->getReferenceLabel()] |
||
77 | ); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param ErrorElement $errorElement |
||
82 | * @param AbstractMedia $media |
||
83 | */ |
||
84 | public function validate(ErrorElement $errorElement, AbstractMedia $media) |
||
93 | |||
94 | /** |
||
95 | * @param \MediaMonks\SonataMediaBundle\Model\AbstractMedia $media |
||
96 | */ |
||
97 | public function refreshImage(AbstractMedia $media) |
||
106 | |||
107 | /** |
||
108 | * @param string $id |
||
109 | * @return string |
||
110 | */ |
||
111 | public function getImageUrl($id) |
||
115 | |||
116 | /** |
||
117 | * @param $id |
||
118 | * @return mixed |
||
119 | * @throws \Exception |
||
120 | */ |
||
121 | protected function getOembedDataCache($id) |
||
122 | { |
||
123 | if (empty($this->oembedDataCache[$id])) { |
||
124 | |||
125 | $this->disableErrorHandler(); |
||
126 | $data = json_decode($this->getHttpClient()->getData($this->getOembedUrl($id)), true); |
||
127 | $this->restoreErrorHandler(); |
||
128 | |||
129 | if (empty($data['title'])) { |
||
130 | throw new \Exception($this->getTranslator()->trans('error.provider_reference', [ |
||
131 | '%provider%' => $this->getName(), |
||
132 | '%reference%' => $id |
||
133 | ])); |
||
134 | } |
||
135 | |||
136 | $this->oembedDataCache[$id] = $data; |
||
137 | } |
||
138 | |||
139 | return $this->oembedDataCache[$id]; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @return string |
||
144 | */ |
||
145 | public function getReferenceLabel() |
||
149 | } |
||
150 |