1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sonata\MediaBundle\Provider; |
13
|
|
|
|
14
|
|
|
use Sonata\CoreBundle\Model\Metadata; |
15
|
|
|
use Sonata\MediaBundle\Model\MediaInterface; |
16
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
17
|
|
|
|
18
|
|
|
class DailyMotionProvider extends BaseVideoProvider |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
public function getHelperProperties(MediaInterface $media, $format, $options = array()) |
24
|
|
|
{ |
25
|
|
|
// documentation : http://www.dailymotion.com/en/doc/api/player |
26
|
|
|
|
27
|
|
|
$defaults = array( |
28
|
|
|
// Values: 0 or 1. Default is 0. Determines if the player loads related videos when |
29
|
|
|
// the current video begins playback. |
30
|
|
|
'related' => 0, |
31
|
|
|
|
32
|
|
|
// Values: 0 or 1. Default is 1. Determines if the player allows explicit content to |
33
|
|
|
// be played. This parameter may be added to embed code by platforms which do not |
34
|
|
|
// want explicit content to be posted by their users. |
35
|
|
|
'explicit' => 0, |
36
|
|
|
|
37
|
|
|
// Values: 0 or 1. Default is 0. Determines if the video will begin playing |
38
|
|
|
// automatically when the player loads. |
39
|
|
|
'autoPlay' => 0, |
40
|
|
|
|
41
|
|
|
// Values: 0 or 1. Default is 0. Determines if the video will begin muted. |
42
|
|
|
'autoMute' => 0, |
43
|
|
|
|
44
|
|
|
// Values: 0 or 1. Default is 0. Determines if the video will unmuted on mouse over. |
45
|
|
|
// Of course it works only if the player is on automute=1. |
46
|
|
|
'unmuteOnMouseOver' => 0, |
47
|
|
|
|
48
|
|
|
// Values: a number of seconds. Default is 0. Determines if the video will begin |
49
|
|
|
// playing the video at a given time. |
50
|
|
|
'start' => 0, |
51
|
|
|
|
52
|
|
|
// Values: 0 or 1. Default is 0. Enable the Javascript API by setting this parameter |
53
|
|
|
// to 1. For more information and instructions on using the Javascript API, see the |
54
|
|
|
// JavaScript API documentation. |
55
|
|
|
'enableApi' => 0, |
56
|
|
|
|
57
|
|
|
// Values: 0 or 1. Default is 0. Determines if the player should display controls |
58
|
|
|
// or not during video playback. |
59
|
|
|
'chromeless' => 0, |
60
|
|
|
|
61
|
|
|
// Values: 0 or 1. Default is 0. Determines if the video should be expended to fit |
62
|
|
|
// the whole player's size. |
63
|
|
|
'expendVideo' => 0, |
64
|
|
|
'color2' => null, |
65
|
|
|
|
66
|
|
|
// Player color changes may be set using color codes. A color is described by its |
67
|
|
|
// hexadecimal value (eg: FF0000 for red). |
68
|
|
|
'foreground' => null, |
69
|
|
|
'background' => null, |
70
|
|
|
'highlight' => null, |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$player_parameters = array_merge($defaults, isset($options['player_parameters']) ? $options['player_parameters'] : array()); |
74
|
|
|
|
75
|
|
|
$box = $this->getBoxHelperProperties($media, $format, $options); |
76
|
|
|
|
77
|
|
|
$params = array( |
78
|
|
|
'player_parameters' => http_build_query($player_parameters), |
79
|
|
|
'allowFullScreen' => isset($options['allowFullScreen']) ? $options['allowFullScreen'] : 'true', |
80
|
|
|
'allowScriptAccess' => isset($options['allowScriptAccess']) ? $options['allowScriptAccess'] : 'always', |
81
|
|
|
'width' => $box->getWidth(), |
82
|
|
|
'height' => $box->getHeight(), |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
return $params; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function getProviderMetadata() |
92
|
|
|
{ |
93
|
|
|
return new Metadata($this->getName(), $this->getName().'.description', 'bundles/sonatamedia/dailymotion-icon.png', 'SonataMediaBundle'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function updateMetadata(MediaInterface $media, $force = false) |
100
|
|
|
{ |
101
|
|
|
$url = sprintf('http://www.dailymotion.com/services/oembed?url=%s&format=json', $this->getReferenceUrl($media)); |
102
|
|
|
|
103
|
|
|
try { |
104
|
|
|
$metadata = $this->getMetadata($media, $url); |
105
|
|
|
} catch (\RuntimeException $e) { |
106
|
|
|
$media->setEnabled(false); |
107
|
|
|
$media->setProviderStatus(MediaInterface::STATUS_ERROR); |
108
|
|
|
|
109
|
|
|
return; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$media->setProviderMetadata($metadata); |
113
|
|
|
|
114
|
|
|
if ($force) { |
115
|
|
|
$media->setName($metadata['title']); |
116
|
|
|
$media->setAuthorName($metadata['author_name']); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$media->setHeight($metadata['height']); |
120
|
|
|
$media->setWidth($metadata['width']); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
public function getDownloadResponse(MediaInterface $media, $format, $mode, array $headers = array()) |
127
|
|
|
{ |
128
|
|
|
return new RedirectResponse($this->getReferenceUrl($media), 302, $headers); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get provider reference url. |
133
|
|
|
* |
134
|
|
|
* @param MediaInterface $media |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function getReferenceUrl(MediaInterface $media) |
139
|
|
|
{ |
140
|
|
|
return sprintf('http://www.dailymotion.com/video/%s', $media->getProviderReference()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param MediaInterface $media |
145
|
|
|
*/ |
146
|
|
|
protected function fixBinaryContent(MediaInterface $media) |
147
|
|
|
{ |
148
|
|
|
if (!$media->getBinaryContent()) { |
149
|
|
|
return; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (preg_match("/www.dailymotion.com\/video\/([0-9a-zA-Z]*)_/", $media->getBinaryContent(), $matches)) { |
153
|
|
|
$media->setBinaryContent($matches[1]); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
|
|
protected function doTransform(MediaInterface $media) |
161
|
|
|
{ |
162
|
|
|
$this->fixBinaryContent($media); |
163
|
|
|
|
164
|
|
|
if (!$media->getBinaryContent()) { |
165
|
|
|
return; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$media->setProviderName($this->name); |
169
|
|
|
$media->setProviderStatus(MediaInterface::STATUS_OK); |
170
|
|
|
$media->setProviderReference($media->getBinaryContent()); |
171
|
|
|
|
172
|
|
|
$this->updateMetadata($media, true); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.