Completed
Push — master ( 4b2a2e...a7f05a )
by Ricardo
02:17
created

VimeoServiceAdapter   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 239
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 98.41%

Importance

Changes 8
Bugs 3 Features 1
Metric Value
wmc 22
c 8
b 3
f 1
lcom 1
cbo 3
dl 0
loc 239
ccs 62
cts 63
cp 0.9841
rs 10

18 Methods

Rating   Name   Duplication   Size   Complexity  
A hasThumbnail() 0 4 1
A getTitle() 0 4 1
A setTitle() 0 4 1
A getDescription() 0 4 1
A setDescription() 0 4 1
A __construct() 0 17 1
A getServiceName() 0 4 1
A setThumbnails() 0 6 2
A getThumbnail() 0 8 2
A getEmbedUrl() 0 4 2
A getThumbNailSizes() 0 8 1
A getSmallThumbnail() 0 4 1
A getMediumThumbnail() 0 4 1
A getLargeThumbnail() 0 4 1
A getLargestThumbnail() 0 4 1
A isEmbeddable() 0 4 1
A getVideoIdByPattern() 0 8 1
A getVideoDataFromServiceApi() 0 10 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Ricardo Fiorani
5
 * Date: 29/08/2015
6
 * Time: 14:56.
7
 */
8
namespace RicardoFiorani\Adapter\Vimeo;
9
10
use RicardoFiorani\Adapter\AbstractServiceAdapter;
11
use RicardoFiorani\Exception\InvalidThumbnailSizeException;
12
use RicardoFiorani\Exception\ServiceApiNotAvailable;
13
use RicardoFiorani\Renderer\EmbedRendererInterface;
14
15
class VimeoServiceAdapter extends AbstractServiceAdapter
16
{
17
    const THUMBNAIL_SMALL = 'thumbnail_small';
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...
18
    const THUMBNAIL_MEDIUM = 'thumbnail_medium';
19
    const THUMBNAIL_LARGE = 'thumbnail_large';
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
21
    /**
22
     * @var string
23
     */
24
    public $title;
25
26
    /**
27
     * @var string
28
     */
29
    public $description;
30
31
    /**
32
     * @var array
33
     */
34
    public $thumbnails;
35
36
    /**
37
     * @param string $url
38
     * @param string $pattern
39
     * @param EmbedRendererInterface $renderer
40
     */
41 12
    public function __construct($url, $pattern, EmbedRendererInterface $renderer)
42
    {
43 12
        $videoId = $this->getVideoIdByPattern($url, $pattern);
44 12
        $this->setVideoId($videoId);
45 12
        $videoData = $this->getVideoDataFromServiceApi();
46
47 12
        $this->setThumbnails(array(
48 12
            self::THUMBNAIL_SMALL => $videoData[self::THUMBNAIL_SMALL],
49 12
            self::THUMBNAIL_MEDIUM => $videoData[self::THUMBNAIL_MEDIUM],
50 12
            self::THUMBNAIL_LARGE => $videoData[self::THUMBNAIL_LARGE],
51 12
        ));
52
53 12
        $this->setTitle($videoData['title']);
54 12
        $this->setDescription($videoData['description']);
55
56 12
        return parent::__construct($url, $pattern, $renderer);
57
    }
58
59
    /**
60
     * Returns the service name (ie: "Youtube" or "Vimeo").
61
     *
62
     * @return string
63
     */
64 1
    public function getServiceName()
65
    {
66 1
        return 'Vimeo';
67
    }
68
69
    /**
70
     * Returns if the service has a thumbnail image.
71
     *
72
     * @return bool
73
     */
74 1
    public function hasThumbnail()
75
    {
76 1
        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...
77
    }
78
79
    /**
80
     * @return string
81
     */
82 1
    public function getTitle()
83
    {
84 1
        return $this->title;
85
    }
86
87
    /**
88
     * @param string $title
89
     */
90 12
    public function setTitle($title)
91
    {
92 12
        $this->title = $title;
93 12
    }
94
95
    /**
96
     * @return string
97
     */
98 1
    public function getDescription()
99
    {
100 1
        return $this->description;
101
    }
102
103
    /**
104
     * @param string $description
105
     */
106 12
    public function setDescription($description)
107
    {
108 12
        $this->description = $description;
109 12
    }
110
111
    /**
112
     * @param array $thumbnails
113
     */
114 12
    private function setThumbnails(array $thumbnails)
115
    {
116 12
        foreach ($thumbnails as $key => $thumbnail) {
117 12
            $this->thumbnails[$key] = parse_url($thumbnail);
118 12
        }
119 12
    }
120
121
    /**
122
     * @param string $size
123
     *
124
     * @return string
125
     *
126
     * @throws InvalidThumbnailSizeException
127
     */
128 2
    public function getThumbnail($size, $secure = false)
129
    {
130 2
        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...
131 1
            throw new InvalidThumbnailSizeException();
132
        }
133
134 1
        return $this->getScheme($secure) . '://' . $this->thumbnails[$size]['host'] . $this->thumbnails[$size]['path'];
135
    }
