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

YouTubeProvider::getOembedUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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