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

Entrance::scriptFilter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Godbout\Alfred\Kat\Menus;
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
use Godbout\Alfred\Workflow\Menus\BaseMenu;
12
use Godbout\Alfred\Kat\TorrentMenuItemBuilder;
13
14
class Entrance extends BaseMenu
15
{
16
    public static function scriptFilter()
17
    {
18
        ScriptFilter::add(
19
            self::resultsFor(self::userInput())
20
        );
21
    }
22
23
    protected static function resultsFor($term = '')
24
    {
25
        $torrents = self::searchOnlineFor($term);
26
27
        return self::menuFor($torrents);
28
    }
29
30
    protected static function searchOnlineFor($term)
31
    {
32
        $crawler = (new Client())->request('GET', getenv('url') . '/usearch/' . urlencode($term));
33
34
        return $crawler->filter('.frontPageWidget tr');
35
    }
36
37
    protected static function menuFor(Crawler $torrents)
38
    {
39
        if ($torrents->count()) {
40
            $torrents->nextAll()->each(function ($row) {
41
                ScriptFilter::add(
42
                    Item::create()
43
                        ->title(TorrentMenuItemBuilder::title($row))
44
                        ->subtitle(TorrentMenuItemBuilder::subtitle($row))
45
                        ->icon(Icon::create("resources/icons/magnet.png"))
46
                        ->arg('download')
47
                        ->variable('torrent_page_link', TorrentMenuItemBuilder::pageLink($row))
48
                        ->variable('torrent_name', TorrentMenuItemBuilder::subtitle($row))
49
                        ->mod(
50
                            Cmd::create()
51
                                ->arg('copy')
52
                                ->subtitle('Copy magnet link')
53
                                ->variable('torrent_page_link', TorrentMenuItemBuilder::pageLink($row))
54
                                ->variable('torrent_name', TorrentMenuItemBuilder::subtitle($row))
55
                        )
56
                );
57
            });
58
        } else {
59
            ScriptFilter::add(
60
                Item::create()
61
                    ->title('404 for ' . self::userInput() . ' ☹️')
62
                    ->subtitle('Try some other terms maybe?')
63
            );
64
        }
65
66
        return ScriptFilter::output();
67
    }
68
}
69