Completed
Push — master ( ac02e0...0e5803 )
by
unknown
04:21
created

YouTubeProvider::getReferenceLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Provider;
4
5
use MediaMonks\SonataMediaBundle\Entity\Media;
6
use Sonata\AdminBundle\Form\FormMapper;
7
use Sonata\CoreBundle\Validator\ErrorElement;
8
use Symfony\Component\Form\Extension\Core\Type\TextType;
9
use Symfony\Component\HttpFoundation\Response;
10
11
class YouTubeProvider extends AbstractOembedProvider implements ProviderInterface
12
{
13
    const URL_OEMBED = 'http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=%s&format=json';
14
    const URL_IMAGE_MAX_RES = 'https://i.ytimg.com/vi/%s/maxresdefault.jpg';
15
    const URL_IMAGE_HQ = 'https://i.ytimg.com/vi/%s/hqdefault.jpg';
16
17
    /**
18
     * @param string $id
19
     * @return string
20
     */
21
    public function getOembedUrl($id)
22
    {
23
        return sprintf(self::URL_OEMBED, $id);
24
    }
25
26
    /**
27
     * @param string $id
28
     * @return string
29
     */
30
    public function getImageUrl($id)
31
    {
32
        // try to get max res image (only available for 720P videos)
33
        $urlMaxRes = sprintf(self::URL_IMAGE_MAX_RES, $id);
34
        stream_context_set_default(['http' => ['method' => 'HEAD']]);
35
        $headers = get_headers($urlMaxRes);
36
        stream_context_set_default(['http' => ['method' => 'GET']]);
37
        if ((int)substr($headers[0], 9, 3) === Response::HTTP_OK) {
38
            return $urlMaxRes;
39
        }
40
41
        return sprintf(self::URL_IMAGE_HQ, $id); // this one always exists
42
    }
43
44
    /**
45
     * @param $value
46
     * @return string
47
     * @throws \Exception
48
     */
49
    public function parseProviderReference($value)
50
    {
51
        if (strpos($value, 'youtube.com')) {
52
            $url = parse_url($value);
53
            if (empty($url['query'])) {
54
                throw new \Exception('The supplied URL does not look like a Youtube URL');
55
            }
56
            parse_str($url['query'], $params);
57
            if (empty($params['v'])) {
58
                throw new \Exception('The supplied URL does not look like a Youtube URL');
59
            }
60
61
            return $params['v'];
62
        }
63
64
        if (strpos($value, 'youtu.be')) {
65
            $url = parse_url($value);
66
            $id = substr($url['path'], 1);
67
68
            return $id;
69
        }
70
71
        return $value;
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function getIcon()
78
    {
79
        return 'fa fa-youtube-play';
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getTitle()
86
    {
87
        return 'YouTube';
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    public function getName()
94
    {
95
        return 'youtube';
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function getType()
102
    {
103
        return AbstractProvider::TYPE_VIDEO;
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    public function getEmbedTemplate()
110
    {
111
        return 'MediaMonksSonataMediaBundle:Provider:youtube_embed.html.twig';
112
    }
113
114
    /**
115
     * @return string
116
     */
117
    public function getReferenceLabel()
118
    {
119
        return 'form.youtube.reference';
120
    }
121
}
122