Completed
Push — master ( 4f1c30...468f94 )
by Guillaume
02:35
created

src/commands/menus.php (4 issues)

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
/**
4
 * If not config file found, call script with actions
5
 */
6 View Code Duplication
if ($config->isConfigured() === false) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
7
    $data = ['action' => 'config'];
8
9
    $workflow->result()
10
        ->uid('')
11
        ->arg(json_encode($data))
12
        ->title('No config file found')
13
        ->subtitle('Generate and edit the config file')
14
        ->type('default')
15
        ->valid(true);
16
17
    echo $workflow->output();
18
    exit();
19
}
20
21
$query = ltrim($argv[2]);
22
23
/**
24
 * If the user wants to edit the config file
25
 */
26 View Code Duplication
if ($query === 'config') {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
    $data = ['action' => 'edit'];
28
29
    $workflow->result()
30
        ->uid('')
31
        ->arg(json_encode($data))
32
        ->title('Edit config file')
33
        ->subtitle('Open the config file in your favorite editor!')
34
        ->type('default')
35
        ->valid(true);
36
37
    echo $workflow->output();
38
    exit();
39
}
40
41
if ($query === 'sync') {
42
    $data = ['action' => 'sync'];
43
44
    $workflow->result()
45
        ->uid('')
46
        ->arg(json_encode($data))
47
        ->title('Sync projects and tags from online to local cache')
48
        ->subtitle('Update local projects and tags data')
49
        ->type('default')
50
        ->valid(true);
51
52
    echo $workflow->output();
53
    exit();
54
}
55
56
if ($query === 'delete') {
57
    $data = ['action' => 'delete'];
58
59
    $workflow->result()
60
        ->uid('')
61
        ->arg(json_encode($data))
62
        ->title('Delete a timer')
63
        ->subtitle('Press enter to load recent timers list')
64
        ->type('default')
65
        ->valid(true);
66
67
    echo $workflow->output();
68
    exit();
69
}
70
71
if ($query === 'undo') {
72
    $data['action'] = 'undo';
73
74
    $runningServices = $config->runningServices();
75
76
    if (empty($runningServices) === true) {
77
        $workflow->result()
78
            ->uid('')
79
            ->arg('')
80
            ->title('Undo ""')
81
            ->subtitle('Nothing to undo!')
82
            ->type('default')
83
            ->valid(false);
84
    } else {
85
        $subtitle = $timer->isRunning() === true ? 'Stop and delete current timer for ' : 'Delete timer for ';
86
        $subtitle .= implode(' and ', array_map('ucfirst', $runningServices));
87
88
        $workflow->result()
89
            ->uid('')
90
            ->arg(json_encode($data))
91
            ->title('Undo "' . $timer->getDescription() . '"')
92
            ->subtitle($subtitle)
93
            ->type('default')
94
            ->valid(true);
95
    }
96
97
    echo $workflow->output();
98
    exit();
99
}
100
101
if (empty($data) === true) {
102
    $services = $config->activatedServices();
103
104
    if ($timer->isRunning() === true) {
105
        $data = [
106
            'action' => 'stop',
107
            'query'  => $query,
108
        ];
109
110
        if (empty($services) === true) {
111
            $subtitle = 'No timer services activated. Edit config file to active services';
112
        } else {
113
            $subtitle = 'Stop current timer for ' . implode(' and ', array_map('ucfirst', $services));
114
        }
115
116
        $workflow->result()
117
            ->uid('')
118
            ->arg(json_encode($data))
119
            ->title('Stop "' . $timer->getDescription() . '"')
120
            ->subtitle($subtitle)
121
            ->type('default')
122
            ->valid(true);
123
    } else {
124
        $data = [
125
            'action' => 'start',
126
            'query'  => $query,
127
        ];
128
129
        $continueData = $data;
130
        $continueData['action'] = 'continue';
131
132
        $startAllData = $data;
133
        $startAllData['action'] = 'start_all';
134
135
        $service = $timer->getPrimaryService();
136
137
        if (empty($service) === true) {
138
            $subtitle = 'No timer services activated. Edit config file to active services';
139
        } else {
140
            $subtitle = 'Start new timer for ' . ucfirst($service);
141
        }
142
143
        $workflow->result()
144
            ->uid('')
145
            ->arg(json_encode($data))
146
            ->title('Start "' . $query . '"')
147
            ->subtitle($subtitle)
148
            ->cmd('Continue a timer', json_encode($continueData))
149
            ->shift('Start new timer for ' . implode(
150
                ' and ',
151
                array_map('ucfirst', $config->activatedServices())),
152
                json_encode($startAllData)
153
            )
154
            ->type('default')
155
            ->valid(true);
156
    }
157
158
    echo $workflow->output();
159
    exit();
160
}
161
162
if ($data['show'] === 'project_list') {
163
    $data['action'] = 'choose_project';
164
    $data['project_ids'] = null;
165
    $workflow->result()
166
        ->arg(json_encode($data))
167
        ->title('No project')
168
        ->subtitle('Timer will be created without a project')
169
        ->type('default')
170
        ->valid(true);
171
172
    $items = $workflowHandler->getProjects();
173
174 View Code Duplication
    foreach ($items as $name => $ids) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
        $subtitle = 'Project available for ' . implode(' and ', array_map(function ($value) {
176
            return ucfirst($value);
177
        }, array_keys($ids)));
178
179
        $data['project_ids'] = $ids;
180
181
        $item = $workflow->result()
182
            ->arg(json_encode($data))
183
            ->title($name)
184
            ->subtitle($subtitle)
185
            ->type('default')
186
            ->valid(true);
187
188
        if (count($ids) === 1) {
189
            $item->icon('icons/' . key($ids) . '.png');
190
        }
191
    }
