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 | /** |
||
6 | * |
||
7 | */ |
||
8 | class Toggl extends Service |
||
9 | { |
||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | protected $apiBaseUrl = 'https://www.toggl.com/api/v8/'; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $methods = [ |
||
19 | 'start' => 'post', |
||
20 | 'stop' => 'put', |
||
21 | 'delete' => 'delete', |
||
22 | 'get_recent_timers' => 'get', |
||
23 | 'get_online_data' => 'get', |
||
24 | ]; |
||
25 | |||
26 | /** |
||
27 | * @param $apiToken |
||
28 | */ |
||
29 | public function __construct($apiToken = null) |
||
30 | { |
||
31 | parent::__construct($this->apiBaseUrl, base64_encode($apiToken . ':api_token')); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @param $timerId |
||
36 | */ |
||
37 | public function apiDeleteUrl($timerId) |
||
38 | { |
||
39 | return 'time_entries/' . $timerId; |
||
40 | } |
||
41 | |||
42 | public function apiStartUrl() |
||
43 | { |
||
44 | return 'time_entries/start'; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param $timerId |
||
49 | */ |
||
50 | public function apiStopUrl($timerId) |
||
51 | { |
||
52 | return 'time_entries/' . $timerId . '/stop'; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param $description |
||
57 | * @param $projectId |
||
58 | * @param $tagData |
||
59 | */ |
||
60 | public function generateTimer($description, $projectId, $tagData) |
||
61 | { |
||
62 | return [ |
||
63 | 'time_entry' => [ |
||
64 | 'description' => $description, |
||
65 | 'pid' => $projectId, |
||
66 | 'tags' => explode(', ', $tagData), |
||
67 | 'created_with' => 'Alfred Time Workflow', |
||
68 | ], |
||
69 | ]; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @return mixed |
||
74 | */ |
||
75 | public function getOnlineData() |
||
76 | { |
||
77 | $data = []; |
||
78 | |||
79 | $togglData = $this->timerAction('get_online_data', 'me?with_related_data=true'); |
||
80 | |||
81 | if ($togglData !== false) { |
||
82 | $data = $togglData; |
||
83 | } |
||
84 | |||
85 | return $data; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param $projectId |
||
90 | * @return mixed |
||
91 | */ |
||
92 | public function getProjectName($projectId, array $data = []) |
||
93 | { |
||
94 | $projectName = ''; |
||
95 | $projects = $this->getProjects($data); |
||
96 | |||
97 | foreach ($projects as $project) { |
||
98 | if ($project['id'] === $projectId) { |
||
99 | $projectName = $project['name']; |
||
100 | |||
101 | break; |
||
102 | } |
||
103 | } |
||
104 | |||
105 | return $projectName; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param $data |
||
110 | * @return mixed |
||
111 | */ |
||
112 | public function getProjects($data) |
||
113 | { |
||
114 | return $this->getItems('projects', $data); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @return mixed |
||
119 | */ |
||
120 | View Code Duplication | public function getRecentTimers() |
|
0 ignored issues
–
show
|
|||
121 | { |
||
122 | $timers = []; |
||
123 | |||
124 | foreach ($this->timerAction('get_recent_timers', 'time_entries') as $timeEntry) { |
||
125 | $timers[] = [ |
||
126 | 'id' => $timeEntry['id'], |
||
127 | 'description' => $timeEntry['description'], |
||
128 | 'project_id' => $timeEntry['pid'], |
||
129 | 'project_name' => $timeEntry['pid'], |
||
130 | 'tags' => empty($timeEntry['tags']) ? '' : implode(', ', $timeEntry['tags']), |
||
131 | 'duration' => $timeEntry['duration'], |
||
132 | ]; |
||
133 | } |
||
134 | |||
135 | return array_reverse($timers); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param $data |
||
140 | * @return mixed |
||
141 | */ |
||
142 | public function getTags($data) |
||
143 | { |
||
144 | return $this->getItems('tags', $data); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @param $action |
||
149 | * @return mixed |
||
150 | */ |
||
151 | protected function methodForAction($action) |
||
152 | { |
||
153 | if (isset($this->methods[$action]) === false) { |
||
154 | return; |
||
155 | } |
||
156 | |||
157 | return $this->methods[$action]; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @param string $action |
||
162 | * @param string $apiUri |
||
163 | * @return mixed |
||
164 | */ |
||
165 | View Code Duplication | protected function timerAction($action, $apiUri, array $options = []) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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.
Loading history...
|
|||
166 | { |
||
167 | $returnDataFor = ['start', 'get_recent_timers', 'get_online_data']; |
||
168 | |||
169 | $method = $this->methodForAction($action); |
||
170 | |||
171 | return $this->serviceApiCall->send( |
||
172 | $method, |
||
173 | $apiUri, |
||
174 | $options, |
||
175 | in_array($action, $returnDataFor) |
||
176 | ); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @param $needle |
||
181 | * @param array $haystack |
||
182 | * @return mixed |
||
183 | */ |
||
184 | private function getItems($needle, array $haystack = []) |
||
185 | { |
||
186 | $items = []; |
||
0 ignored issues
–
show
$items is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
187 | |||
188 | if (isset($haystack['data'][$needle]) === false) { |
||
189 | return []; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * To only show projects that are currently active |
||
194 | * The Toggl API is slightly weird on that |
||
195 | */ |
||
196 | $items = array_filter($haystack['data'][$needle], function ($item) { |
||
197 | return isset($item['server_deleted_at']) === false; |
||
198 | }); |
||
199 | |||
200 | return array_map(function ($item) use ($needle) { |
||
201 | return [ |
||
202 | 'name' => $item['name'], |
||
203 | 'id' => ($needle === 'tags') ? $item['name'] : $item['id'], |
||
204 | ]; |
||
205 | }, $items); |
||
206 | } |
||
207 | } |
||
208 |
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.