Completed
Pull Request — master (#3)
by Joseph
01:59
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 3
Bugs 1 Features 1
Metric Value
c 3
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 static function get($url)
8
    {
9 57
        if (strpos($url, "youtu") !== false) {
10 9
            $link = static::youtubeFormat($url);
0 ignored issues
show
Bug introduced by
Since youtubeFormat() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of youtubeFormat() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
11 9
            if ($link !== false) {
12 6
                return $link;
13
            }
14 2
        }
15 51
        if (strpos($url, "vimeo") !== false) {
16 6
            $link = static::vimeoFormat($url);
0 ignored issues
show
Bug introduced by
Since vimeoFormat() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of vimeoFormat() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

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