Completed
Push — master ( d3fff0...eb77f8 )
by
unknown
10:47
created

YouTubeProvider::parseProviderReference()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
ccs 0
cts 20
cp 0
rs 8.5125
cc 5
eloc 14
nc 5
nop 1
crap 30
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 Video';
88
    }
89
90
    public function getType()
91
    {
92
        return 'youtube';
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getEmbedTemplate()
99
    {
100
        return 'MediaMonksSonataMediaBundle:Provider:youtube_embed.html.twig';
101
    }
102
}
103