These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace AlfredTime; |
||
4 | |||
5 | use AlfredTime\Toggl; |
||
6 | use AlfredTime\Config; |
||
7 | use AlfredTime\Harvest; |
||
8 | |||
9 | class Timer |
||
10 | { |
||
11 | /** |
||
12 | * @var mixed |
||
13 | */ |
||
14 | private $config; |
||
15 | |||
16 | /** |
||
17 | * @var mixed |
||
18 | */ |
||
19 | private $harvest; |
||
20 | |||
21 | /** |
||
22 | * @var mixed |
||
23 | */ |
||
24 | private $toggl; |
||
25 | |||
26 | /** |
||
27 | * @param Config $config |
||
28 | */ |
||
29 | View Code Duplication | public function __construct(Config $config = null) |
|
0 ignored issues
–
show
|
|||
30 | { |
||
31 | $this->config = $config; |
||
32 | $this->harvest = new Harvest( |
||
33 | $this->config->get('harvest', 'domain'), |
||
34 | $this->config->get('harvest', 'api_token') |
||
35 | ); |
||
36 | $this->toggl = new Toggl($this->config->get('toggl', 'api_token')); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @param array $timerData |
||
41 | * @return mixed |
||
42 | */ |
||
43 | public function delete(array $timerData = []) |
||
44 | { |
||
45 | $res = []; |
||
46 | |||
47 | if (empty($timerData) === true) { |
||
48 | return []; |
||
49 | } |
||
50 | |||
51 | foreach ($timerData as $service => $id) { |
||
52 | $res[$service] = $this->deleteServiceTimer($service, $id); |
||
53 | } |
||
54 | |||
55 | return $res; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @param $service |
||
60 | * @param $timerId |
||
61 | * @return boolean |
||
62 | */ |
||
63 | public function deleteServiceTimer($service, $timerId) |
||
64 | { |
||
65 | if ($this->$service->deleteTimer($timerId) === false) { |
||
66 | return false; |
||
67 | } |
||
68 | |||
69 | if ($timerId === $this->getProperty($service . '_id')) { |
||
70 | $this->updateProperty($service . '_id', null); |
||
71 | $this->updateProperty('is_running', false); |
||
72 | } |
||
73 | |||
74 | return true; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @return mixed |
||
79 | */ |
||
80 | public function getDescription() |
||
81 | { |
||
82 | return $this->getProperty('description'); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @return mixed |
||
87 | */ |
||
88 | public function getPrimaryService() |
||
89 | { |
||
90 | return $this->getProperty('primary_service'); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @return mixed |
||
95 | */ |
||
96 | public function isRunning() |
||
97 | { |
||
98 | return $this->getProperty('is_running'); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param $description |
||
103 | * @param array $projectData |
||
104 | * @param array $tagData |
||
105 | * @param $specificService |
||
106 | * @return array |
||
107 | */ |
||
108 | public function start($description = '', array $projectData = [], array $tagData = [], $specificService = null) |
||
109 | { |
||
110 | $res = []; |
||
111 | $oneServiceStarted = false; |
||
112 | |||
113 | $servicesToRun = ($specificService === null) |
||
114 | ? $this->config->activatedServices() |
||
115 | : [$specificService]; |
||
116 | |||
117 | /** |
||
118 | * When starting a new timer, all the services timer IDs have to be put to null |
||
119 | * so that when the user uses the UNDO feature, it doesn't delete old previous |
||
120 | * other services timers. The timer IDs are used for the UNDO feature and |
||
121 | * should then contain the IDs of the last starts through the workflow, not |
||
122 | * through each individual sefrvice |
||
123 | */ |
||
124 | if (empty($servicesToRun) === true) { |
||
125 | return []; |
||
126 | } |
||
127 | |||
128 | foreach ($this->config->activatedServices() as $service) { |
||
129 | $this->updateProperty($service . '_id', null); |
||
130 | } |
||
131 | |||
132 | foreach ($servicesToRun as $service) { |
||
133 | $timerId = $this->$service->startTimer( |
||
134 | $description, |
||
135 | $projectData[$service], |
||
136 | $tagData[$service] |
||
137 | ); |
||
138 | $res[$service] = $timerId; |
||
139 | $this->updateProperty($service . '_id', $timerId); |
||
140 | $oneServiceStarted = $oneServiceStarted || ($timerId !== null); |
||
141 | } |
||
142 | |||
143 | if ($oneServiceStarted === true) { |
||
144 | $this->updateProperty('description', $description); |
||
145 | $this->updateProperty('is_running', true); |
||
146 | } |
||
147 | |||
148 | return $res; |
||
149 | } |
||
150 | |||
151 | public function stop() |
||
152 | { |
||
153 | $res = []; |
||
154 | $oneServiceStopped = false; |
||
155 | |||
156 | foreach ($this->config->runningServices() as $service) { |
||
157 | $timerId = $this->getProperty($service . '_id'); |
||
158 | $res[$service] = $this->$service->stopTimer($timerId); |
||
159 | $oneServiceStopped = $oneServiceStopped || $res[$service]; |
||
160 | } |
||
161 | |||
162 | if ($oneServiceStopped === true) { |
||
163 | $this->updateProperty('is_running', false); |
||
164 | } |
||
165 | |||
166 | return $res; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * @return string |
||
171 | */ |
||
172 | public function undo() |
||
173 | { |
||
174 | $timerData = []; |
||
175 | |||
176 | foreach ($this->config->runningServices() as $service) { |
||
177 | $timerData[$service] = $this->getProperty($service . '_id'); |
||
178 | } |
||
179 | |||
180 | return $this->delete($timerData); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @param $name |
||
185 | * @param $value |
||
186 | */ |
||
187 | public function updateProperty($name, $value) |
||
188 | { |
||
189 | $this->config->update('timer', $name, $value); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @param $name |
||
194 | * @return mixed |
||
195 | */ |
||
196 | private function getProperty($name) |
||
197 | { |
||
198 | return $this->config->get('timer', $name); |
||
199 | } |
||
200 | } |
||
201 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.