Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
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 = [];
27
28
    /**
29
     * Constructor. Takes the configuration of the RemoveVideoHandler
30
     *
31
     * @param array $configuration
32
     */
33 2
    public function __construct($priority, $configuration = [])
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 ['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
     * @return RemoteVideoHelper
92
     */
93
    public function getFormHelper(Media $media)
94
    {
95
        return new RemoteVideoHelper($media);
96
    }
97
98
    /**
99
     * @throws \RuntimeException when the file does not exist
100
     */
101
    public function prepareMedia(Media $media)
102
    {
103
        if (null === $media->getUuid()) {
104
            $uuid = uniqid();
105
            $media->setUuid($uuid);
106
        }
107
        $video = new RemoteVideoHelper($media);
108
        $code = $video->getCode();
109
        //update thumbnail
110
        switch ($video->getType()) {
111
            case 'youtube':
112
                try {
113
                    if (@fopen('https://img.youtube.com/vi/' . $code . '/maxresdefault.jpg', 'r') === false) {
114
                        $video->setThumbnailUrl('https://img.youtube.com/vi/' . $code . '/0.jpg');
115
                    } else {
116
                        $video->setThumbnailUrl('https://img.youtube.com/vi/' . $code . '/maxresdefault.jpg');
117
                    }
118
                } catch (\Exception $e) {
119
                }
120
121
                break;
122
            case 'vimeo':
123
                try {
124
                    $xml = simplexml_load_file('https://vimeo.com/api/v2/video/' . $code . '.xml');
125
                    $video->setThumbnailUrl((string) $xml->video->thumbnail_large);
126
                } catch (\Exception $e) {
127
                }
128
129
                break;
130
            case 'dailymotion':
131
                try {
132
                    $json = json_decode(
133
                        file_get_contents('https://api.dailymotion.com/video/' . $code . '?fields=thumbnail_large_url')
134
                    );
135
                    $thumbnailUrl = $json->{'thumbnail_large_url'};
136
                    /* dirty hack to fix urls for imagine */
137
                    if (!$this->endsWith($thumbnailUrl, '.jpg') && !$this->endsWith($thumbnailUrl, '.png')) {
138
                        $thumbnailUrl = $thumbnailUrl . '&ext=.jpg';
139
                    }
140
                    $video->setThumbnailUrl($thumbnailUrl);
141
                } catch (\Exception $e) {
142
                }
143
144
                break;
145
        }
146
    }
147
148
    /**
149
     * String helper
150
     *
151
     * @param string $str string
152
     * @param string $sub substring
153
     *
154
     * @return bool
155
     */
156
    private function endsWith($str, $sub)
157
    {
158
        return substr($str, \strlen($str) - \strlen($sub)) === $sub;
159
    }
160
161
    public function saveMedia(Media $media)
162
    {
163
    }
164
165
    public function removeMedia(Media $media)
166
    {
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function updateMedia(Media $media)
173
    {
174
    }
175
176
    /**
177
     * @return array
178
     */
179
    public function getAddUrlFor(array $params = [])
180
    {
181
        return [
182
            'video' => [
183
                'path' => 'KunstmaanMediaBundle_folder_videocreate',
184
                'params' => [
185
                    'folderId' => $params['folderId'],
186
                ],
187
            ],
188
        ];
189
    }
190
191
    /**
192
     * @param mixed $data
193
     *
194
     * @return Media
195
     */
196 2
    public function createNew($data)
197
    {
198 2
        $result = null;
199 2
        if (\is_string($data)) {
200 2
            if (strncmp($data, 'http', 4) !== 0) {
201
                $data = 'http://' . $data;
202
            }
203 2
            $parsedUrl = parse_url($data);
204 2
            switch ($parsedUrl['host']) {
205 2 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...
206 1
                    $code = substr($parsedUrl['path'], 1); // remove slash
207 1
                    $result = new Media();
208 1
                    $video = new RemoteVideoHelper($result);
209 1
                    $video->setType('youtube');
210 1
                    $video->setCode($code);
211 1
                    $result = $video->getMedia();
212 1
                    $result->setName('Youtube ' . $code);
213
214 1
                    break;
215
216 1
                case 'www.youtube.com':
217 View Code Duplication
                case 'youtube.com':
218 1
                    parse_str($parsedUrl['query'], $queryFields);
219 1
                    $code = $queryFields['v'];
220 1
                    $result = new Media();
221 1
                    $video = new RemoteVideoHelper($result);
222 1
                    $video->setType('youtube');
223 1
                    $video->setCode($code);
224 1
                    $result = $video->getMedia();
225 1
                    $result->setName('Youtube ' . $code);
226
227 1
                    break;
228
                case 'www.vimeo.com':
229 View Code Duplication
                case 'vimeo.com':
230
                    $code = substr($parsedUrl['path'], 1);
231
                    $result = new Media();
232
                    $video = new RemoteVideoHelper($result);
233
                    $video->setType('vimeo');
234
                    $video->setCode($code);
235
                    $result = $video->getMedia();
236
                    $result->setName('Vimeo ' . $code);
237
238
                    break;
239
                case 'www.dailymotion.com':
240 View Code Duplication
                case 'dailymotion.com':
241
                    $code = substr($parsedUrl['path'], 7);
242
                    $result = new Media();
243
                    $video = new RemoteVideoHelper($result);
244
                    $video->setType('dailymotion');
245
                    $video->setCode($code);
246
                    $result = $video->getMedia();
247
                    $result->setName('Dailymotion ' . $code);
248
249
                    break;
250
            }
251
        }
252
253 2
        return $result;
254
    }
255
256
    /**
257
     * {@inheritdoc}
258
     */
259
    public function getShowTemplate(Media $media)
260
    {
261
        return '@KunstmaanMedia/Media/RemoteVideo/show.html.twig';
262
    }
263
264
    /**
265
     * @param Media  $media    The media entity
266
     * @param string $basepath The base path
267
     *
268
     * @return string
269
     */
270
    public function getImageUrl(Media $media, $basepath)
271
    {
272
        $helper = new RemoteVideoHelper($media);
273
274
        return $helper->getThumbnailUrl();
275
    }
276
277
    /**
278
     * @return array
279
     */
280
    public function getAddFolderActions()
281
    {
282
        return [
283
            RemoteVideoHandler::TYPE => [
284
                'type' => RemoteVideoHandler::TYPE,
285
                'name' => 'media.video.add',
286
            ],
287
        ];
288
    }
289
}
290