1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Godbout\Alfred\Time; |
4
|
|
|
|
5
|
|
|
use Godbout\Alfred\Workflow\Config; |
6
|
|
|
use Godbout\Alfred\Time\Services\Toggl; |
7
|
|
|
use Godbout\Alfred\Time\Services\Harvest; |
8
|
|
|
use Godbout\Alfred\Workflow\BaseWorkflow; |
9
|
|
|
use Godbout\Alfred\Workflow\ScriptFilter; |
10
|
|
|
use Godbout\Alfred\Time\Services\Everhour; |
11
|
|
|
|
12
|
|
|
class Workflow extends BaseWorkflow |
13
|
|
|
{ |
14
|
|
|
const SERVICES = [ |
15
|
|
|
'toggl', |
16
|
|
|
'harvest', |
17
|
|
|
'everhour' |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
private $config = null; |
21
|
|
|
|
22
|
|
|
|
23
|
97 |
|
protected function __construct() |
24
|
|
|
{ |
25
|
97 |
|
$this->config = Config::ifEmptyStartWith(self::getDefaultConfig()); |
26
|
|
|
|
27
|
97 |
|
parent::__construct(); |
28
|
97 |
|
} |
29
|
|
|
|
30
|
50 |
|
public static function currentMenu() |
31
|
|
|
{ |
32
|
50 |
|
self::getCurrentMenuClass()::scriptFilter(); |
33
|
|
|
|
34
|
50 |
|
return self::getInstance()->scriptFilter->output(); |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
public static function do() |
38
|
|
|
{ |
39
|
1 |
|
$action = getenv('timer_action'); |
40
|
|
|
|
41
|
1 |
|
if ($timerId = getenv('timer_id')) { |
42
|
|
|
return Timer::$action($timerId); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
if (method_exists(Timer::class, $action)) { |
46
|
1 |
|
return Timer::$action(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return true; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public static function notify($result = false) |
53
|
|
|
{ |
54
|
|
|
$action = getenv('timer_action'); |
55
|
|
|
|
56
|
|
|
$service = ucfirst(self::serviceEnabled()); |
57
|
|
|
|
58
|
|
|
if ($result === false) { |
59
|
|
|
return "Oops... $service cannot $action."; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return "$service $action!"; |
63
|
|
|
} |
64
|
|
|
|
65
|
97 |
|
private static function getDefaultConfig() |
66
|
|
|
{ |
67
|
97 |
|
return include __DIR__ . '/../config/default.php'; |
68
|
|
|
} |
69
|
|
|
|
70
|
97 |
|
public static function getConfig() |
71
|
|
|
{ |
72
|
97 |
|
return self::getInstance()->config; |
73
|
|
|
} |
74
|
|
|
|
75
|
68 |
|
public static function enableService($service = '') |
76
|
|
|
{ |
77
|
68 |
|
return self::getInstance()->serviceStatus($service, true); |
78
|
|
|
} |
79
|
|
|
|
80
|
11 |
|
public static function disableService($service = '') |
81
|
|
|
{ |
82
|
11 |
|
return self::getInstance()->serviceStatus($service, false); |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
public static function services() |
86
|
|
|
{ |
87
|
2 |
|
return self::SERVICES; |
88
|
|
|
} |
89
|
|
|
|
90
|
75 |
|
protected function serviceStatus($service, $status = false) |
91
|
|
|
{ |
92
|
75 |
|
self::getInstance()->disableAllServices(); |
93
|
|
|
|
94
|
75 |
|
if (self::getInstance()->classExistsForService($service)) { |
95
|
75 |
|
Workflow::getConfig()->write("$service.is_active", $status); |
96
|
|
|
|
97
|
75 |
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
75 |
|
protected function classExistsForService($service = '') |
104
|
|
|
{ |
105
|
75 |
|
return class_exists(__NAMESPACE__ . '\\Services\\' . ucfirst($service)); |
106
|
|
|
} |
107
|
|
|
|
108
|
19 |
|
public static function serviceEnabled() |
109
|
|
|
{ |
110
|
19 |
|
if (self::getInstance()->getConfig()->read('toggl.is_active')) { |
111
|
15 |
|
return new Toggl( |
112
|
15 |
|
Workflow::getConfig()->read('toggl.api_token') |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
7 |
|
if (self::getInstance()->getConfig()->read('harvest.is_active')) { |
117
|
2 |
|
return new Harvest( |
118
|
2 |
|
Workflow::getConfig()->read('harvest.account_id'), |
119
|
2 |
|
Workflow::getConfig()->read('harvest.api_token') |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
5 |
|
if (self::getInstance()->getConfig()->read('everhour.is_active')) { |
124
|
1 |
|
return new Everhour( |
125
|
1 |
|
Workflow::getConfig()->read('everhour.api_token') |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
4 |
|
return null; |
130
|
|
|
} |
131
|
|
|
|
132
|
97 |
|
public static function disableAllServices() |
133
|
|
|
{ |
134
|
97 |
|
foreach (self::SERVICES as $service) { |
135
|
97 |
|
Workflow::getConfig()->write("$service.is_active", false); |
136
|
|
|
} |
137
|
97 |
|
} |
138
|
|
|
|
139
|
50 |
|
private static function getCurrentMenuClass() |
140
|
|
|
{ |
141
|
50 |
|
$args = explode('_', getenv('action')); |
142
|
|
|
|
143
|
50 |
|
if (in_array($args[0], self::SERVICES)) { |
144
|
38 |
|
$service = ucfirst($args[0]); |
145
|
38 |
|
$action = substr(getenv('action'), strlen($args[0])); |
146
|
|
|
|
147
|
38 |
|
return __NAMESPACE__ . "\\Menus\\$service\\" . self::getMenuClassName($action); |
148
|
|
|
} |
149
|
|
|
|
150
|
12 |
|
return __NAMESPACE__ . "\\Menus\\" . (self::getMenuClassName(getenv('action')) ?: 'Entrance'); |
151
|
|
|
} |
152
|
|
|
|
153
|
50 |
|
private static function getMenuClassName($action) |
154
|
|
|
{ |
155
|
50 |
|
return str_replace('_', '', ucwords($action === false ? 'entrance' : $action, '_')); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|