YouTubeProvider   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 107
Duplicated Lines 21.5 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 3
dl 23
loc 107
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getOembedUrl() 0 4 1
A getImageUrl() 0 10 2
A parseProviderReference() 0 12 3
A parseProviderReferenceFromUrl() 13 13 3
A parseProviderReferenceFromShortUrl() 10 10 3
A getIcon() 0 4 1
A getName() 0 4 1
A getType() 0 4 1

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, EmbeddableProviderInterface
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): string
18
    {
19 2
        return sprintf(self::URL_OEMBED, $id);
20
    }
21
22
    /**
23
     * @param string $id
24
     * @return string
25
     */
26 3
    public function getImageUrl($id): string
27
    {
28
        // try to get max res image (only available for 720P videos)
29 3
        $urlMaxRes = sprintf(self::URL_IMAGE_MAX_RES, $id);
30 3
        if ($this->getHttpClient()->exists($urlMaxRes)) {
31 2
            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): string
43
    {
44 6
        if (strpos($value, 'youtube.com')) {
45 4
            return $this->parseProviderReferenceFromUrl($value);
46
        }
47
48 4
        if (strpos($value, 'youtu.be')) {
49 2
            return $this->parseProviderReferenceFromShortUrl($value);
50
        }
51
52 3
        return $value;
53
    }
54
55
    /**
56
     * @param string $url
57
     * @return mixed
58
     * @throws InvalidProviderUrlException
59
     */
60 4 View Code Duplication
    protected function parseProviderReferenceFromUrl($url): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
61
    {
62 4
        $url = parse_url($url);
63 4
        if (empty($url['query'])) {
64 1
            throw new InvalidProviderUrlException('Youtube');
65
        }
66 3
        parse_str($url['query'], $params);
67 3
        if (empty($params['v'])) {
68 1
            throw new InvalidProviderUrlException('Youtube');
69
        }
70
71 2
        return $params['v'];
72
    }
73
74
    /**
75
     * @param string $url
76
     * @return string
77
     * @throws InvalidProviderUrlException
78
     */
79 2 View Code Duplication
    protected function parseProviderReferenceFromShortUrl($url): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
80
    {
81 2
        $url = parse_url($url);
82 2
        if (empty($url['path']) || empty(trim($url['path'], '/'))) {
83 1
            throw new InvalidProviderUrlException('Youtube');
84
        }
85 1
        $id = trim($url['path'], '/');
86
87 1
        return $id;
88
    }
89
90
    /**
91
     * @return string
92
     */
93 12
    public function getIcon(): string
94
    {
95 12
        return 'fa fa-youtube-play';
96
    }
97
98
    /**
99
     * @return string
100
     */
101 13
    public function getName(): string
102
    {
103 13
        return 'youtube';
104
    }
105
106
    /**
107
     * @return string
108
     */
109 2
    public function getType(): string
110
    {
111 2
        return AbstractProvider::TYPE_VIDEO;
112
    }
113
}
114