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

VimeoServiceAdapter   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 3
dl 0
loc 171
ccs 63
cts 66
cp 0.9545
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A getServiceName() 0 4 1
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 setThumbnails() 0 6 2
A getThumbnail() 0 13 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 13 2
1
<?php declare(strict_types=1);
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';
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...
14
    const THUMBNAIL_MEDIUM = 'thumbnail_medium';
15
    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...
16
17
    public $title;
18
    public $description;
19
    public $thumbnails;
20
21
    /**
22
     * @throws ServiceApiNotAvailable
23
     */
24 12
    public function __construct(string $url, string $pattern, EmbedRendererInterface $renderer)
25
    {
26 12
        $videoId = $this->getVideoIdByPattern($url, $pattern);
27 12
        $this->setVideoId($videoId);
28 12
        $videoData = $this->getVideoDataFromServiceApi();
29
30 12
        $this->setThumbnails(array(
31 12
            self::THUMBNAIL_SMALL => $videoData[self::THUMBNAIL_SMALL],
32 12
            self::THUMBNAIL_MEDIUM => $videoData[self::THUMBNAIL_MEDIUM],
33 12
            self::THUMBNAIL_LARGE => $videoData[self::THUMBNAIL_LARGE],
34
        ));
35
36 12
        $this->setTitle($videoData['title']);
37 12
        $this->setDescription($videoData['description']);
38
39 12
        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...
40
    }
41
42 1
    public function getServiceName(): string
43
    {
44 1
        return 'Vimeo';
45
    }
46
47 1
    public function hasThumbnail(): bool
48
    {
49 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...
50
    }
51
52 1
    public function getTitle(): string
53
    {
54 1
        return $this->title;
55
    }
56
57 12
    public function setTitle(string $title)
58
    {
59 12
        $this->title = $title;
60 12
    }
61
62 1
    public function getDescription(): string
63
    {
64 1
        return $this->description;
65
    }
66
67 12
    public function setDescription(string $description)
68
    {
69 12
        $this->description = $description;
70 12
    }
71
72 12
    private function setThumbnails(array $thumbnails)
73
    {
74 12
        foreach ($thumbnails as $key => $thumbnail) {
75 12
            $this->thumbnails[$key] = parse_url($thumbnail);
76
        }
77 12
    }
78
79
    /**
80
     * @throws InvalidThumbnailSizeException
81
     * @throws InvalidUrlException
82
     */
83 2
    public function getThumbnail(string $size, bool $forceSecure = false): string
84
    {
85 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...
86 1
            throw new InvalidThumbnailSizeException();
87
        }
88
89 1
        return sprintf(
90 1
            '%s://%s%s',
91 1
            $this->getScheme($forceSecure),
92 1
            $this->thumbnails[$size]['host'],
93 1
            $this->thumbnails[$size]['path']
94
        );
95
    }
96
97
    /**
98
     * @throws InvalidUrlException
99
     */
100 2
    public function getEmbedUrl(bool $forceAutoplay = false, bool $forceSecure = false): string
101
    {
102 2
        return $this->getScheme($forceSecure) . '://player.vimeo.com/video/' . $this->getVideoId() . ($forceAutoplay ? '?autoplay=1' : '');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 139 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...
103
    }
104
105 3
    public function getThumbNailSizes(): array
106
    {
107
        return array(
108 3
            self::THUMBNAIL_SMALL,
109 3
            self::THUMBNAIL_MEDIUM,
110 3
            self::THUMBNAIL_LARGE,
111
        );
112
    }
113
114
    /**
115
     * @throws InvalidThumbnailSizeException
116
     * @throws InvalidUrlException
117
     */
118 1
    public function getSmallThumbnail(bool $forceSecure = false): string
119
    {
120 1
        return $this->getThumbnail(self::THUMBNAIL_SMALL,$forceSecure);
121
    }
122
123
    /**
124
     * @throws InvalidThumbnailSizeException
125
     * @throws InvalidUrlException
126
     */
127 1
    public function getMediumThumbnail(bool $forceSecure = false): string
128
    {
129 1
        return $this->getThumbnail(self::THUMBNAIL_MEDIUM,$forceSecure);
130
    }
131
132
    /**
133
     * @throws InvalidThumbnailSizeException
134
     * @throws InvalidUrlException
135
     */
136 1
    public function getLargeThumbnail(bool $forceSecure = false): string
137
    {
138 1
        return $this->getThumbnail(self::THUMBNAIL_LARGE,$forceSecure);
139
    }
140
141
    /**
142
     * @throws InvalidThumbnailSizeException
143
     * @throws InvalidUrlException
144
     */
145 1
    public function getLargestThumbnail(bool $forceSecure = false): string
146
    {
147 1
        return $this->getThumbnail(self::THUMBNAIL_LARGE,$forceSecure);
148
    }
149
150 1
    public function isEmbeddable(): bool
151
    {
152 1
        return true;
153
    }
154
155 12
    private function getVideoIdByPattern(string $url, string $pattern): string
156
    {
157 12
        $match = array();
158 12
        preg_match($pattern, $url, $match);
159 12
        $videoId = $match[2];
160
161 12
        return $videoId;
162
    }
163
164
    /**
165
     * TODO make this better by using guzzle
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
166
     * @throws ServiceApiNotAvailable
167
     */
168 12
    private function getVideoDataFromServiceApi(): array
169
    {
170 12
        $contents = file_get_contents('http://vimeo.com/api/v2/video/' . $this->getVideoId() . '.php');
171 12
        if (false === $contents) {
172
            throw new ServiceApiNotAvailable(
173
                'Service "%s" could not reach it\'s API. Check if file_get_contents() function is available.',
174
                $this->getServiceName()
175
            );
176
        }
177 12
        $hash = unserialize($contents);
178
179 12
        return reset($hash);
180
    }
181
}
182