Completed
Push — master ( ac8d52...da5576 )
by Guillaume
02:10
created

src/commands/menu.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
require_once __DIR__ . '/../../vendor/autoload.php';
4
5
use AlfredTime\Timer;
6
use AlfredTime\Config;
7
use Alfred\Workflows\Workflow;
8
9
$workflow = new Workflow();
10
$config = new Config(getenv('alfred_workflow_data') . '/config.json');
11
$timer = new Timer($config);
12
13
$query = trim($argv[1]);
14
15
/**
16
 * Check for config file
17
 * If cannot find, Workflow is not usable
18
 */
19
if ($config->isConfigured() === false) {
20
    $workflow->result()
21
        ->uid('')
22
        ->arg('config')
23
        ->title('No config file found')
24
        ->subtitle('Generate and edit the config file')
25
        ->type('default')
26
        ->valid(true);
27
} else {
28
    if ($query === 'config') {
29
        $workflow->result()
30
            ->uid('')
31
            ->arg('edit')
32
            ->title('Edit config file')
33
            ->subtitle('Open the config file in your favorite editor!')
34
            ->type('default')
35
            ->valid(true);
36 View Code Duplication
    } elseif ($query === 'sync') {
37
        $workflow->result()
38
            ->uid('')
39
            ->arg('sync')
40
            ->title('Sync projects and tags from online to local cache')
41
            ->subtitle('Update local projects and tags data')
42
            ->type('default')
43
            ->valid(true);
44
    } elseif ($query === 'undo') {
45
        $runningServices = $config->runningServices();
46
47
        if (empty($runningServices) === true) {
48
            $workflow->result()
49
                ->uid('')
50
                ->arg('')
51
                ->title('Undo ""')
52
                ->subtitle('Nothing to undo!')
53
                ->type('default')
54
                ->valid(false);
55
        } else {
56
            $subtitle = $timer->isRunning() === true ? 'Stop and delete current timer for ' : 'Delete timer for ';
57
            $subtitle .= implode(' and ', array_map('ucfirst', $runningServices));
58
59
            $workflow->result()
60
                ->uid('')
61
                ->arg('undo')
62
                ->title('Undo "' . $timer->getDescription() . '"')
63
                ->subtitle($subtitle)
64
                ->type('default')
65
                ->valid(true);
66
        }
67 View Code Duplication
    } elseif ($query === 'delete') {
68
        $workflow->result()
69
            ->uid('')
70
            ->arg('delete')
71
            ->title('Delete a timer')
72
            ->subtitle('Press enter to load recent timers list')
73
            ->type('default')
74
            ->valid(true);
75
    } elseif ($query === 'continue') {
76
        $workflow->result()
77
            ->uid('')
78
            ->arg('continue')
79
            ->title('Continue a timer')
80
            ->subtitle('Press enter to load the list of recent timers')
81
            ->type('default')
82
            ->valid(true);
83
    } elseif ($timer->isRunning() === false) {
84
        $service = $timer->getPrimaryService();
85
86
        if (empty($service) === true) {
87
            $subtitle = 'No timer services activated. Edit config file to active services';
88
        } else {
89
            $subtitle = 'Start new timer for ' . ucfirst($service);
90
        }
91
92
        $workflow->result()
93
            ->uid('')
94
            ->arg('start ' . $query)
95
            ->title('Start "' . $query . '"')
96
            ->subtitle($subtitle)
97
            ->type('default')
98
            ->mod(
99
                'cmd',
100
                'Start new timer for ' . implode(
101
                    ' and ',
102
                    array_map('ucfirst', $config->activatedServices('start'))
0 ignored issues
show
The call to Config::activatedServices() has too many arguments starting with 'start'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
103
                ),
104
                'start_all ' . $query)
105
            ->valid(true);
106
    } else {
107
        $services = $config->activatedServices();
108
109
        if (empty($services) === true) {
110
            $subtitle = 'No timer services activated. Edit config file to active services';
111
        } else {
112
            $subtitle = 'Stop current timer for ' . implode(' and ', array_map('ucfirst', $services));
113
        }
114
115
        $workflow->result()
116
            ->uid('')
117
            ->arg('stop')
118
            ->title('Stop "' . $timer->getDescription() . '"')
119
            ->subtitle($subtitle)
120
            ->type('default')
121
            ->valid(true);
122
    }
123
}
124
125
echo $workflow->output();
126