Completed
Push — master ( d20c63...0b7335 )
by Guillaume
02:17
created

src/commands/menus.php (6 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 View Code Duplication
if ($query === 'sync') {
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...
42
    $data = [
43
        'continue' => false,
44
        'action'   => 'sync',
45
    ];
46
47
    $workflow->result()
48
        ->uid('')
49
        ->arg(json_encode($data))
50
        ->title('Sync projects and tags from online to local cache')
51
        ->subtitle('Update local projects and tags data')
52
        ->type('default')
53
        ->valid(true);
54
55
    echo $workflow->output();
56
    exit();
57
}
58
59 View Code Duplication
if ($query === 'delete') {
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...
60
    $data = [
61
        'continue' => true,
62
        'action'   => 'delete',
63
    ];
64
65
    $workflow->result()
66
        ->uid('')
67
        ->arg(json_encode($data))
68
        ->title('Delete a timer')
69
        ->subtitle('Press enter to load recent timers list')
70
        ->type('default')
71
        ->valid(true);
72
73
    echo $workflow->output();
74
    exit();
75
}
76
77
if ($query === 'undo') {
78
    $data['continue'] = false;
79
    $data['action'] = 'undo';
80
81
    $runningServices = $config->runningServices();
82
83
    if (empty($runningServices) === true) {
84
        $workflow->result()
85
            ->uid('')
86
            ->arg('')
87
            ->title('Undo ""')
88
            ->subtitle('Nothing to undo!')
89
            ->type('default')
90
            ->valid(false);
91
    } else {
92
        $subtitle = $timer->isRunning() === true ? 'Stop and delete current timer for ' : 'Delete timer for ';
93
        $subtitle .= implode(' and ', array_map('ucfirst', $runningServices));
94
95
        $workflow->result()
96
            ->uid('')
97
            ->arg(json_encode($data))
98
            ->title('Undo "' . $timer->getDescription() . '"')
99
            ->subtitle($subtitle)
100
            ->type('default')
101
            ->valid(true);
102
    }
103
104
    echo $workflow->output();
105
    exit();
106
}
107
108
if (empty($data) === true) {
109
    $services = $config->activatedServices();
110
111
    if ($timer->isRunning() === true) {
112
        $data = [
113
            'continue' => false,
114
            'action'   => 'stop',
115
            'query'    => $query,
116
        ];
117
118
        if (empty($services) === true) {
119
            $subtitle = 'No timer services activated. Edit config file to active services';
120
        } else {
121
            $subtitle = 'Stop current timer for ' . implode(' and ', array_map('ucfirst', $services));
122
        }
123
124
        $workflow->result()
125
            ->uid('')
126
            ->arg(json_encode($data))
127
            ->title('Stop "' . $timer->getDescription() . '"')
128
            ->subtitle($subtitle)
129
            ->type('default')
130
            ->valid(true);
131
    } else {
132
        $data = [
133
            'continue' => true,
134
            'action'   => 'start',
135
            'query'    => $query,
136
        ];
137
138
        $continueData = $data;
139
        $continueData['action'] = 'continue';
140
141
        $startAllData = $data;
142
        $startAllData['action'] = 'start_all';
143
144
        $service = $timer->getPrimaryService();
145
146
        if (empty($service) === true) {
147
            $subtitle = 'No timer services activated. Edit config file to active services';
148
        } else {
149
            $subtitle = 'Start new timer for ' . ucfirst($service);
150
        }
151
152
        $workflow->result()
153
            ->uid('')
154
            ->arg(json_encode($data))
155
            ->title('Start "' . $query . '"')
156
            ->subtitle($subtitle)
157
            ->cmd('Continue a timer', json_encode($continueData))
158
            ->shift('Start new timer for ' . implode(
159
                ' and ',
160
                array_map('ucfirst', $config->activatedServices())),
161
                json_encode($startAllData)
162
            )
163
            ->type('default')
164
            ->valid(true);
165
    }
166
167
    echo $workflow->output();
168
    exit();
169
}
170
171
if ($data['action'] === 'show_projects') {
172
    $data['action'] = 'show_tags';
173
    $data['project_ids'] = null;
174
    $workflow->result()
175
        ->arg(json_encode($data))
176
        ->title('No project')
177
        ->subtitle('Timer will be created without a project')
178
        ->type('default')
179
        ->valid(true);
180
181
    $items = $workflowHandler->getProjects();
182
183 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...
184
        $subtitle = ucfirst($type) . ' available for ' . implode(' and ', array_map(function ($value) {
185
            return ucfirst($value);
186
        }, array_keys($ids)));
187
188
        $data['project_ids'] = $ids;
189
190
        $item = $workflow->result()
191
            ->arg(json_encode($data))
192
            ->title($name)
193
            ->subtitle($subtitle)
194
            ->type('default')
195
            ->valid(true);
196
197
        if (count($ids) === 1) {
198
            $item->icon('icons/' . key($ids) . '.png');
199
        }
200
    }
201
} elseif ($data['action'] === 'show_tags') {
202
    $data['continue'] = false;
203
    $data['action'] = 'final';
204
    $data['tag_ids'] = null;
205
    $workflow->result()
206
        ->arg(json_encode($data))
207
        ->title('No tag')
208
        ->subtitle('Timer will be created without a tag')
209
        ->type('default')
210
        ->valid(true);
211
212
    $items = $workflowHandler->getTags();
213
214 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...
215
        $subtitle = ucfirst($type) . ' available for ' . implode(' and ', array_map(function ($value) {
216
            return ucfirst($value);
217
        }, array_keys($ids)));
218
219
        $data['tag_ids'] = $ids;
220
        $item = $workflow->result()
221
            ->arg(json_encode($data))
222
            ->title($name)
223
            ->subtitle($subtitle)
224
            ->type('default')
225
            ->valid(true);
226
227
        if (count($ids) === 1) {
228
            $item->icon('icons/' . key($ids) . '.png');
229
        }
230
    }
