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

AbstractServiceAdapter::getScheme()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 6
cts 7
cp 0.8571
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.0261
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 55
    public function __construct(string $url, string $pattern, EmbedRendererInterface $renderer)
17
    {
18 55
        $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 55
        $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 55
        $this->renderer = $renderer;
21 55
    }
22
23 2
    public function getRawUrl(): string
24
    {
25 2
        return $this->rawUrl;
26
    }
27
28 1
    public function setRawUrl(string $rawUrl)
29
    {
30 1
        $this->rawUrl = $rawUrl;
31 1
    }
32
33 19
    public function getVideoId(): string
34
    {
35 19
        return $this->videoId;
36
    }
37
38 55
    public function setVideoId(string $videoId)
39
    {
40 55
        $this->videoId = $videoId;
41 55
    }
42
43 2
    public function getPattern(): string
44
    {
45 2
        return $this->pattern;
46
    }
47
48 1
    public function setPattern(string $pattern)
49
    {
50 1
        $this->pattern = $pattern;
51 1
    }
52
53 2
    public function getRenderer(): EmbedRendererInterface
54
    {
55 2
        return $this->renderer;
56
    }
57
58 1
    public function setRenderer(EmbedRendererInterface $renderer)
59
    {
60 1
        $this->renderer = $renderer;
61 1
    }
62
63
    /**
64
     * @throws NotEmbeddableException
65
     */
66 1
    public function getEmbedCode(
67
        int $width,
68
        int $height,
69
        bool $forceAutoplay = false,
70
        bool $forceSecure = false
71
    ): string {
72 1
        if (false === $this->isEmbeddable()) {
73
            throw new NotEmbeddableException(
74
                sprintf('The service "%s" does not provide embeddable videos', $this->getServiceName())
75
            );
76
        }
77
78 1
        return $this->getRenderer()->renderVideoEmbedCode(
79 1
            $this->getEmbedUrl($forceAutoplay, $forceSecure),
80 1
            $width,
81 1
            $height
82
        );
83
    }
84
85
    /**
86
     * @throws InvalidUrlException
87
     */
88 14
    public function getScheme(bool $forceSecure = false): string
89
    {
90 14
        if ($forceSecure) {
91 5
            return 'https';
92
        }
93
94 14
        $parsedUrlSchema = parse_url($this->rawUrl, PHP_URL_SCHEME);
95
96 14
        if (false !== $parsedUrlSchema) {
97 14
            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