Completed
Pull Request — master (#18)
by Ricardo
05:45
created

AbstractServiceAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Ricardo Fiorani
5
 * Date: 29/08/2015
6
 * Time: 19:37.
7
 */
8
9
namespace RicardoFiorani\Adapter;
10
11
use RicardoFiorani\Adapter\Exception\InvalidUrlException;
12
use RicardoFiorani\Adapter\Exception\NotEmbeddableException;
13
use RicardoFiorani\Renderer\EmbedRendererInterface;
14
15
abstract class AbstractServiceAdapter implements VideoAdapterInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    public $rawUrl;
21
22
    /**
23
     * @var string
24
     */
25
    public $videoId;
26
27
    /**
28
     * @var string
29
     */
30
    public $pattern;
31
32
    /**
33
     * @var EmbedRendererInterface
34
     */
35
    public $renderer;
36
37
    /**
38
     * AbstractVideoAdapter constructor.
39
     *
40
     * @param string $url
41
     * @param string $pattern
42
     * @param EmbedRendererInterface $renderer
43
     */
44
    public function __construct($url, $pattern, EmbedRendererInterface $renderer)
45
    {
46
        $this->rawUrl = $url;
47
        $this->pattern = $pattern;
48
        $this->renderer = $renderer;
49
    }
50
51
    /**
52
     * Returns the input URL.
53
     *
54
     * @return string
55
     */
56
    public function getRawUrl()
57
    {
58
        return $this->rawUrl;
59
    }
60
61
    /**
62
     * @param string $rawUrl
63
     */
64
    public function setRawUrl($rawUrl)
65
    {
66
        $this->rawUrl = $rawUrl;
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    public function getVideoId()
73
    {
74
        return $this->videoId;
75
    }
76
77
    /**
78
     * @param string $videoId
79
     */
80
    public function setVideoId($videoId)
81
    {
82
        $this->videoId = $videoId;
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getPattern()
89
    {
90
        return $this->pattern;
91
    }
92
93
    /**
94
     * @param string $pattern
95
     */
96
    public function setPattern($pattern)
97
    {
98
        $this->pattern = $pattern;
99
    }
100
101
    /**
102
     * @return EmbedRendererInterface
103
     */
104
    public function getRenderer()
105
    {
106
        return $this->renderer;
107
    }
108
109
    /**
110
     * @param EmbedRendererInterface $renderer
111
     */
112
    public function setRenderer($renderer)
113
    {
114
        $this->renderer = $renderer;
115
    }
116
117
    /**
118
     * @param int $width
119
     * @param int $height
120
     * @param bool $forceAutoplay
121
     * @param bool $forceSecure
122
     *
123
     * @return string
124
     * @throws NotEmbeddableException
125
     */
126
    public function getEmbedCode($width, $height, $forceAutoplay = false, $forceSecure = false)
127
    {
128
        if (false === $this->isEmbeddable()) {
129
            throw new NotEmbeddableException(
130
                sprintf('The service "%s" does not provide embeddable videos', $this->getServiceName())
131
            );
132
        }
133
134
        return $this->getRenderer()->renderVideoEmbedCode(
135
            $this->getEmbedUrl($forceAutoplay, $forceSecure),
136
            $width,
137
            $height
138
        );
139
    }
140
141
    /**
142
     * Switches the protocol scheme between http and https in case you want to force https
143
     *
144
     * @param bool|false $forceSecure
145
     * @return string
146
     * @throws InvalidUrlException
147
     */
148
    public function getScheme($forceSecure = false)
149
    {
150
        if ($forceSecure) {
151
            return 'https';
152
        }
153
154
        $parsedUrlSchema = parse_url($this->rawUrl, PHP_URL_SCHEME);
155
156
        if (false !== $parsedUrlSchema) {
157
            return $parsedUrlSchema;
158
        }
159
160
        throw new InvalidUrlException(sprintf('The URL %s is not valid', $this->rawUrl));
161
    }
162
163
    public function isThumbnailSizeAvailable($intendedSize)
164
    {
165
        return in_array($intendedSize, $this->getThumbNailSizes());
166
    }
167
}
168