Completed
Pull Request — master (#8)
by Ricardo
03:59
created

AbstractServiceAdapter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 88.1%

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 3
dl 0
loc 99
ccs 37
cts 42
cp 0.881
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getRawUrl() 0 4 1
A setRawUrl() 0 4 1
A getVideoId() 0 4 1
A setVideoId() 0 4 1
A getPattern() 0 4 1
A setPattern() 0 4 1
A getRenderer() 0 4 1
A setRenderer() 0 4 1
A getEmbedCode() 0 18 2
A getScheme() 0 14 3
A isThumbnailSizeAvailable() 0 4 1
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 56
    public function __construct(string $url, string $pattern, EmbedRendererInterface $renderer)
17
    {
18 56
        $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 56
        $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 56
        $this->renderer = $renderer;
21 56
    }
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 56
    public function setVideoId(string $videoId)
39
    {
40 56
        $this->videoId = $videoId;
41 56
    }
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