Passed
Branch workflow-workflow (8e3f4b)
by Guillaume
08:01
created

Workflow::findMagnetLinkOn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Godbout\Alfred\Kat;
4
5
use Goutte\Client;
6
use Godbout\Alfred\Workflow\BaseWorkflow;
7
8
class Workflow extends BaseWorkflow
9
{
10
    public static function do()
11
    {
12
        $action = getenv('workflow_action');
13
14
        if (method_exists(static::class, $action)) {
15
            return static::$action(getenv('torrent_page_link'));
16
        }
17
18
        return false;
19
    }
20
21 2
    public static function download($torrentPageLink = '')
22
    {
23 2
        $magnetLink = self::findMagnetLinkOn($torrentPageLink);
24
25 2
        if (getenv('cli') !== false) {
26 1
            return self::downloadThroughCliCommand($magnetLink);
27
        }
28
29 1
        return self::downloadThroughDefaultApplication($magnetLink);
30
    }
31
32 1
    protected static function downloadThroughCliCommand($magnetLink = '')
33
    {
34 1
        system(str_replace('{magnet}', $magnetLink, getenv('cli')), $result);
35
36 1
        return $result === 0;
37
    }
38
39 1
    protected static function downloadThroughDefaultApplication($magnetLink = '')
40
    {
41 1
        system("open $magnetLink 2>&1", $result);
42
43 1
        return $result === 0;
44
    }
45
46
    public static function copy($torrentPageLink = '')
47
    {
48
        $magnetLink = self::findMagnetLinkOn($torrentPageLink);
49
50
        system("echo '$magnetLink' | pbcopy 2>&1", $result);
51
52
        return $result === 0;
53
    }
54
55 2
    protected static function findMagnetLinkOn($torrentPageLink = '')
56
    {
57 2
        $crawler = (new Client())->request('GET', getenv('url') . $torrentPageLink);
58
59 2
        return $crawler->filter('#tab-technical a.siteButton.giantButton')->attr('href');
60
    }
61
62 1
    public static function notifyDownload($torrentName = '')
63
    {
64 1
        return '"' . $torrentName . '" will soon be at home!';
65
    }
66
67 1
    public static function notifyCopy($torrentName = '')
68
    {
69 1
        return 'Magnet link for "' . substr($torrentName, 0, 30) . '..." has been copied to clipboard!';
70
    }
71
}
72