YoutubeServiceAdapter::getSmallThumbnail()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

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
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Ricardo Fiorani
5
 * Date: 29/08/2015
6
 * Time: 14:53.
7
 */
8
9
namespace RicardoFiorani\Adapter\Youtube;
10
11
use RicardoFiorani\Adapter\AbstractServiceAdapter;
12
use RicardoFiorani\Exception\InvalidThumbnailSizeException;
13
use RicardoFiorani\Renderer\EmbedRendererInterface;
14
15
class YoutubeServiceAdapter extends AbstractServiceAdapter
16
{
17
    const THUMBNAIL_DEFAULT = 'default';
18
    const THUMBNAIL_STANDARD_DEFINITION = 'sddefault';
19
    const THUMBNAIL_MEDIUM_QUALITY = 'mqdefault';
20
    const THUMBNAIL_HIGH_QUALITY = 'hqdefault';
21
    const THUMBNAIL_MAX_QUALITY = 'maxresdefault';
22
23
    /**
24
     * @param string $url
25
     * @param string $pattern
26
     * @param EmbedRendererInterface $renderer
27
     */
28
    public function __construct($url, $pattern, EmbedRendererInterface $renderer)
29
    {
30
        preg_match($pattern, $url, $match);
31
        if (isset($match[2])) {
32
            $videoId = $match[2];
33
        }
34
        if (empty($videoId)) {
35
            $videoId = $match[1];
36
        }
37
        $this->setVideoId($videoId);
38
39
        return parent::__construct($url, $pattern, $renderer);
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
40
    }
41
42
    /**
43
     * Returns the service name (ie: "Youtube" or "Vimeo").
44
     *
45
     * @return string
46
     */
47
    public function getServiceName()
48
    {
49
        return 'Youtube';
50
    }
51
52
    /**
53
     * Returns if the service has a thumbnail image.
54
     *
55
     * @return bool
56
     */
57
    public function hasThumbnail()
58
    {
59
        return true;
60
    }
61
62
    /**
63
     * @param string $size
64
     * @param bool $forceSecure
65
     * @return string
66
     * @throws InvalidThumbnailSizeException
67
     */
68 View Code Duplication
    public function getThumbnail($size, $forceSecure = false)
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...
69
    {
70
        if (false == in_array($size, $this->getThumbNailSizes())) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
71
            throw new InvalidThumbnailSizeException();
72
        }
73
74
        return $this->getScheme($forceSecure) . '://img.youtube.com/vi/' . $this->getVideoId() . '/' . $size . '.jpg';
75
    }
76
77
    /**
78
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
79
     */
80
    public function getThumbNailSizes()
81
    {
82
        return array(
83
            self::THUMBNAIL_DEFAULT,
84
            self::THUMBNAIL_STANDARD_DEFINITION,
85
            self::THUMBNAIL_MEDIUM_QUALITY,
86
            self::THUMBNAIL_HIGH_QUALITY,
87
            self::THUMBNAIL_MAX_QUALITY,
88
        );
89
    }
90
91
    /**
92
     * @param bool $forceAutoplay
93
     * @param bool $forceSecure
94
     * @return string
95
     */
96
    public function getEmbedUrl($forceAutoplay = false, $forceSecure = false)
97
    {
98
        $queryString = $this->generateQuerystring($forceAutoplay);
99
100
        return sprintf(
101
            '%s://www.youtube.com/embed/%s?%s',
102
            $this->getScheme($forceSecure),
103
            $this->getVideoId(),
104
            http_build_query($queryString)
105
        );
106
    }
107
108
    /**
109
     * Returns the small thumbnail's url.
110
     *
111
     * @return string
112
     */
113
    public function getSmallThumbnail($forceSecure = false)
114
    {
115
        return $this->getThumbnail(self::THUMBNAIL_STANDARD_DEFINITION, $forceSecure);
116
    }
117
118
    /**
119
     * Returns the medium thumbnail's url.
120
     *
121
     * @return string
122
     */
123
    public function getMediumThumbnail($forceSecure = false)
124
    {
125
        return $this->getThumbnail(self::THUMBNAIL_MEDIUM_QUALITY, $forceSecure);
126
    }
127
128
    /**
129
     * Returns the large thumbnail's url.
130
     *
131
     * @return string
132
     */
133
    public function getLargeThumbnail($forceSecure = false)
134
    {
135
        return $this->getThumbnail(self::THUMBNAIL_HIGH_QUALITY, $forceSecure);
136
    }
137
138
    /**
139
     * Returns the largest thumnbnaail's url.
140
     *
141
     * @return string
142
     */
143
    public function getLargestThumbnail($forceSecure = false)
144
    {
145
        return $this->getThumbnail(self::THUMBNAIL_MAX_QUALITY, $forceSecure);
146
    }
147
148
    /**
149
     * @return bool
150
     */
151
    public function isEmbeddable()
152
    {
153
        return true;
154
    }
155
156
    /**
157
     * @param bool $forceAutoplay
158
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
159
     */
160
    private function generateQuerystring($forceAutoplay = false)
161
    {
162
        parse_str(parse_url($this->rawUrl, PHP_URL_QUERY), $queryString);
163
        unset($queryString['v']);
164
165
        if ($forceAutoplay) {
166
            $queryString['autoplay'] = (int) $forceAutoplay;
167
        }
168
169
        return $queryString;
170
    }
171
}
172