231
} elseif ($data['action'] === 'choose_timer') {
232
    $data['continue'] = false;
233
    $data['action'] = 'final';
234
235
    if ($data['original_action'] === 'delete') {
236
        $workflow->result()
237
            ->title('Choose a timer to delete below')
238
            ->subtitle('_____________________ BE CAREFUL, NO RECOVERY POSSIBLE _____________________')
239
            ->type('default')
240
            ->valid(false);
241
    }
242
243
    $timers = $workflowHandler->getRecentTimers();
244
245
    foreach ($timers as $service => $recentTimers) {
246
        foreach ($recentTimers as $recentTimer) {
247
            $projectName = is_int($recentTimer['project_name'])
248
                ? $workflowHandler->getProjectName($service, $recentTimer['project_name'])
249
                : $recentTimer['project_name'];
250
            $tags = $recentTimer['tags'];
251
            $duration = $recentTimer['duration'];
252
253
            $data['timer_info'] = [
254
                'service'     => $service,
255
                'description' => $recentTimer['description'],
256
                'id'          => $recentTimer['id'],
257
                'project_id'  => $recentTimer['project_id'],
258
                'tags'        => $recentTimer['tags'],
259
            ];
260
261
            $subtitle = (empty($projectName) === true ? 'No project' : $projectName) . ', '
262
                . (empty($tags) === true ? 'No tag' : '[' . $tags . ']') . ', '
263
                . ($duration > 0 ? gmdate('H:i:s', $duration) : '--:--:--');
264
265
            $workflow->result()
266
                ->arg(json_encode($data))
267
                ->title(empty($recentTimer['description']) ? '' : $recentTimer['description'])
268
                ->subtitle($subtitle)
269
                ->type('default')
270
                ->icon('icons/' . $service . '.png')
271
                ->valid(true);
272
        }
273
    }
274
}
275
276
$workflow->filterResults($query);
277
echo $workflow->output();
278