Completed
Pull Request — master (#8)
by Ricardo
01:45
created

AbstractServiceAdapter::isThumbnailSizeAvailable()   A

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 declare(strict_types=1);
2
3
namespace RicardoFiorani\Adapter;
4
5
use RicardoFiorani\Adapter\Exception\InvalidUrlException;
6
use RicardoFiorani\Adapter\Exception\NotEmbeddableException;
7
use RicardoFiorani\Renderer\EmbedRendererInterface;
8
9
abstract class AbstractServiceAdapter implements VideoAdapterInterface
10
{
11
    public $rawUrl;
12
    public $videoId;
13
    public $pattern;
14
    public $renderer;
15
16
    public function __construct(string $url, string $pattern, EmbedRendererInterface $renderer)
17
    {
18
        $this->rawUrl = $url;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 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...
19
        $this->pattern = $pattern;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 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
        $this->renderer = $renderer;
21
    }
22
23
    public function getRawUrl(): string
24
    {
25
        return $this->rawUrl;
26
    }
27
28
    public function setRawUrl(string $rawUrl)
29
    {
30
        $this->rawUrl = $rawUrl;
31
    }
32
33
    public function getVideoId(): string
34
    {
35
        return $this->videoId;
36
    }
37
38
    public function setVideoId(string $videoId)
39
    {
40
        $this->videoId = $videoId;
41
    }
42
43
    public function getPattern(): string
44
    {
45
        return $this->pattern;
46
    }
47
48
    public function setPattern(string $pattern)
49
    {
50
        $this->pattern = $pattern;
51
    }
52
53
    public function getRenderer(): EmbedRendererInterface
54
    {
55
        return $this->renderer;
56
    }
57
58
    public function setRenderer(EmbedRendererInterface $renderer)
59
    {
60
        $this->renderer = $renderer;
61
    }
62
63
    /**
64
     * @throws NotEmbeddableException
65
     */
66
    public function getEmbedCode(
67
        int $width,
68
        int $height,
69
        bool $forceAutoplay = false,
70
        bool $forceSecure = false
71
    ): string {
72
        if (false === $this->isEmbeddable()) {
73
            throw new NotEmbeddableException(
74
                sprintf('The service "%s" does not provide embeddable videos', $this->getServiceName())
75
            );
76
        }
77
78
        return $this->getRenderer()->renderVideoEmbedCode(
79
            $this->getEmbedUrl($forceAutoplay, $forceSecure),
80
            $width,
81
            $height
82
        );
83
    }
84
85
    /**
86
     * @throws InvalidUrlException
87
     */
88
    public function getScheme(bool $forceSecure = false): string
89
    {
90
        if ($forceSecure) {
91
            return 'https';
92
        }
93
94
        $parsedUrlSchema = parse_url($this->rawUrl, PHP_URL_SCHEME);
95
96
        if (false !== $parsedUrlSchema) {
97
            return $parsedUrlSchema;
98
        }
99
100
        throw new InvalidUrlException(sprintf('The URL %s is not valid', $this->rawUrl));
101
    }
102
103
    public function isThumbnailSizeAvailable($intendedSize): bool
104
    {
105
        return in_array($intendedSize, $this->getThumbNailSizes());
106
    }
107
}
108