Completed
Push — master ( efdf77...b67611 )
by Christian
7s
created

VimeoProvider::getHelperProperties()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 52
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 52
rs 8.6868
cc 5
eloc 23
nc 8
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
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\MediaBundle\Provider;
13
14
use Sonata\CoreBundle\Model\Metadata;
15
use Sonata\MediaBundle\Model\MediaInterface;
16
use Symfony\Component\HttpFoundation\RedirectResponse;
17
18
class VimeoProvider extends BaseVideoProvider
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function getHelperProperties(MediaInterface $media, $format, $options = array())
24
    {
25
        // documentation : http://vimeo.com/api/docs/moogaloop
26
        $defaults = array(
27
            // (optional) Flash Player version of app. Defaults to 9 .NEW!
28
            // 10 - New Moogaloop. 9 - Old Moogaloop without newest features.
29
            'fp_version'      => 10,
30
31
            // (optional) Enable fullscreen capability. Defaults to true.
32
            'fullscreen'      => true,
33
34
            // (optional) Show the byline on the video. Defaults to true.
35
            'title'           => true,
36
37
            // (optional) Show the title on the video. Defaults to true.
38
            'byline'          => 0,
39
40
            // (optional) Show the user's portrait on the video. Defaults to true.
41
            'portrait'        => true,
42
43
            // (optional) Specify the color of the video controls.
44
            'color'           => null,
45
46
            // (optional) Set to 1 to disable HD.
47
            'hd_off'          => 0,
48
49
            // Set to 1 to enable the Javascript API.
50
            'js_api'          => null,
51
52
            // (optional) JS function called when the player loads. Defaults to vimeo_player_loaded.
53
            'js_onLoad'       => 0,
54
55
            // Unique id that is passed into all player events as the ending parameter.
56
            'js_swf_id'       => uniqid('vimeo_player_'),
57
        );
58
59
        $player_parameters = array_merge($defaults, isset($options['player_parameters']) ? $options['player_parameters'] : array());
60
61
        $box = $this->getBoxHelperProperties($media, $format, $options);
62
63
        $params = array(
64
            'src'               => http_build_query($player_parameters),
65
            'id'                => $player_parameters['js_swf_id'],
66
            'frameborder'       => isset($options['frameborder']) ? $options['frameborder'] : 0,
67
            'width'             => $box->getWidth(),
68
            'height'            => $box->getHeight(),
69
            'class'             => isset($options['class']) ? $options['class'] : '',
70
            'allow_fullscreen'  => isset($options['allowfullscreen']) ? true : false,
71
        );
72
73
        return $params;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getProviderMetadata()
80
    {
81
        return new Metadata($this->getName(), $this->getName().'.description', false, 'SonataMediaBundle', array('class' => 'fa fa-vimeo-square'));
82
    }
83
84
    /**
85
     * @param MediaInterface $media
86
     */
87
    protected function fixBinaryContent(MediaInterface $media)
88
    {
89
        if (!$media->getBinaryContent()) {
90
            return;
91
        }
92
93
        if (preg_match("/vimeo\.com\/(video\/|)(\d+)/", $media->getBinaryContent(), $matches)) {
94
            $media->setBinaryContent($matches[2]);
95
        }
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    protected function doTransform(MediaInterface $media)
102
    {
103
        $this->fixBinaryContent($media);
104
105
        if (!$media->getBinaryContent()) {
106
            return;
107
        }
108
109
        // store provider information
110
        $media->setProviderName($this->name);
0 ignored issues
show
Bug introduced by
The property name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
111
        $media->setProviderReference($media->getBinaryContent());
112
        $media->setProviderStatus(MediaInterface::STATUS_OK);
113
114
        $this->updateMetadata($media, true);
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function updateMetadata(MediaInterface $media, $force = false)
121
    {
122
        $url = sprintf('http://vimeo.com/api/oembed.json?url=http://vimeo.com/%s', $media->getProviderReference());
123
124
        try {
125
            $metadata = $this->getMetadata($media, $url);
126
        } catch (\RuntimeException $e) {
127
            $media->setEnabled(false);
128
            $media->setProviderStatus(MediaInterface::STATUS_ERROR);
129
130
            return;
131
        }
132
133
        // store provider information
134
        $media->setProviderMetadata($metadata);
135
136
        // update Media common fields from metadata
137
        if ($force) {
138
            $media->setName($metadata['title']);
139
            $media->setDescription($metadata['description']);
140
            $media->setAuthorName($metadata['author_name']);
141
        }
142
143
        $media->setHeight($metadata['height']);
144
        $media->setWidth($metadata['width']);
145
        $media->setLength($metadata['duration']);
146
        $media->setContentType('video/x-flv');
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function getDownloadResponse(MediaInterface $media, $format, $mode, array $headers = array())
153
    {
154
        return new RedirectResponse(sprintf('http://vimeo.com/%s', $media->getProviderReference()), 302, $headers);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Symfony\Comp...nce()), 302, $headers); (Symfony\Component\HttpFoundation\RedirectResponse) is incompatible with the return type declared by the interface Sonata\MediaBundle\Provi...ce::getDownloadResponse of type Sonata\MediaBundle\Provider\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
155
    }
156
}
157