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

Workflow   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 8.33%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 56
ccs 2
cts 24
cp 0.0833
rs 10
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A downloadThroughCliCommand() 0 5 1
A findMagnetLinkOn() 0 5 1
A notifyDownload() 0 3 1
A copy() 0 7 1
A notifyCopy() 0 3 1
A downloadThroughDefaultApplication() 0 5 1
A action() 0 3 1
A download() 0 9 2
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