Completed
Pull Request — master (#2094)
by Sander
50:34 queued 34:10
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
    /**
16
     * @var string
17
     */
18
    const CONTENT_TYPE = 'remote/video';
19
    /**
20
     * @var string
21
     */
22
    const TYPE = 'video';
23
    /**
24
     * @var array
25
     */
26
    protected $configuration = array();
27
28
    /**
29
     * Constructor. Takes the configuration of the RemoveVideoHandler
30
     * @param array $configuration
31
     */
32
    public function __construct($priority, $configuration = array())
33
    {
34
        parent::__construct($priority);
35
        $this->configuration = $configuration;
36
    }
37
38
    /**
39
     * @return string
40
     */
41
    public function getName()
42
    {
43
        return 'Remote Video Handler';
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function getType()
50
    {
51
        return RemoteVideoHandler::TYPE;
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getFormType()
58
    {
59
        return RemoteVideoType::class;
60
    }
61
62
    /**
63
     * Return the default form type options
64
     *
65
     * @return array
66
     */
67
    public function getFormTypeOptions()
68
    {
69
        return array('configuration' => $this->configuration);
70
    }
71
72
    /**
73
     * @param mixed $object
74
     *
75
     * @return bool
76
     */
77 View Code Duplication
    public function canHandle($object)
78
    {
79
        if (
80
            (is_string($object)) ||
81
            ($object instanceof Media && $object->getContentType() == RemoteVideoHandler::CONTENT_TYPE)
82
        ) {
83
            return true;
84
        }
85
86
        return false;
87
    }
88
89
    /**
90
     * @param Media $media
91
     *
92
     * @return RemoteVideoHelper
93
     */
94
    public function getFormHelper(Media $media)
95
    {
96
        return new RemoteVideoHelper($media);
97
    }
98
99
    /**
100
     * @param Media $media
101
     *
102
     * @throws \RuntimeException when the file does not exist
103
     */
104
    public function prepareMedia(Media $media)
105
    {
106
        if (null === $media->getUuid()) {
107
            $uuid = uniqid();
108
            $media->setUuid($uuid);
109
        }
110
        $video = new RemoteVideoHelper($media);
111
        $code = $video->getCode();
112
        //update thumbnail
113
        switch ($video->getType()) {
114
            case 'youtube':
115
                try {
116
                    if (@fopen('https://img.youtube.com/vi/'.$code.'/maxresdefault.jpg', 'r') === false) {
117
                        $video->setThumbnailUrl('https://img.youtube.com/vi/'.$code.'/0.jpg');
118
                    } else {
119
                        $video->setThumbnailUrl('https://img.youtube.com/vi/'.$code.'/maxresdefault.jpg');
120
                    }
121
                } catch (\Exception $e) {}
122
                break;
123
            case 'vimeo':
124
                try {
125
                    $xml = simplexml_load_file('https://vimeo.com/api/v2/video/' . $code . '.xml');
126
                    $video->setThumbnailUrl((string)$xml->video->thumbnail_large);
127
                } catch (\Exception $e) {
128
129
                }
130
                break;
131
            case 'dailymotion':
132
                try {
133
                    $json = json_decode(
134
                        file_get_contents('https://api.dailymotion.com/video/' . $code . '?fields=thumbnail_large_url')
135
                    );
136
                    $thumbnailUrl = $json->{'thumbnail_large_url'};
137
                    /* dirty hack to fix urls for imagine */
138
                    if (!$this->endsWith($thumbnailUrl, '.jpg') && !$this->endsWith($thumbnailUrl, '.png')) {
139
                        $thumbnailUrl = $thumbnailUrl . '&ext=.jpg';
140
                    }
141
                    $video->setThumbnailUrl($thumbnailUrl);
142
                } catch (\Exception $e) {
143
144
                }
145
                break;
146
        }
147
    }
148
149
    /**
150
     * String helper
151
     *
152
     * @param string $str string
153
     * @param string $sub substring
154
     *
155
     * @return boolean
156
     */
157
    private function endsWith($str, $sub)
158
    {
159
        return substr($str, strlen($str) - strlen($sub)) === $sub;
160
    }
161
162
    /**
163
     * @param Media $media
164
     */
165
    public function saveMedia(Media $media)
166
    {
167
    }
168
169
    /**
170
     * @param Media $media
171
     */
172
    public function removeMedia(Media $media)
173
    {
174
    }
175
176
    /**
177
     * {@inheritDoc}
178
     */
179
    public function updateMedia(Media $media)
180
    {
181
182
183
    }
184
185
    /**
186
     * @param array $params
187
     *
188
     * @return array
189
     */
190
    public function getAddUrlFor(array $params = array())
191
    {
192
        return array(
193
            'video' => array(
194
                'path' => 'KunstmaanMediaBundle_folder_videocreate',
195
                'params' => array(
196
                    'folderId' => $params['folderId']
197
                )
198
            )
199
        );
200
    }
201
202
    /**
203
     * @param mixed $data
204
     *
205
     * @return Media
206
     */
207
    public function createNew($data)
208
    {
209
        $result = null;
210
        if (is_string($data)) {
211
            if (strpos($data, 'http') !== 0) {
212
                $data = 'http://' . $data;
213
            }
214
            $parsedUrl = parse_url($data);
215
            switch ($parsedUrl['host']) {
216 View Code Duplication
                case 'youtu.be':
0 ignored issues
show
This code seems to be duplicated across your project.

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.

Loading history...
217
                    $code = substr($parsedUrl['path'], 1); // remove slash
218
                    $result = new Media();
219
                    $video = new RemoteVideoHelper($result);
220
                    $video->setType('youtube');
221
                    $video->setCode($code);
222
                    $result = $video->getMedia();
223
                    $result->setName('Youtube ' . $code);
224
                    break;
225
226
                case 'www.youtube.com':
227 View Code Duplication
                case 'youtube.com':
228
                    parse_str($parsedUrl['query'], $queryFields);
229
                    $code = $queryFields['v'];
230
                    $result = new Media();
231
                    $video = new RemoteVideoHelper($result);
232
                    $video->setType('youtube');
233
                    $video->setCode($code);
234
                    $result = $video->getMedia();
235
                    $result->setName('Youtube ' . $code);
236
                    break;
237
                case 'www.vimeo.com':
238 View Code Duplication
                case 'vimeo.com':
239
                    $code = substr($parsedUrl['path'], 1);
240
                    $result = new Media();
241
                    $video = new RemoteVideoHelper($result);
242
                    $video->setType('vimeo');
243
                    $video->setCode($code);
244
                    $result = $video->getMedia();
245
                    $result->setName('Vimeo ' . $code);
246
                    break;
247
                case 'www.dailymotion.com':
248 View Code Duplication
                case 'dailymotion.com':
249
                    $code = substr($parsedUrl['path'], 7);
250
                    $result = new Media();
251
                    $video = new RemoteVideoHelper($result);
252
                    $video->setType('dailymotion');
253
                    $video->setCode($code);
254
                    $result = $video->getMedia();
255
                    $result->setName('Dailymotion ' . $code);
256
                    break;
257
            }
258
        }
259
260
        return $result;
261
    }
262
263
    /**
264
     * {@inheritDoc}
265
     */
266
    public function getShowTemplate(Media $media)
267
    {
268
        return 'KunstmaanMediaBundle:Media\RemoteVideo:show.html.twig';
269
    }
270
271
    /**
272
     * @param Media $media The media entity
273
     * @param string $basepath The base path
274
     *
275
     * @return string
276
     */
277
    public function getImageUrl(Media $media, $basepath)
278
    {
279
        $helper = new RemoteVideoHelper($media);
280
281
        return $helper->getThumbnailUrl();
282
    }
283
284
    /**
285
     * @return array
286
     */
287
    public function getAddFolderActions()
288
    {
289
        return array(
290
            RemoteVideoHandler::TYPE => array(
291
                'type' => RemoteVideoHandler::TYPE,
292
                'name' => 'media.video.add'
293
            )
294
        );
295
    }
296
}
297