136
137
    /**
138
     * @param bool $autoplay
139
     *
140
     * @return string
141
     */
142 2
    public function getEmbedUrl($autoplay = false, $secure = false)
143
    {
144 2
        return $this->getScheme($secure) . '://player.vimeo.com/video/' . $this->getVideoId() . ($autoplay ? '?autoplay=1' : '');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
145
    }
146
147
    /**
148
     * Returns all thumbnails available sizes.
149
     *
150
     * @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...
151
     */
152 3
    public function getThumbNailSizes()
153
    {
154
        return array(
155 3
            self::THUMBNAIL_SMALL,
156 3
            self::THUMBNAIL_MEDIUM,
157 3
            self::THUMBNAIL_LARGE,
158 3
        );
159
    }
160
161
    /**
162
     * Returns the small thumbnail's url.
163
     *
164
     * @param bool $secure
165
     * @return string
166
     * @throws InvalidThumbnailSizeException
167
     */
168 1
    public function getSmallThumbnail($secure = false)
169
    {
170 1
        return $this->getThumbnail(self::THUMBNAIL_SMALL,$secure);
171
    }
172
173
    /**
174
     * Returns the medium thumbnail's url.
175
     *
176
     * @param bool $secure
177
     * @return string
178
     * @throws InvalidThumbnailSizeException
179
     */
180 1
    public function getMediumThumbnail($secure = false)
181
    {
182 1
        return $this->getThumbnail(self::THUMBNAIL_MEDIUM,$secure);
183
    }
184
185
    /**
186
     * Returns the large thumbnail's url.
187
     *
188
     * @param bool $secure
189
     * @param $secure
190
     * @return string
191
     * @throws InvalidThumbnailSizeException
192
     */
193 1
    public function getLargeThumbnail($secure = false)
194
    {
195 1
        return $this->getThumbnail(self::THUMBNAIL_LARGE,$secure);
196
    }
197
198
    /**
199
     * Returns the largest thumnbnaail's url.
200
     *
201
     * @param bool $secure
202
     * @param $secure
203
     * @return string
204
     * @throws InvalidThumbnailSizeException
205
     */
206 1
    public function getLargestThumbnail($secure = false)
207
    {
208 1
        return $this->getThumbnail(self::THUMBNAIL_LARGE,$secure);
209
    }
210
211
    /**
212
     * @return bool
213
     */
214 1
    public function isEmbeddable()
215
    {
216 1
        return true;
217
    }
218
219
    /**
220
     * @param string $url
221
     * @param string $pattern
222
     *
223
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
224
     */
225 12
    private function getVideoIdByPattern($url, $pattern)
226
    {
227 12
        $match = array();
228 12
        preg_match($pattern, $url, $match);
229 12
        $videoId = $match[2];
230
231 12
        return $videoId;
232
    }
233
234
    /**
235
     * Uses the Vimeo video API to get video info.
236
     *
237
     * @todo make this better by using guzzle
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
238
     *
239
     * @return array
240
     *
241
     * @throws ServiceApiNotAvailable
242
     */
243 12
    private function getVideoDataFromServiceApi()
244
    {
245 12
        $contents = file_get_contents('http://vimeo.com/api/v2/video/' . $this->getVideoId() . '.php');
246 12
        if (false === $contents) {
247
            throw new ServiceApiNotAvailable('Vimeo Service Adapter could not reach Vimeo API Service. Check if your server has file_get_contents() function available.');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 170 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
248
        }
249 12
        $hash = unserialize($contents);
250
251 12
        return reset($hash);
252
    }
253
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
254