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

Workflow   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 21
c 3
b 0
f 0
dl 0
loc 62
ccs 18
cts 27
cp 0.6667
rs 10
wmc 10

8 Methods

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