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