Passed
Push — master ( 7706d5...e57032 )
by Guillaume
18:36 queued 16:57
created

Entrance::buildURLFrom()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 24
    public static function scriptFilter()
17
    {
18 24
        ScriptFilter::add(
19 24
            self::resultsFor(self::userInput())
20
        );
21 24
    }
22
23 24
    protected static function resultsFor($userInput = '')
24
    {
25 24
        $torrents = self::searchOnlineFor($userInput);
26
27 24
        return self::menuFor($torrents);
28
    }
29
30 24
    protected static function searchOnlineFor($userInput)
31
    {
32 24
        $crawler = (new Client())->request('GET', self::buildURLFrom($userInput));
33
34 24
        return $crawler->filter('.frontPageWidget tr');
35
    }
36
37 24
    protected static function buildURLFrom($userInput)
38
    {
39 24
        if (($tag = strstr($userInput, '#')) !== false) {
40 6
            $term = strstr($userInput, '#', true);
41
42 6
            return getenv('url') . '/search/' . urlencode($term) . '/category/' . ltrim($tag, '#') . '/';
43
        }
44
45 18
        return getenv('url') . '/usearch/' . urlencode($userInput);
46
    }
47
48 24
    protected static function menuFor(Crawler $torrents)
49
    {
50 24
        if ($torrents->count()) {
51 18
            $torrents->nextAll()->each(function ($row) {
52 18
                ScriptFilter::add(
53 18
                    Item::create()
54 18
                        ->title(TorrentMenuItemBuilder::title($row))
55 18
                        ->subtitle(TorrentMenuItemBuilder::subtitle($row))
56 18
                        ->icon(Icon::create("resources/icons/magnet.png"))
57 18
                        ->arg('do')
58 18
                        ->variable('action', 'download')
59 18
                        ->variable('torrent_page_link', TorrentMenuItemBuilder::pageLink($row))
60 18
                        ->variable('torrent_name', TorrentMenuItemBuilder::subtitle($row))
61 18
                        ->mod(
62 18
                            Cmd::create()
63 18
                                ->arg('do')
64 18
                                ->subtitle('Copy magnet link')
65 18
                                ->variable('action', 'copy')
66 18
                                ->variable('torrent_page_link', TorrentMenuItemBuilder::pageLink($row))
67 18
                                ->variable('torrent_name', TorrentMenuItemBuilder::subtitle($row))
68
                        )
69
                );
70 18
            });
71
        } else {
72 6
            ScriptFilter::add(
73 6
                Item::create()
74 6
                    ->title('404 for ' . self::userInput() . ' ☹️')
75 6
                    ->subtitle('Try some other terms maybe?')
76
            );
77
        }
78
79 24
        return ScriptFilter::output();
80
    }
81
}
82