192
} elseif ($data['show'] === 'tag_list') {
193
    $data['action'] = 'choose_tag';
194
    $data['tag_ids'] = null;
195
    $workflow->result()
196
        ->arg(json_encode($data))
197
        ->title('No tag')
198
        ->subtitle('Timer will be created without a tag')
199
        ->type('default')
200
        ->valid(true);
201
202
    $items = $workflowHandler->getTags();
203
204 View Code Duplication
    foreach ($items as $name => $ids) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
205
        $subtitle = 'Tag available for ' . implode(' and ', array_map(function ($value) {
206
            return ucfirst($value);
207
        }, array_keys($ids)));
208
209
        $data['tag_ids'] = $ids;
210
        $item = $workflow->result()
211
            ->arg(json_encode($data))
212
            ->title($name)
213
            ->subtitle($subtitle)
214
            ->type('default')
215
            ->valid(true);
216
217
        if (count($ids) === 1) {
218
            $item->icon('icons/' . key($ids) . '.png');
219
        }
220
    }
221
} elseif ($data['show'] === 'timer_list') {
222
    $data['action'] = 'final';
223
224
    if ($data['original_action'] === 'delete') {
225
        $workflow->result()
226
            ->title('Choose a timer to delete below')
227
            ->subtitle('_____________________ BE CAREFUL, NO RECOVERY POSSIBLE _____________________')
228
            ->type('default')
229
            ->valid(false);
230
    }
231
232
    $timers = $workflowHandler->getRecentTimers();
233
234
    foreach ($timers as $service => $recentTimers) {
235
        foreach ($recentTimers as $recentTimer) {
236
            $projectName = is_int($recentTimer['project_name'])
237
                ? $workflowHandler->getProjectName($service, $recentTimer['project_name'])
238
                : $recentTimer['project_name'];
239
            $tags = $recentTimer['tags'];
240
            $duration = $recentTimer['duration'];
241
242
            $data['timer_info'] = [
243
                'service'     => $service,
244
                'description' => $recentTimer['description'],
245
                'id'          => $recentTimer['id'],
246
                'project_id'  => $recentTimer['project_id'],
247
                'tags'        => $recentTimer['tags'],
248
            ];
249
250
            $subtitle = (empty($projectName) === true ? 'No project' : $projectName) . ', '
251
                . (empty($tags) === true ? 'No tag' : '[' . $tags . ']') . ', '
252
                . ($duration > 0 ? gmdate('H:i:s', $duration) : '--:--:--');
253
254
            $workflow->result()
255
                ->arg(json_encode($data))
256
                ->title(empty($recentTimer['description']) ? '' : $recentTimer['description'])
257
                ->subtitle($subtitle)
258
                ->type('default')
259
                ->icon('icons/' . $service . '.png')
260
                ->valid(true);
261
        }
262
    }
263
}
264
265
$workflow->filterResults($query);
266
echo $workflow->output();
267