Completed
Push — master ( 847f63...4433bd )
by
unknown
02:42
created

VimeoProvider::getHelperProperties()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 52
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 9.4929
c 0
b 0
f 0
cc 2
eloc 23
nc 2
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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