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

DailyMotionProvider::getHelperProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 64
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 9.3956
c 0
b 0
f 0
cc 1
eloc 24
nc 1
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 DailyMotionProvider extends BaseVideoProvider
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function getHelperProperties(MediaInterface $media, $format, $options = [])
26
    {
27
        // documentation : http://www.dailymotion.com/en/doc/api/player
28
29
        $defaults = [
30
            // Values: 0 or 1. Default is 0. Determines if the player loads related videos when
31
            // the current video begins playback.
32
            'related' => 0,
33
34
            // Values: 0 or 1. Default is 1. Determines if the player allows explicit content to
35
            // be played. This parameter may be added to embed code by platforms which do not
36
            // want explicit content to be posted by their users.
37
            'explicit' => 0,
38
39
            // Values: 0 or 1. Default is 0. Determines if the video will begin playing
40
            // automatically when the player loads.
41
            'autoPlay' => 0,
42
43
            // Values: 0 or 1. Default is 0. Determines if the video will begin muted.
44
            'autoMute' => 0,
45
46
            // Values: 0 or 1. Default is 0. Determines if the video will unmuted on mouse over.
47
            // Of course it works only if the player is on automute=1.
48
            'unmuteOnMouseOver' => 0,
49
50
            // Values: a number of seconds. Default is 0. Determines if the video will begin
51
            // playing the video at a given time.
52
            'start' => 0,
53
54
            // Values: 0 or 1. Default is 0. Enable the Javascript API by setting this parameter
55
            // to 1. For more information and instructions on using the Javascript API, see the
56
            // JavaScript API documentation.
57
            'enableApi' => 0,
58
59
            // Values: 0 or 1. Default is 0. Determines if the player should display controls
60
            // or not during video playback.
61
            'chromeless' => 0,
62
63
            // Values: 0 or 1. Default is 0. Determines if the video should be expended to fit
64
            // the whole player's size.
65
            'expendVideo' => 0,
66
            'color2' => null,
67
68
            // Player color changes may be set using color codes. A color is described by its
69
            // hexadecimal value (eg: FF0000 for red).
70
            'foreground' => null,
71
            'background' => null,
72
            'highlight' => null,
73
        ];
74
75
        $player_parameters = array_merge($defaults, $options['player_parameters'] ?? []);
76
77
        $box = $this->getBoxHelperProperties($media, $format, $options);
78
79
        $params = [
80
            'player_parameters' => http_build_query($player_parameters),
81
            'allowFullScreen' => $options['allowFullScreen'] ?? 'true',
82
            'allowScriptAccess' => $options['allowScriptAccess'] ?? 'always',
83
            'width' => $box->getWidth(),
84
            'height' => $box->getHeight(),
85
        ];
86
87
        return $params;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function getProviderMetadata()
94
    {
95
        return new Metadata($this->getName(), $this->getName().'.description', 'bundles/sonatamedia/dailymotion-icon.png', 'SonataMediaBundle');
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function updateMetadata(MediaInterface $media, $force = false): void
102
    {
103
        $url = sprintf('http://www.dailymotion.com/services/oembed?url=%s&format=json', $this->getReferenceUrl($media));
104
105
        try {
106
            $metadata = $this->getMetadata($media, $url);
107
        } catch (\RuntimeException $e) {
108
            $media->setEnabled(false);
109
            $media->setProviderStatus(MediaInterface::STATUS_ERROR);
110
111
            return;
112
        }
113
114
        $media->setProviderMetadata($metadata);
115
116
        if ($force) {
117
            $media->setName($metadata['title']);
118
            $media->setAuthorName($metadata['author_name']);
119
        }
120
121
        $media->setHeight($metadata['height']);
122
        $media->setWidth($metadata['width']);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function getDownloadResponse(MediaInterface $media, $format, $mode, array $headers = [])
129
    {
130
        return new RedirectResponse($this->getReferenceUrl($media), 302, $headers);
131
    }
132
133
    /**
134
     * Get provider reference url.
135
     *
136
     * @param MediaInterface $media
137
     *
138
     * @return string
139
     */
140
    public function getReferenceUrl(MediaInterface $media)
141
    {
142
        return sprintf('http://www.dailymotion.com/video/%s', $media->getProviderReference());
143
    }
144
145
    /**
146
     * @param MediaInterface $media
147
     */
148
    protected function fixBinaryContent(MediaInterface $media): void
149
    {
150
        if (!$media->getBinaryContent()) {
151
            return;
152
        }
153
154
        if (preg_match("/www.dailymotion.com\/video\/([0-9a-zA-Z]*)_/", $media->getBinaryContent(), $matches)) {
155
            $media->setBinaryContent($matches[1]);
156
        }
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162
    protected function doTransform(MediaInterface $media): void
163
    {
164
        $this->fixBinaryContent($media);
165
166
        if (!$media->getBinaryContent()) {
167
            return;
168
        }
169
170
        $media->setProviderName($this->name);
171
        $media->setProviderStatus(MediaInterface::STATUS_OK);
172
        $media->setProviderReference($media->getBinaryContent());
173
174
        $this->updateMetadata($media, true);
175
    }
176
}
177