Passed
Push — master ( 7f5f13...76379c )
by Guillaume
03:30
created

Workflow   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 91.11%

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 87
ccs 41
cts 45
cp 0.9111
rs 10
c 0
b 0
f 0
wmc 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A menuFor() 0 30 2
A notifyDownload() 0 3 1
A userInput() 0 5 1
A copy() 0 7 1
A notifyCopy() 0 4 1
A searchOnlineFor() 0 5 1
A action() 0 3 1
A download() 0 7 1
A resultsFor() 0 5 1
1
<?php
2
3
namespace Godbout\Alfred\Kat;
4
5
use Goutte\Client;
6
use Godbout\Alfred\Workflow\Icon;
7
use Godbout\Alfred\Workflow\Item;
8
use Godbout\Alfred\Workflow\Mods\Cmd;
9
use Godbout\Alfred\Workflow\ScriptFilter;
10
use Symfony\Component\DomCrawler\Crawler;
11
12
class Workflow
13
{
14
    public static function action()
15
    {
16
        return getenv('action');
17
    }
18
19 1
    public static function userInput()
20
    {
21 1
        global $argv;
22
23 1
        return trim($argv[1] ?? '');
24
    }
25
26 3
    public static function resultsFor($term = '')
27
    {
28 3
        $torrents = self::searchOnlineFor($term);
29
30 3
        return self::menuFor($torrents);
31
    }
32
33 1
    public static function download($magnetLink = '')
34
    {
35 1
        $crawler = (new Client())->request('GET', getenv('url') . $magnetLink);
36
37 1
        $magnetLink = $crawler->filter('#tab-technical a.siteButton.giantButton')->attr('href');
38
39 1
        return exec("open $magnetLink 2>&1", $result);
40
    }
41
42 1
    public static function copy($magnetLink = '')
43
    {
44 1
        $crawler = (new Client())->request('GET', getenv('url') . $magnetLink);
45
46 1
        $magnetLink = $crawler->filter('#tab-technical a.siteButton.giantButton')->attr('href');
47
48 1
        return exec("echo '$magnetLink' | pbcopy", $result);
49
    }
50
51 1
    public static function notifyDownload($torrentName = '')
52
    {
53 1
        return '"' . $torrentName . '" will soon be at home!';
54
    }
55
56
    public static function notifyCopy($torrentName = '')
57
    {
58
59
        return 'Magnet link for "' . substr($torrentName, 0, 30) . '..." has been copied to clipboard!';
60
    }
61
62 3
    protected static function searchOnlineFor($term)
63
    {
64 3
        $crawler = (new Client())->request('GET', getenv('url') . '/usearch/' . urlencode($term));
65
66 3
        return $crawler->filter('.frontPageWidget tr');
67
    }
68
69 3
    protected static function menuFor(Crawler $torrents)
70
    {
71 3
        if ($torrents->count()) {
72
            $torrents->nextAll()->each(function ($row) {
73 2
                ScriptFilter::add(
74 2
                    Item::create()
75 2
                        ->title(TorrentMenuItemBuilder::title($row))
76 2
                        ->subtitle(TorrentMenuItemBuilder::subtitle($row))
77 2
                        ->icon(Icon::create("resources/icons/magnet.png"))
78 2
                        ->arg('download')
79 2
                        ->variable('torrent_page_link', TorrentMenuItemBuilder::pageLink($row))
80 2
                        ->variable('torrent_name', TorrentMenuItemBuilder::subtitle($row))
81 2
                        ->mod(
82 2
                            Cmd::create()
83 2
                                ->arg('copy')
84 2
                                ->subtitle('Copy magnet link')
85 2
                                ->variable('torrent_page_link', TorrentMenuItemBuilder::pageLink($row))
86 2
                                ->variable('torrent_name', TorrentMenuItemBuilder::subtitle($row))
87
                        )
88
                );
89 2
            });
90
        } else {
91 1
            ScriptFilter::add(
92 1
                Item::create()
93 1
                    ->title('404 for ' . self::userInput() . ' ☹️')
94 1
                    ->subtitle('Try some other terms maybe?')
95
            );
96
        }
97
98 3
        return ScriptFilter::output();
99
    }
100
}
101