Completed
Push — master ( 062693...6b78cb )
by
unknown
10:53
created

YouTubeProvider   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 95
Duplicated Lines 22.11 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 96.77%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 14
lcom 0
cbo 3
dl 21
loc 95
ccs 30
cts 31
cp 0.9677
rs 10
c 3
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getOembedUrl() 0 4 1
A getImageUrl() 0 10 2
A getIcon() 0 4 1
A getName() 0 4 1
A getType() 0 4 1
A getEmbedTemplate() 0 4 1
C parseProviderReference() 21 27 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Provider;
4
5
use MediaMonks\SonataMediaBundle\Exception\InvalidProviderUrlException;
6
7
class YouTubeProvider extends AbstractOembedProvider implements ProviderInterface
8
{
9
    const URL_OEMBED = 'http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=%s&format=json';
10
    const URL_IMAGE_MAX_RES = 'https://i.ytimg.com/vi/%s/maxresdefault.jpg';
11
    const URL_IMAGE_HQ = 'https://i.ytimg.com/vi/%s/hqdefault.jpg';
12
13
    /**
14
     * @param string $id
15
     * @return string
16
     */
17 2
    public function getOembedUrl($id)
18
    {
19 2
        return sprintf(self::URL_OEMBED, $id);
20
    }
21
22
    /**
23
     * @param string $id
24
     * @return string
25
     */
26 1
    public function getImageUrl($id)
27
    {
28
        // try to get max res image (only available for 720P videos)
29 1
        $urlMaxRes = sprintf(self::URL_IMAGE_MAX_RES, $id);
30 1
        if ($this->getHttpClient()->exists($urlMaxRes)) {
31
            return $urlMaxRes;
32
        }
33
34 1
        return sprintf(self::URL_IMAGE_HQ, $id); // this one always exists
35
    }
36
37
    /**
38
     * @param $value
39
     * @return string
40
     * @throws \Exception
41
     */
42 6
    public function parseProviderReference($value)
43
    {
44 6 View Code Duplication
        if (strpos($value, 'youtube.com')) {
0 ignored issues
show
Duplication introduced by
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...
45 4
            $url = parse_url($value);
46 4
            if (empty($url['query'])) {
47 1
                throw new InvalidProviderUrlException('Youtube');
48
            }
49 3
            parse_str($url['query'], $params);
50 3
            if (empty($params['v'])) {
51 1
                throw new InvalidProviderUrlException('Youtube');
52
            }
53
54 2
            return $params['v'];
55
        }
56
57 4 View Code Duplication
        if (strpos($value, 'youtu.be')) {
0 ignored issues
show
Duplication introduced by
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...
58 2
            $url = parse_url($value);
59 2
            if (empty($url['path']) || empty(trim($url['path'], '/'))) {
60 1
                throw new InvalidProviderUrlException('Youtube');
61
            }
62 1
            $id = trim($url['path'], '/');
63
64 1
            return $id;
65
        }
66
67 3
        return $value;
68
    }
69
70
    /**
71
     * @return string
72
     */
73 12
    public function getIcon()
74
    {
75 12
        return 'fa fa-youtube-play';
76
    }
77
78
    /**
79
     * @return string
80
     */
81 13
    public function getName()
82
    {
83 13
        return 'youtube';
84
    }
85
86
    /**
87
     * @return string
88
     */
89 2
    public function getType()
90
    {
91 2
        return AbstractProvider::TYPE_VIDEO;
92
    }
93
94
    /**
95
     * @return string
96
     */
97 1
    public function getEmbedTemplate()
98
    {
99 1
        return 'MediaMonksSonataMediaBundle:Provider:youtube_embed.html.twig';
100
    }
101
}
102