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 VimeoProvider extends BaseVideoProvider |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
public function getHelperProperties(MediaInterface $media, $format, $options = array()) |
24
|
|
|
{ |
25
|
|
|
// documentation : http://vimeo.com/api/docs/moogaloop |
26
|
|
|
$defaults = array( |
27
|
|
|
// (optional) Flash Player version of app. Defaults to 9 .NEW! |
28
|
|
|
// 10 - New Moogaloop. 9 - Old Moogaloop without newest features. |
29
|
|
|
'fp_version' => 10, |
30
|
|
|
|
31
|
|
|
// (optional) Enable fullscreen capability. Defaults to true. |
32
|
|
|
'fullscreen' => true, |
33
|
|
|
|
34
|
|
|
// (optional) Show the byline on the video. Defaults to true. |
35
|
|
|
'title' => true, |
36
|
|
|
|
37
|
|
|
// (optional) Show the title on the video. Defaults to true. |
38
|
|
|
'byline' => 0, |
39
|
|
|
|
40
|
|
|
// (optional) Show the user's portrait on the video. Defaults to true. |
41
|
|
|
'portrait' => true, |
42
|
|
|
|
43
|
|
|
// (optional) Specify the color of the video controls. |
44
|
|
|
'color' => null, |
45
|
|
|
|
46
|
|
|
// (optional) Set to 1 to disable HD. |
47
|
|
|
'hd_off' => 0, |
48
|
|
|
|
49
|
|
|
// Set to 1 to enable the Javascript API. |
50
|
|
|
'js_api' => null, |
51
|
|
|
|
52
|
|
|
// (optional) JS function called when the player loads. Defaults to vimeo_player_loaded. |
53
|
|
|
'js_onLoad' => 0, |
54
|
|
|
|
55
|
|
|
// Unique id that is passed into all player events as the ending parameter. |
56
|
|
|
'js_swf_id' => uniqid('vimeo_player_'), |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
$player_parameters = array_merge($defaults, isset($options['player_parameters']) ? $options['player_parameters'] : array()); |
60
|
|
|
|
61
|
|
|
$box = $this->getBoxHelperProperties($media, $format, $options); |
62
|
|
|
|
63
|
|
|
$params = array( |
64
|
|
|
'src' => http_build_query($player_parameters), |
65
|
|
|
'id' => $player_parameters['js_swf_id'], |
66
|
|
|
'frameborder' => isset($options['frameborder']) ? $options['frameborder'] : 0, |
67
|
|
|
'width' => $box->getWidth(), |
68
|
|
|
'height' => $box->getHeight(), |
69
|
|
|
'class' => isset($options['class']) ? $options['class'] : '', |
70
|
|
|
'allow_fullscreen' => isset($options['allowfullscreen']) ? true : false, |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
return $params; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function getProviderMetadata() |
80
|
|
|
{ |
81
|
|
|
return new Metadata($this->getName(), $this->getName().'.description', false, 'SonataMediaBundle', array('class' => 'fa fa-vimeo-square')); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function updateMetadata(MediaInterface $media, $force = false) |
88
|
|
|
{ |
89
|
|
|
$url = sprintf('http://vimeo.com/api/oembed.json?url=%s', $this->getReferenceUrl($media)); |
90
|
|
|
|
91
|
|
|
try { |
92
|
|
|
$metadata = $this->getMetadata($media, $url); |
93
|
|
|
} catch (\RuntimeException $e) { |
94
|
|
|
$media->setEnabled(false); |
95
|
|
|
$media->setProviderStatus(MediaInterface::STATUS_ERROR); |
96
|
|
|
|
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// store provider information |
101
|
|
|
$media->setProviderMetadata($metadata); |
102
|
|
|
|
103
|
|
|
// update Media common fields from metadata |
104
|
|
|
if ($force) { |
105
|
|
|
$media->setName($metadata['title']); |
106
|
|
|
$media->setDescription($metadata['description']); |
107
|
|
|
$media->setAuthorName($metadata['author_name']); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$media->setHeight($metadata['height']); |
111
|
|
|
$media->setWidth($metadata['width']); |
112
|
|
|
$media->setLength($metadata['duration']); |
113
|
|
|
$media->setContentType('video/x-flv'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function getDownloadResponse(MediaInterface $media, $format, $mode, array $headers = array()) |
120
|
|
|
{ |
121
|
|
|
return new RedirectResponse($this->getReferenceUrl($media), 302, $headers); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get provider reference url. |
126
|
|
|
* |
127
|
|
|
* @param MediaInterface $media |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function getReferenceUrl(MediaInterface $media) |
132
|
|
|
{ |
133
|
|
|
return sprintf('http://vimeo.com/%s', $media->getProviderReference()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param MediaInterface $media |
138
|
|
|
*/ |
139
|
|
|
protected function fixBinaryContent(MediaInterface $media) |
140
|
|
|
{ |
141
|
|
|
if (!$media->getBinaryContent()) { |
142
|
|
|
return; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (preg_match("/vimeo\.com\/(video\/|)(\d+)/", $media->getBinaryContent(), $matches)) { |
146
|
|
|
$media->setBinaryContent($matches[2]); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
protected function doTransform(MediaInterface $media) |
154
|
|
|
{ |
155
|
|
|
$this->fixBinaryContent($media); |
156
|
|
|
|
157
|
|
|
if (!$media->getBinaryContent()) { |
158
|
|
|
return; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
// store provider information |
162
|
|
|
$media->setProviderName($this->name); |
163
|
|
|
$media->setProviderReference($media->getBinaryContent()); |
164
|
|
|
$media->setProviderStatus(MediaInterface::STATUS_OK); |
165
|
|
|
|
166
|
|
|
$this->updateMetadata($media, true); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
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.