Completed
Pull Request — master (#4)
by Joseph
03:16
created

Media::vimeoFormat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 9.4285
cc 2
eloc 12
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Jclyons52\PagePreview;
4
5
class Media
6
{
7 57
    public function __construct(HttpInterface $http)
8
    {
9 57
        $this->http = $http;
0 ignored issues
show
Bug introduced by
The property http does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
10 57
    }
11
12 57
    public function get($url)
13
    {
14 57
        if (strpos($url, "youtu") !== false) {
15 9
            $link = $this->youtubeFormat($url);
16 9
            if ($link !== false) {
17 6
                return $link;
18
            }
19 2
        }
20 51
        if (strpos($url, "vimeo") !== false) {
21 6
            $link = $this->vimeoFormat($url);
22 6
            if ($link !== false) {
23 3
                return $link;
24
            }
25 2
        }
26 48
        return [];
27
    }
28
29
    /**
30
     * @param $url
31
     * @return array|bool
32
     */
33 9
    private function youtubeFormat($url)
34
    {
35 9
        $id = null;
36
        $re = "/https?:\\/\\/(?:www\\.)?youtu(?:be\\.com\\/(watch\\?)?".
37 9
            "(.*?&?)?v=|\\.be\\/)([\\w\\-]+)(&(amp;)?[\\w\\?=]*)?/i";
38 9
        if (preg_match($re, $url, $matching)) {
39 6
            $id = $matching[3];
40 4
        } else {
41 6
            $re = "/https?:\\/\\/(?:www\\.)?youtube\\.com\\/embed\\/([^&|^?|^\\/]*)/i";
42 6
            if (preg_match($re, $url, $matching)) {
43 3
                $id = $matching[1];
44 2
            }
45
        }
46 9
        if ($id !== null) {
47
            return [
48 6
                'url' => 'https://www.youtube.com/embed/' . $id,
49 6
                'thumbnail' => "http://i2.ytimg.com/vi/{$id}/default.jpg",
50 4
            ];
51
        }
52 3
        return false;
53
    }
54
55
    /**
56
     * @param $url
57
     * @param $matches
58
     * @return array
59
     */
60 6
    private function vimeoFormat($url)
61
    {
62
        $re = "/https?:\\/\\/(?:www\\.|player\\.)?vimeo.com\\/" .
63 4
            "(?:channels\\/(?:\\w+\\/)?|groups\\/([^\\/]*)\\/videos\\/".
64 6
            "|album\\/(\\d+)\\/video\\/|video\\/|)(\\d+)(?:$|\\/|\\?)?/";
65 6
        preg_match_all($re, $url, $matches);
66
67 6
        if (!$matches[3]) {
68 3
            return false;
69
        }
70 3
        $imgId = $matches[3][0];
71
72 3
        $hash = $this->http->get("http://vimeo.com/api/v2/video/{$imgId}.json");
73
74
        return [
75 3
            'url'        => "http://player.vimeo.com/video/{$imgId}",
76 3
            'thumbnail' => $hash[0]->thumbnail_large,
77 2
        ];
78
    }
79
}
80