Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

Helper/RemoteVideo/RemoteVideoHandler.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\MediaBundle\Helper\RemoteVideo;
4
5
use Kunstmaan\MediaBundle\Entity\Media;
6
use Kunstmaan\MediaBundle\Form\RemoteVideo\RemoteVideoType;
7
use Kunstmaan\MediaBundle\Helper\Media\AbstractMediaHandler;
8
9
/**
10
 * RemoteVideoStrategy
11
 */
12
class RemoteVideoHandler extends AbstractMediaHandler
13
{
14
    /**
15
     * @var string
16
     */
17
    const CONTENT_TYPE = 'remote/video';
18
    /**
19
     * @var string
20
     */
21
    const TYPE = 'video';
22
23
    /**
24
     * @var array
25
     */
26
    protected $configuration = array();
27
28
    /**
29
     * Constructor. Takes the configuration of the RemoveVideoHandler
30
     *
31
     * @param array $configuration
32
     */
33 2
    public function __construct($priority, $configuration = array())
34
    {
35 2
        parent::__construct($priority);
36 2
        $this->configuration = $configuration;
37 2
    }
38
39
    /**
40
     * @return string
41
     */
42
    public function getName()
43
    {
44
        return 'Remote Video Handler';
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    public function getType()
51
    {
52
        return RemoteVideoHandler::TYPE;
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    public function getFormType()
59
    {
60
        return RemoteVideoType::class;
61
    }
62
63
    /**
64
     * Return the default form type options
65
     *
66
     * @return array
67
     */
68
    public function getFormTypeOptions()
69
    {
70
        return array('configuration' => $this->configuration);
71
    }
72
73
    /**
74
     * @param mixed $object
75
     *
76
     * @return bool
77
     */
78 View Code Duplication
    public function canHandle($object)
79
    {
80
        if (
81
            (\is_string($object)) ||
82
            ($object instanceof Media && $object->getContentType() == RemoteVideoHandler::CONTENT_TYPE)
83
        ) {
84
            return true;
85
        }
86
87
        return false;
88
    }
89
90
    /**
91
     * @param Media $media
92
     *
93
     * @return RemoteVideoHelper
94
     */
95
    public function getFormHelper(Media $media)
96
    {
97
        return new RemoteVideoHelper($media);
98
    }
99
100
    /**
101
     * @param Media $media
102
     *
103
     * @throws \RuntimeException when the file does not exist
104
     */
105
    public function prepareMedia(Media $media)
106
    {
107
        if (null === $media->getUuid()) {
108
            $uuid = uniqid();
109
            $media->setUuid($uuid);
110
        }
111
        $video = new RemoteVideoHelper($media);
112
        $code = $video->getCode();
113
        //update thumbnail
114
        switch ($video->getType()) {
115
            case 'youtube':
116
                try {
117
                    if (@fopen('https://img.youtube.com/vi/'.$code.'/maxresdefault.jpg', 'r') === false) {
118
                        $video->setThumbnailUrl('https://img.youtube.com/vi/'.$code.'/0.jpg');
119
                    } else {
120
                        $video->setThumbnailUrl('https://img.youtube.com/vi/'.$code.'/maxresdefault.jpg');
121
                    }
122
                } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
123
                }
124
125
                break;
126
            case 'vimeo':
127
                try {
128
                    $xml = simplexml_load_file('https://vimeo.com/api/v2/video/' . $code . '.xml');
129
                    $video->setThumbnailUrl((string) $xml->video->thumbnail_large);
130
                } catch (\Exception $e) {
131
                }
132
133
                break;
134
            case 'dailymotion':
135
                try {
136
                    $json = json_decode(
137
                        file_get_contents('https://api.dailymotion.com/video/' . $code . '?fields=thumbnail_large_url')
138
                    );
139
                    $thumbnailUrl = $json->{'thumbnail_large_url'};
140
                    /* dirty hack to fix urls for imagine */
141
                    if (!$this->endsWith($thumbnailUrl, '.jpg') && !$this->endsWith($thumbnailUrl, '.png')) {
142
                        $thumbnailUrl = $thumbnailUrl . '&ext=.jpg';
143
                    }
144
                    $video->setThumbnailUrl($thumbnailUrl);
145
                } catch (\Exception $e) {
146
                }
147
148
                break;
149
        }
150
    }
151
152
    /**
153
     * String helper
154
     *
155
     * @param string $str string
156
     * @param string $sub substring
157
     *
158
     * @return bool
159
     */
160
    private function endsWith($str, $sub)
161
    {
162
        return substr($str, \strlen($str) - \strlen($sub)) === $sub;
163
    }
164
165
    /**
166
     * @param Media $media
167
     */
168
    public function saveMedia(Media $media)
169
    {
170
    }
171
172
    /**
173
     * @param Media $media
174
     */
175
    public function removeMedia(Media $media)
176
    {
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public function updateMedia(Media $media)
183
    {
184
    }
185
186
    /**
187
     * @param array $params
188
     *
189
     * @return array
190
     */
191
    public function getAddUrlFor(array $params = array())
192
    {
193
        return array(
194
            'video' => array(
195
                'path' => 'KunstmaanMediaBundle_folder_videocreate',
196
                'params' => array(
197
                    'folderId' => $params['folderId'],
198
                ),
199
            ),
200
        );
201
    }
202
203
    /**
204
     * @param mixed $data
205
     *
206
     * @return Media
207
     */
208 2
    public function createNew($data)
209
    {
210 2
        $result = null;
211 2
        if (\is_string($data)) {
212 2
            if (strncmp($data, 'http', 4) !== 0) {
213
                $data = 'http://' . $data;
214
            }
215 2
            $parsedUrl = parse_url($data);
216 2
            switch ($parsedUrl['host']) {
217 2 View Code Duplication
                case 'youtu.be':
218 1
                    $code = substr($parsedUrl['path'], 1); // remove slash
219 1
                    $result = new Media();
220 1
                    $video = new RemoteVideoHelper($result);
221 1
                    $video->setType('youtube');
222 1
                    $video->setCode($code);
223 1
                    $result = $video->getMedia();
224 1
                    $result->setName('Youtube ' . $code);
225
226 1
                    break;
227
228 1
                case 'www.youtube.com':
229 View Code Duplication
                case 'youtube.com':
230 1
                    parse_str($parsedUrl['query'], $queryFields);
231 1
                    $code = $queryFields['v'];
232 1
                    $result = new Media();
233 1
                    $video = new RemoteVideoHelper($result);
234 1
                    $video->setType('youtube');
235 1
                    $video->setCode($code);
236 1
                    $result = $video->getMedia();
237 1
                    $result->setName('Youtube ' . $code);
238
239 1
                    break;
240
                case 'www.vimeo.com':
241 View Code Duplication
                case 'vimeo.com':
242
                    $code = substr($parsedUrl['path'], 1);
243
                    $result = new Media();
244
                    $video = new RemoteVideoHelper($result);
245
                    $video->setType('vimeo');
246
                    $video->setCode($code);
247
                    $result = $video->getMedia();
248
                    $result->setName('Vimeo ' . $code);
249
250
                    break;
251
                case 'www.dailymotion.com':
252 View Code Duplication
                case 'dailymotion.com':
253
                    $code = substr($parsedUrl['path'], 7);
254
                    $result = new Media();
255
                    $video = new RemoteVideoHelper($result);
256
                    $video->setType('dailymotion');
257
                    $video->setCode($code);
258
                    $result = $video->getMedia();
259
                    $result->setName('Dailymotion ' . $code);
260
261
                    break;
262
            }
263
        }
264
265 2
        return $result;
266
    }
267
268
    /**
269
     * {@inheritdoc}
270
     */
271
    public function getShowTemplate(Media $media)
272
    {
273
        return '@KunstmaanMedia/Media/RemoteVideo/show.html.twig';
274
    }
275
276
    /**
277
     * @param Media  $media    The media entity
278
     * @param string $basepath The base path
279
     *
280
     * @return string
281
     */
282
    public function getImageUrl(Media $media, $basepath)
283
    {
284
        $helper = new RemoteVideoHelper($media);
285
286
        return $helper->getThumbnailUrl();
287
    }
288
289
    /**
290
     * @return array
291
     */
292
    public function getAddFolderActions()
293
    {
294
        return array(
295
            RemoteVideoHandler::TYPE => array(
296
                'type' => RemoteVideoHandler::TYPE,
297
                'name' => 'media.video.add',
298
            ),
299
        );
300
    }
301
}
302