|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AlfredTime; |
|
4
|
|
|
|
|
5
|
|
|
use AlfredTime\Config; |
|
6
|
|
|
|
|
7
|
|
|
class WorkflowHandler |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var mixed |
|
11
|
|
|
*/ |
|
12
|
|
|
private $config; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var mixed |
|
16
|
|
|
*/ |
|
17
|
|
|
private $harvest; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var mixed |
|
21
|
|
|
*/ |
|
22
|
|
|
private $toggl; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param Config $config |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(Config $config = null) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->config = $config; |
|
30
|
|
|
$this->harvest = new Harvest($this->config->get('harvest', 'domain'), $this->config->get('harvest', 'api_token')); |
|
31
|
|
|
$this->toggl = new Toggl($this->config->get('toggl', 'api_token')); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param $projectId |
|
36
|
|
|
* @return mixed |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getProjectName($projectId) |
|
39
|
|
|
{ |
|
40
|
|
|
$projectName = ''; |
|
41
|
|
|
|
|
42
|
|
|
$projects = $this->getProjects(); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
foreach ($projects as $project) { |
|
45
|
|
|
if ($project['id'] === $projectId) { |
|
46
|
|
|
$projectName = $project['name']; |
|
47
|
|
|
break; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $projectName; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return mixed |
|
56
|
|
|
*/ |
|
57
|
|
View Code Duplication |
public function getRecentTimers() |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
$timers = []; |
|
60
|
|
|
|
|
61
|
|
|
foreach ($this->config->implementedServicesForFeature('get_timers') as $service) { |
|
62
|
|
|
if ($this->config->isServiceActive($service) === true) { |
|
63
|
|
|
$timers = array_merge($timers, $this->getRecentServiceTimers($service)); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $timers; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param $service |
|
72
|
|
|
* @param null $action |
|
73
|
|
|
* @param null $success |
|
74
|
|
|
*/ |
|
75
|
|
View Code Duplication |
public function setNotificationForService($service = null, $action = null, $success = null) |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
if (empty($success) === true) { |
|
78
|
|
|
return '- ' . ucfirst($service) . ': cannot ' . $action . ' [' . $this->$service->getLastMessage() . ']' . "\r\n"; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return '- ' . ucfirst($service) . ': ' . $action . "\r\n"; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return mixed |
|
86
|
|
|
*/ |
|
87
|
|
View Code Duplication |
public function syncOnlineDataToLocalCache() |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
$message = ''; |
|
90
|
|
|
|
|
91
|
|
|
foreach ($this->config->implementedServicesForFeature('sync_data') as $service) { |
|
92
|
|
|
if ($this->config->isServiceActive($service) === true) { |
|
93
|
|
|
$message .= $this->syncServiceOnlineDataToLocalCache($service); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $message; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return mixed |
|
102
|
|
|
*/ |
|
103
|
|
|
private function getRecentServiceTimers($service) |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->$service->getRecentTimers(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param $data |
|
110
|
|
|
* @param string $service |
|
111
|
|
|
*/ |
|
112
|
|
|
private function saveServiceDataCache($service, $data) |
|
113
|
|
|
{ |
|
114
|
|
|
$cacheFile = getenv('alfred_workflow_data') . '/' . $service . '_cache.json'; |
|
115
|
|
|
file_put_contents($cacheFile, json_encode($data)); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param $service |
|
120
|
|
|
* @return mixed |
|
121
|
|
|
*/ |
|
122
|
|
|
private function syncServiceOnlineDataToLocalCache($service) |
|
123
|
|
|
{ |
|
124
|
|
|
$data = $this->$service->getOnlineData(); |
|
125
|
|
|
|
|
126
|
|
|
if (empty($data) === true) { |
|
127
|
|
|
return $this->setNotificationForService($service, 'data', false); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$this->saveServiceDataCache($service, $data); |
|
131
|
|
|
|
|
132
|
|
|
return $this->setNotificationForService($service, 'data', true); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.