Completed
Push — master ( e61af8...4238f3 )
by Guillaume
03:28 queued 01:23
created

Toggl::getProjects()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace AlfredTime;
4
5
use AlfredTime\ServiceApiCall;
6
7
/**
8
 *
9
 */
10
class Toggl
11
{
12
    /**
13
     * @var string
14
     */
15
    private $message = '';
16
17
    /**
18
     * @var mixed
19
     */
20
    private $serviceApiCall = null;
21
22
    /**
23
     * @param $apiToken
24
     */
25 View Code Duplication
    public function __construct($apiToken = null)
0 ignored issues
show
Duplication introduced by
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...
26
    {
27
        $this->serviceApiCall = new ServiceApiCall([
28
            'base_uri' => 'https://www.toggl.com/api/v8/',
29
            'headers'  => [
30
                'Content-type'  => 'application/json',
31
                'Accept'        => 'application/json',
32
                'Authorization' => 'Basic ' . base64_encode($apiToken . ':api_token'),
33
            ],
34
        ]);
35
    }
36
37
    /**
38
     * @param  $timerId
39
     * @return mixed
40
     */
41 View Code Duplication
    public function deleteTimer($timerId = null)
0 ignored issues
show
Duplication introduced by
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...
42
    {
43
        $res = $this->timerAction('delete', 'time_entries/' . $timerId);
44
45
        if ($res === true) {
46
            $this->setMessage('timer deleted');
47
        } else {
48
            $this->setMessage('could not delete timer! [' . $this->message . ']');
49
        }
50
51
        return $res;
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getLastMessage()
58
    {
59
        return $this->message;
60
    }
61
62
    /**
63
     * @return mixed
64
     */
65 View Code Duplication
    public function getOnlineData()
0 ignored issues
show
Duplication introduced by
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...
66
    {
67
        $data = $this->timerAction('get_online_data', 'me?with_related_data=true');
68
69
        if (empty($data) === false) {
70
            $this->setMessage('data cached');
71
        } else {
72
            $this->setMessage('cannot get online data! [' . $this->message . ']');
73
        }
74
75
        return $data;
76
    }
77
78
    public function getProjects()
79
    {
80
        # code...
81
    }
82
83
    public function getRecentTimers()
84
    {
85
        return array_reverse($this->timerAction('get_recent_timers', 'time_entries'));
86
    }
87
88
    /**
89
     * @param  $description
90
     * @param  $projectId
91
     * @param  $tagNames
92
     * @return mixed
93
     */
94
    public function startTimer($description, $projectId, $tagNames)
95
    {
96
        $togglId = null;
97
98
        $item = [
99
            'time_entry' => [
100
                'description'  => $description,
101
                'pid'          => $projectId,
102
                'tags'         => explode(', ', $tagNames),
103
                'created_with' => 'Alfred Time Workflow',
104
            ],
105
        ];
106
107
        $data = $this->timerAction('start', 'time_entries/start', ['json' => $item]);
108
109
        if (isset($data['data']['id']) === true) {
110
            $this->setMessage('timer started');
111
            $togglId = $data['data']['id'];
112
        } else {
113
            $this->setMessage('could not start timer! [' . $this->message . ']');
114
        }
115
116
        return $togglId;
117
    }
118
119
    /**
120
     * @param  $timerId
121
     * @return mixed
122
     */
123 View Code Duplication
    public function stopTimer($timerId = null)
0 ignored issues
show
Duplication introduced by
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...
124
    {
125
        $res = $this->timerAction('stop', 'time_entries/' . $timerId . '/stop');
126
127
        if ($res === true) {
128
            $this->setMessage('timer stopped');
129
        } else {
130
            $this->setMessage('could not stop timer! [' . $this->message . ']');
131
        }
132
133
        return $res;
134
    }
135
136
    /**
137
     * @param string $message
138
     */
139
    private function setMessage($message = null)
140
    {
141
        $this->message = '- Toggl: ' . $message;
142
    }
143
144
    /**
145
     * @param  string  $action
146
     * @param  string  $apiUri
147
     * @return mixed
148
     */
149
    private function timerAction($action, $apiUri, array $options = [])
150
    {
151
        $res = false;
152
        $method = '';
0 ignored issues
show
Unused Code introduced by
$method 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 $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.

Loading history...
153
        $returnDataFor = ['start', 'get_recent_timers', 'get_online_data'];
154
        $methods = [
155
            'start'             => 'post',
156
            'stop'              => 'put',
157
            'delete'            => 'delete',
158
            'get_recent_timers' => 'get',
159
            'get_online_data'   => 'get',
160
        ];
161
        $method = isset($methods[$action]) ? $methods[$action] : '';
162
163
        if ($this->serviceApiCall->send($method, $apiUri, $options) === true) {
164
            $res = $this->serviceApiCall->last('success');
165
166
            if (in_array($action, $returnDataFor) === true) {
167
                $res = $this->serviceApiCall->getData();
168
            }
169
        } else {
170
            $this->message = $this->serviceApiCall->getMessage();
171
        }
172
173
        return $res;
174
    }
175
}
176