Wistia   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 101
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B getDownloadUrl() 0 38 5
A getWistiaID() 0 12 2
1
<?php
2
/**
3
 * Wistia.net functions
4
 */
5
6
namespace App\Http;
7
8
use App\Exceptions\NoWistiaIDException;
9
use App\Utils\Utils;
10
use GuzzleHttp\Client;
11
use GuzzleHttp\Cookie\CookieJar;
12
use Ubench;
13
14
/**
15
 * Class Wistia
16
 * @package App\Http
17
 */
18
class Wistia
19
{
20
    /**
21
     * Guzzle client
22
     * @var Client
23
     */
24
    private $client;
25
26
    /**
27
     * Guzzle cookie
28
     * @var CookieJar
29
     */
30
    private $cookie;
31
32
    /**
33
     * Ubench lib
34
     * @var Ubench
35
     */
36
    private $bench;
37
38
    /**
39
     * @var $html
40
     */
41
    private $html;
42
43
    /**
44
     * @var $wistiaID
45
     */
46
    private $wistiaID;
47
48
    /**
49
     * Receives dependencies
50
     *
51
     * @param string $html
52
     * @param Ubench $bench
53
     */
54
    public function __construct($html, Ubench $bench)
55
    {
56
        $this->client = new \GuzzleHttp\Client(['base_url' => 'http://www.clipconverter.cc']);
57
        $this->cookie = new CookieJar();
58
        $this->bench = $bench;
59
        $this->html = $html;
60
    }
61
    /**
62
     * Get wistia.net video url
63
     */
64
    public function getDownloadUrl()
65
    {
66
        try {
67
            $this->getWistiaID();
68
        } catch(NoWistiaIDException $e) {
69
            Utils::write(sprintf("Can't find any wistia.net ID! :("));
70
            return false;
71
        }
72
        
73
        $response =  $this->client->post('check.php', [
74
            'cookies' => $this->cookie,
75
            'body'    => [
76
                'mediaurl'    => 'http://fast.wistia.net/embed/iframe/'.$this->wistiaID
77
            ],
78
            'verify' => false
79
        ]);
80
81
        $html = $response->getBody()->getContents();
82
83
        $data = json_decode($html,true);
84
85
        if(!isset($data['url']))
86
            return false;
87
88
        $finalUrl = '';
89
        $maxSize = 0;
90
        foreach($data['url'] as $url) {
91
            if($url['size'] > $maxSize) {
92
                $maxSize = $url['size'];
93
                $finalUrl = $url['url'];
94
            }
95
        }
96
        Utils::writeln(sprintf("Found video URL %s ",
97
            $finalUrl
98
        ));
99
100
        return $finalUrl;
101
    }
102
103
    /**
104
     * Get wistia.net video id
105
     */
106
    protected function getWistiaID() {
107
        if(preg_match('~wistia_async_([a-z0-9]+)\s~',$this->html,$match)) {
108
            $this->wistiaID = trim($match[1]);
109
110
            Utils::writeln(sprintf("Found Wistia.net ID %s ",
111
                $this->wistiaID
112
            ));
113
        }
114
        else {
115
            return false;
116
        }
117
    }
118
}
119