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