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