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