Completed
Push — master ( 518f9a...5be86c )
by Guillaume
02:19
created

item_list.php ➔ getItemName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
use AlfredTime\WorkflowHandler;
9
10
$workflow = new Workflow();
11
$config = new Config(getenv('alfred_workflow_data') . '/config.json');
12
$timer = new Timer($config);
13
$workflowHandler = new WorkflowHandler($config);
14
15
$query = getenv('description');
16
17
$items = call_user_func([$workflowHandler, 'get' . ucfirst($argv[1])]);
18
19
if (substr($query, 0, 6) === 'start ') {
20
    $workflow->result()
21
        ->arg(json_encode([]))
22
        ->title('No ' . getItemName($argv[1]))
23
        ->subtitle('Timer will be created without a ' . getItemName($argv[1]))
24
        ->type('default')
25
        ->valid(true);
26
27
    $items = array_filter($items, function ($value) use ($timer) {
28
        return isset($value[$timer->getPrimaryService() . '_id']);
29
    });
30
} elseif (substr($query, 0, 10) === 'start_all ') {
31
    $activatedServices = $config->activatedServices();
32
33
    foreach ($items as $name => $services) {
34
        if (count($activatedServices) !== count($services)) {
35
            unset($items[$name]);
36
        }
37
    }
38
}
39
40
foreach ($items as $name => $ids) {
41
    $subtitle = ucfirst(getItemName($argv[1])) . ' available for ' . implode(' and ', array_map(function ($value) {
42
        return substr(ucfirst($value), 0, -3);
43
    }, array_keys($ids)));
44
45
    $item = $workflow->result()
46
        ->arg(json_encode($ids))
47
        ->title($name)
48
        ->subtitle($subtitle)
49
        ->type('default')
50
        ->valid(true);
51
52
    if (count($ids) === 1) {
53
        $item->icon('icons/' . substr(key($ids), 0, -3) . '.png');
54
    }
55
}
56
57
echo $workflow->output();
58
59
/**
60
 * @param $name
61
 */
62
function getItemName($name)
63
{
64
    return substr($name, 0, -1);
65
}
66