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

VimeoServiceAdapter::getVideoIdByPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
3
namespace RicardoFiorani\Adapter\Vimeo;
4
5
use RicardoFiorani\Adapter\AbstractServiceAdapter;
6
use RicardoFiorani\Adapter\Exception\InvalidThumbnailSizeException;
7
use RicardoFiorani\Adapter\Exception\InvalidUrlException;
8
use RicardoFiorani\Adapter\Exception\ServiceApiNotAvailable;
9
use RicardoFiorani\Renderer\EmbedRendererInterface;
10
11
class VimeoServiceAdapter extends AbstractServiceAdapter
12
{
13
    const THUMBNAIL_SMALL = 'thumbnail_small';
14
    const THUMBNAIL_MEDIUM = 'thumbnail_medium';
15
    const THUMBNAIL_LARGE = 'thumbnail_large';
16
17
    /**
18
     * @var string
19
     */
20
    public $title;
21
22
    /**
23
     * @var string
24
     */
25
    public $description;
26
27
    /**
28
     * @var array
29
     */
30
    public $thumbnails;
31
32
    /**
33
     * @param string $url
34
     * @param string $pattern
35
     * @param EmbedRendererInterface $renderer
36
     * @throws ServiceApiNotAvailable
37
     */
38
    public function __construct($url, $pattern, EmbedRendererInterface $renderer)
39
    {
40
        $videoData = $this->getVideoDataFromServiceApi($url);
41
        $videoId = $videoData->video_id;
42
        $this->setVideoId($videoId);
43
44
        $this->setThumbnails(array(
45
            //oEmbed only provides one size now :(
46
            self::THUMBNAIL_SMALL => $videoData->thumbnail_url,
47
            self::THUMBNAIL_MEDIUM => $videoData->thumbnail_url,
48
            self::THUMBNAIL_LARGE => $videoData->thumbnail_url,
49
        ));
50
51
        $this->setTitle($videoData->title);
52
        $this->setDescription($videoData->description);
53
54
        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...
55
    }
56
57
    /**
58
     * Returns the service name (ie: "Youtube" or "Vimeo").
59
     *
60
     * @return string
61
     */
62
    public function getServiceName()
63
    {
64
        return 'Vimeo';
65
    }
66
67
    /**
68
     * Returns if the service has a thumbnail image.
69
     *
70
     * @return bool
71
     */
72
    public function hasThumbnail()
73
    {
74
        return false == empty($this->thumbnails);
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...
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function getTitle()
81
    {
82
        return $this->title;
83
    }
84
85
    /**
86
     * @param string $title
87
     */
88
    public function setTitle($title)
89
    {
90
        $this->title = $title;
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function getDescription()
97
    {
98
        return $this->description;
99
    }
100
101
    /**
102
     * @param string $description
103
     */
104
    public function setDescription($description)
105
    {
106
        $this->description = $description;
107
    }
108
109
    /**
110
     * @param array $thumbnails
111
     */
112
    private function setThumbnails(array $thumbnails)
113
    {
114
        foreach ($thumbnails as $key => $thumbnail) {
115
            $this->thumbnails[$key] = parse_url($thumbnail);
116
        }
117
    }
118
119
    /**
120
     * @param string $size
121
     * @param bool $forceSecure
122
     * @return string
123
     *
124
     * @throws InvalidThumbnailSizeException
125
     * @throws InvalidUrlException
126
     */
127
    public function getThumbnail($size, $forceSecure = false)
128
    {
129
        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...
130
            throw new InvalidThumbnailSizeException();
131
        }
132
133
        return sprintf(
134
            '%s://%s%s',
135
            $this->getScheme($forceSecure),
136
            $this->thumbnails[$size]['host'],
137
            $this->thumbnails[$size]['path']
138
        );
139
    }
140
141
    /**
142
     * @param bool $forceAutoplay
143
     *
144
     * @return string
145
     * @throws InvalidUrlException
146
     */
147
    public function getEmbedUrl($forceAutoplay = false, $forceSecure = false)
148
    {
149
        return $this->getScheme($forceSecure) . '://player.vimeo.com/video/' . $this->getVideoId() . ($forceAutoplay ? '?autoplay=1' : '');
150
    }
151
152
    /**
153
     * Returns all thumbnails available sizes.
154
     *
155
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
156
     */
157
    public function getThumbNailSizes()
158
    {
159
        return array(
160
            self::THUMBNAIL_SMALL,
161
            self::THUMBNAIL_MEDIUM,
162
            self::THUMBNAIL_LARGE,
163
        );
164
    }
165
166
    /**
167
     * Returns the small thumbnail's url.
168
     *
169
     * @param bool $forceSecure
170
     * @return string
171
     * @throws InvalidThumbnailSizeException
172
     * @throws InvalidUrlException
173
     */
174
    public function getSmallThumbnail($forceSecure = false)
175
    {
176
        return $this->getThumbnail(self::THUMBNAIL_SMALL,$forceSecure);
177
    }
178
179
    /**
180
     * Returns the medium thumbnail's url.
181
     *
182
     * @param bool $forceSecure
183
     * @return string
184
     * @throws InvalidThumbnailSizeException
185
     * @throws InvalidUrlException
186
     */
187
    public function getMediumThumbnail($forceSecure = false)
188
    {
189
        return $this->getThumbnail(self::THUMBNAIL_MEDIUM,$forceSecure);
190
    }
191
192
    /**
193
     * Returns the large thumbnail's url.
194
     *
195
     * @param bool $forceSecure
196
     * @return string
197
     * @throws InvalidThumbnailSizeException
198
     * @throws InvalidUrlException
199
     */
200
    public function getLargeThumbnail($forceSecure = false)
201
    {
202
        return $this->getThumbnail(self::THUMBNAIL_LARGE,$forceSecure);
203
    }
204
205
    /**
206
     * Returns the largest thumnbnaail's url.
207
     *
208
     * @param bool $forceSecure
209
     * @return string
210
     * @throws InvalidThumbnailSizeException
211
     * @throws InvalidUrlException
212
     */
213
    public function getLargestThumbnail($forceSecure = false)
214
    {
215
        return $this->getThumbnail(self::THUMBNAIL_LARGE,$forceSecure);
216
    }
217
218
    /**
219
     * @return bool
220
     */
221
    public function isEmbeddable()
222
    {
223
        return true;
224
    }
225
226
    /**
227
     * Uses the Vimeo video API to get video info.
228
     *
229
     * @todo make this better by using guzzle
230
     *
231
     * @return array
232
     *
233
     * @throws ServiceApiNotAvailable
234
     */
235
    private function getVideoDataFromServiceApi($url)
236
    {
237
        $contents = file_get_contents('https://vimeo.com/api/oembed.json?url=' . urlencode($url));
238
        if (false === $contents) {
239
            throw new ServiceApiNotAvailable(
240
                'Service "%s" could not reach it\'s API. Check if file_get_contents() function is available.',
241
                $this->getServiceName()
242
            );
243
        }
244
245
        return json_decode($contents);
246
    }
247
}
248