Completed
Pull Request — master (#8)
by Ricardo
05:26
created

YoutubeServiceAdapter::generateQuerystring()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0185
1
<?php declare(strict_types=1);
2
3
namespace RicardoFiorani\Adapter\Youtube;
4
5
use RicardoFiorani\Adapter\AbstractServiceAdapter;
6
use RicardoFiorani\Adapter\Exception\InvalidThumbnailSizeException;
7
use RicardoFiorani\Renderer\EmbedRendererInterface;
8
9
class YoutubeServiceAdapter extends AbstractServiceAdapter
10
{
11
    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...
12
    const THUMBNAIL_STANDARD_DEFINITION = 'sddefault';
13
    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...
14
    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...
15
    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...
16
17 14
    public function __construct(string $url, string $pattern, EmbedRendererInterface $renderer)
18
    {
19 14
        preg_match($pattern, $url, $match);
20 14
        if (isset($match[2])) {
21 10
            $videoId = $match[2];
22
        }
23 14
        if (empty($videoId)) {
24 4
            $videoId = $match[1];
25
        }
26 14
        $this->setVideoId($videoId);
27
28 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...
29
    }
30
31 1
    public function getServiceName(): string
32
    {
33 1
        return 'Youtube';
34
    }
35
36 1
    public function hasThumbnail(): bool
37
    {
38 1
        return true;
39
    }
40
41
    /**
42
     * @throws InvalidThumbnailSizeException
43
     */
44 2 View Code Duplication
    public function getThumbnail(string $size, bool $forceSecure = false): string
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...
45
    {
46 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...
47 1
            throw new InvalidThumbnailSizeException();
48
        }
49
50 1
        return $this->getScheme($forceSecure) . '://img.youtube.com/vi/' . $this->getVideoId() . '/' . $size . '.jpg';
51
    }
52
53 3
    public function getThumbNailSizes(): array
54
    {
55
        return array(
56 3
            self::THUMBNAIL_DEFAULT,
57 3
            self::THUMBNAIL_STANDARD_DEFINITION,
58 3
            self::THUMBNAIL_MEDIUM_QUALITY,
59 3
            self::THUMBNAIL_HIGH_QUALITY,
60 3
            self::THUMBNAIL_MAX_QUALITY,
61
        );
62
    }
63
64 2
    public function getEmbedUrl(bool $forceAutoplay = false, bool $forceSecure = false): string
65
    {
66 2
        $queryString = $this->generateQuerystring($forceAutoplay);
67
68 2
        return sprintf(
69 2
            '%s://www.youtube.com/embed/%s?%s',
70 2
            $this->getScheme($forceSecure),
71 2
            $this->getVideoId(),
72 2
            http_build_query($queryString)
73
        );
74
    }
75
76 1
    public function getSmallThumbnail(bool $forceSecure = false): string
77
    {
78 1
        return $this->getThumbnail(self::THUMBNAIL_STANDARD_DEFINITION, $forceSecure);
79
    }
80
81 1
    public function getMediumThumbnail(bool $forceSecure = false): string
82
    {
83 1
        return $this->getThumbnail(self::THUMBNAIL_MEDIUM_QUALITY, $forceSecure);
84
    }
85
86 1
    public function getLargeThumbnail(bool $forceSecure = false): string
87
    {
88 1
        return $this->getThumbnail(self::THUMBNAIL_HIGH_QUALITY, $forceSecure);
89
    }
90
91 1
    public function getLargestThumbnail(bool $forceSecure = false): string
92
    {
93 1
        return $this->getThumbnail(self::THUMBNAIL_MAX_QUALITY, $forceSecure);
94
    }
95
96 1
    public function isEmbeddable(): bool
97
    {
98 1
        return true;
99
    }
100
101 2
    private function generateQuerystring(bool $forceAutoplay = false): array
102
    {
103 2
        parse_str(parse_url($this->rawUrl, PHP_URL_QUERY), $queryString);
104 2
        unset($queryString['v']);
105
106 2
        if ($forceAutoplay) {
107
            $queryString['autoplay'] = (int) $forceAutoplay;
108
        }
109
110 2
        return $queryString;
111
    }
112
}
113