Completed
Branch master (415647)
by Ricardo
01:39
created

YoutubeServiceAdapter::generateQuerystring()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 7
cp 0.7143
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2.0932
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';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
18
    const THUMBNAIL_STANDARD_DEFINITION = 'sddefault';
19
    const THUMBNAIL_MEDIUM_QUALITY = 'mqdefault';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
20
    const THUMBNAIL_HIGH_QUALITY = 'hqdefault';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
21
    const THUMBNAIL_MAX_QUALITY = 'maxresdefault';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
22
23
    /**
24
     * @param string $url
25
     * @param string $pattern
26
     * @param EmbedRendererInterface $renderer
27
     */
28 14
    public function __construct($url, $pattern, EmbedRendererInterface $renderer)
29
    {
30 14
        preg_match($pattern, $url, $match);
31 14
        if (isset($match[2])) {
32 10
            $videoId = $match[2];
33 10
        }
34 14
        if (empty($videoId)) {
35 4
            $videoId = $match[1];
36 4
        }
37 14
        $this->setVideoId($videoId);
38
39 14
        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 1
    public function getServiceName()
48
    {
49 1
        return 'Youtube';
50
    }
51
52
    /**
53
     * Returns if the service has a thumbnail image.
54
     *
55
     * @return bool
56
     */
57 1
    public function hasThumbnail()
58
    {
59 1
        return true;
60
    }
61
62
    /**
63
     * @param string $size
64
     * @param bool $forceSecure
65
     * @return string
66
     * @throws InvalidThumbnailSizeException
67
     */
68 2 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 2
        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 1
            throw new InvalidThumbnailSizeException();
72
        }
73
74 1
        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 3
    public function getThumbNailSizes()
81
    {
82
        return array(
83 3
            self::THUMBNAIL_DEFAULT,
84 3
            self::THUMBNAIL_STANDARD_DEFINITION,
85 3
            self::THUMBNAIL_MEDIUM_QUALITY,
86 3
            self::THUMBNAIL_HIGH_QUALITY,
87 3
            self::THUMBNAIL_MAX_QUALITY,
88 3
        );
89
    }
90
91
    /**
92
     * @param bool $forceAutoplay
93
     * @param bool $forceSecure
94
     * @return string
95
     */
96 2
    public function getEmbedUrl($forceAutoplay = false, $forceSecure = false)
97
    {
98 2
        $queryString = $this->generateQuerystring($forceAutoplay);
99
100 2
        return sprintf(
101 2
            '%s://www.youtube.com/embed/%s?%s',
102 2
            $this->getScheme($forceSecure),
103 2
            $this->getVideoId(),
104 2
            http_build_query($queryString)
105 2
        );
106
    }
107
108
    /**
109
     * Returns the small thumbnail's url.
110
     *
111
     * @return string
112
     */
113 1
    public function getSmallThumbnail($forceSecure = false)
114
    {
115 1
        return $this->getThumbnail(self::THUMBNAIL_STANDARD_DEFINITION, $forceSecure);
116
    }
117
118
    /**
119
     * Returns the medium thumbnail's url.
120
     *
121
     * @return string
122
     */
123 1
    public function getMediumThumbnail($forceSecure = false)
124
    {
125 1
        return $this->getThumbnail(self::THUMBNAIL_MEDIUM_QUALITY, $forceSecure);
126
    }
127
128
    /**
129
     * Returns the large thumbnail's url.
130
     *
131
     * @return string
132
     */
133 1
    public function getLargeThumbnail($forceSecure = false)
134
    {
135 1
        return $this->getThumbnail(self::THUMBNAIL_HIGH_QUALITY, $forceSecure);
136
    }
137
138
    /**
139
     * Returns the largest thumnbnaail's url.
140
     *
141
     * @return string
142
     */
143 1
    public function getLargestThumbnail($forceSecure = false)
144
    {
145 1
        return $this->getThumbnail(self::THUMBNAIL_MAX_QUALITY, $forceSecure);
146
    }
147
148
    /**
149
     * @return bool
150
     */
151 1
    public function isEmbeddable()
152
    {
153 1
        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 2
    private function generateQuerystring($forceAutoplay = false)
161
    {
162 2
        parse_str(parse_url($this->rawUrl, PHP_URL_QUERY), $queryString);
163 2
        unset($queryString['v']);
164
165 2
        if ($forceAutoplay) {
166
            $queryString['autoplay'] = (int) $forceAutoplay;
167
        }
168
169 2
        return $queryString;
170
    }
171
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
172