VimeoDownloader::getType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Jackal\Downloader\Ext\Vimeo\Downloader;
4
5
use Jackal\Downloader\Downloader\AbstractDownloader;
6
use Jackal\Downloader\Ext\Vimeo\Exception\VimeoDownloadException;
7
8
class VimeoDownloader extends AbstractDownloader
9
{
10
    public function getURL(): string
11
    {
12
13
        $links = array_reduce($this->getVideoInfo(), function ($vimeoVideos, $currentVimeoVideo) {
14
            $quality = substr($currentVimeoVideo['quality'], 0, -1);
15
            $vimeoVideos[$quality] = $currentVimeoVideo['url'];
16
17
            return $vimeoVideos;
18
        }, []);
19
20
        ksort($links, SORT_NUMERIC);
21
22
        if ($links == []) {
23
            throw VimeoDownloadException::videoURLsNotFound();
24
        }
25
26
        $links = $this->filterByFormats($links);
27
28
        return array_values($links)[0];
29
    }
30
31
    private function getVideoInfo() : array
32
    {
33
        $requestUrl = $this->getRequestedUrl();
34
        $requestUrlContent = file_get_contents($requestUrl);
35
        $decode_to_arr = json_decode($requestUrlContent, true);
36
37
        $contentToReturn = @$decode_to_arr['request']['files']['progressive'];
38
        if (!$decode_to_arr or !is_array($contentToReturn)) {
39
            throw VimeoDownloadException::unableToParseContent($requestUrl, $requestUrlContent);
40
        }
41
42
        return $contentToReturn;
43
    }
44
45
    private function getRequestedUrl() : string
46
    {
47
        $originUrl = 'https://www.vimeo.com/' . $this->getVideoId();
48
        $data = file_get_contents($originUrl);
49
50
        $url = $this->executeRegex($data, '/"config_url"\:"(.*)"/U');
51
        $url = trim(str_replace('\\/', '/', $url));
52
53
        if (!$url or !filter_var($url, FILTER_VALIDATE_URL)) {
54
            throw VimeoDownloadException::unableToParseContent($originUrl, $url);
55
        }
56
57
        return $url;
58
    }
59
60
    protected function executeRegex($string, $regex, $group = 1) : ?string
61
    {
62
        preg_match($regex, $string, $matches);
63
        if (!array_key_exists($group, $matches)) {
64
            return null;
65
        }
66
67
        return $matches[$group];
68
    }
69
70
    public static function getPublicUrlRegex(): string
71
    {
72
        return '/vimeo\.com(?:\/video\/|\/)(\d+)/i';
73
    }
74
75
    public static function getType(): string
76
    {
77
        return 'vimeo';
78
    }
79
}
80