Passed
Branch master (51117f)
by Guillaume
02:50
created

Entrance::timerAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Godbout\Alfred\Time\Menus;
4
5
use Godbout\Alfred\Time\Workflow;
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
11
class Entrance extends Menu
12
{
13 7
    public static function scriptFilter()
14
    {
15 7
        ScriptFilter::add(
16 7
            self::timerAction(),
17 7
            self::setupWorkflow()
18
        );
19 7
    }
20
21 7
    private static function timerAction()
22
    {
23 7
        $serviceEnabled = Workflow::serviceEnabled();
24
25 7
        if (! $serviceEnabled) {
26 3
            return;
27
        }
28
29 4
        if ($serviceEnabled->runningTimer()) {
30 1
            return self::stopCurrentTimer();
31
        }
32
33 3
        return self::startTimer();
34
    }
35
36 1
    private static function stopCurrentTimer()
37
    {
38 1
        return Item::create()
39 1
            ->uid('stop_timer')
40 1
            ->title('Stop current timer')
41 1
            ->subtitle('That timer is currently running!')
42 1
            ->arg('do')
43 1
            ->variable('timer_action', 'stop');
44
    }
45
46 3
    private static function startTimer()
47
    {
48 3
        if (! empty(Workflow::serviceEnabled())) {
49 3
            return Item::create()
50 3
                ->uid('start_timer')
51 3
                ->title('Start "' . self::userInput() . '"')
52 3
                ->mod(
53 3
                    Cmd::create()
54 3
                        ->subtitle('Continue a timer')
55 3
                        ->arg('choose_timer')
56
                )
57 3
                ->arg('choose_project')
58 3
                ->variable('timer_description', self::userInput());
59
        }
60
    }
61
62 7
    private static function setupWorkflow()
63
    {
64 7
        if (empty(Workflow::serviceEnabled()) || (empty(self::userInput()))) {
65 6
            return Item::create()
66 6
                ->uid('setup_timers')
67 6
                ->title('Setup the workflow')
68 6
                ->arg('setup')
69 6
                ->icon(Icon::create('resources/icons/icon.png'));
70
        }
71 1
    }
72
}
73