1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlfredTime; |
4
|
|
|
|
5
|
|
|
use AlfredTime\ServiceApiCall; |
6
|
|
|
|
7
|
|
|
abstract class Service |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var mixed |
11
|
|
|
*/ |
12
|
|
|
protected $serviceApiCall = null; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param $timerId |
16
|
|
|
* @return mixed |
17
|
|
|
*/ |
18
|
|
|
public function deleteTimer($timerId) |
19
|
|
|
{ |
20
|
|
|
return $this->serviceApiCall->send( |
21
|
|
|
$this->methodForAction('delete'), |
22
|
|
|
$this->apiDeleteUrl($timerId) |
23
|
|
|
); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param $descrition |
28
|
|
|
* @param $projectId |
29
|
|
|
* @param $tagData |
30
|
|
|
* @return mixed |
31
|
|
|
*/ |
32
|
|
|
public function startTimer($description, $projectId, $tagData) |
33
|
|
|
{ |
34
|
|
|
$timerId = null; |
35
|
|
|
|
36
|
|
|
$item = $this->generateTimer($description, $projectId, $tagData); |
37
|
|
|
|
38
|
|
|
$data = $this->serviceApiCall->send( |
39
|
|
|
$this->methodForAction('start'), |
40
|
|
|
$this->apiStartUrl(), |
41
|
|
|
['json' => $item], |
42
|
|
|
true |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
if (isset($data['id']) === true) { |
46
|
|
|
$timerId = $data['id']; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (isset($data['data']['id']) === true) { |
50
|
|
|
$timerId = $data['data']['id']; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $timerId; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $timerId |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
|
|
public function stopTimer($timerId) |
61
|
|
|
{ |
62
|
|
|
return $this->serviceApiCall->send( |
63
|
|
|
$this->methodForAction('stop'), |
64
|
|
|
$this->apiStopUrl($timerId) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param $baseUri |
70
|
|
|
* @param $apiToken |
71
|
|
|
*/ |
72
|
|
|
protected function __construct($baseUri, $credentials) |
73
|
|
|
{ |
74
|
|
|
$this->serviceApiCall = new ServiceApiCall([ |
75
|
|
|
'base_uri' => $baseUri, |
76
|
|
|
'headers' => [ |
77
|
|
|
'Authorization' => 'Basic ' . $credentials, |
78
|
|
|
], |
79
|
|
|
]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $timerId |
84
|
|
|
*/ |
85
|
|
|
abstract protected function apiDeleteUrl($timerId); |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param $timerId |
89
|
|
|
*/ |
90
|
|
|
abstract protected function apiStopUrl($timerId); |
91
|
|
|
|
92
|
|
|
abstract protected function getOnlineData(); |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $data |
96
|
|
|
*/ |
97
|
|
|
abstract protected function getProjects($data); |
98
|
|
|
|
99
|
|
|
abstract protected function getRecentTimers(); |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param $data |
103
|
|
|
*/ |
104
|
|
|
abstract protected function getTags($data); |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param $action |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
|
|
protected function methodForAction($action) |
111
|
|
|
{ |
112
|
|
|
if (isset($this->methods[$action]) === false) { |
|
|
|
|
113
|
|
|
return; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->methods[$action]; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: