Passed
Branch workflow-workflow (8c633e)
by Guillaume
04:02
created

Workflow::menuFor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 2

Importance

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