Completed
Push — master ( 3c2d39...503e4f )
by Guillaume
02:18
created

Toggl::timerAction()   C

Complexity

Conditions 8
Paths 18

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 25
nc 18
nop 3
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('time deleted');
47
        } else {
48
            $this->setMessage('could not delete timer!' . $this->message);
49
        }
50
51
        return $res;
52
    }
53
54
    /**
55
     * @return mixed
56
     */
57
    public function getLastMessage()
58
    {
59
        return $this->message;
60
    }
61
62
    /**
63
     * @return mixed
64
     */
65
    public function getOnlineData()
66
    {
67
        $data = [];
0 ignored issues
show
Unused Code introduced by
$data 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...
68
69
        $data = $this->timerAction('get_online_data', 'me?with_related_data=true');
70
71
        if (empty($data) === false) {
72
            $this->setMessage('data cached');
73
        } else {
74
            $this->setMessage('cannot get online data! [' . $this->message . ']');
75
        }
76
77
        return $data;
78
    }
79
80
    public function getProjects()
81
    {
82
        # code...
83
    }
84
85
    public function getRecentTimers()
86
    {
87
        return array_reverse($this->timerAction('get_recent_timers', 'time_entries'));
88
    }
89
90
    /**
91
     * @param  $description
92
     * @param  $projectId
93
     * @param  $tagNames
94
     * @return mixed
95
     */
96
    public function startTimer($description, $projectId, $tagNames)
97
    {
98
        $togglId = null;
99
100
        $item = [
101
            'time_entry' => [
102
                'description'  => $description,
103
                'pid'          => $projectId,
104
                'tags'         => explode(', ', $tagNames),
105
                'created_with' => 'Alfred Time Workflow',
106
            ],
107
        ];
108
109
        $data = $this->timerAction('start', 'time_entries/start', ['json' => $item]);
110
111
        if (isset($data['data']['id']) === true) {
112
            $this->setMessage('timer started');
113
            $togglId = $data['data']['id'];
114
        } else {
115
            $this->setMessage('could not start timer! [' . $this->message . ']');
116
        }
117
118
        return $togglId;
119
    }
120
121
    /**
122
     * @param  $timerId
123
     * @return mixed
124
     */
125 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...
126
    {
127
        $res = $this->timerAction('stop', 'time_entries/' . $timerId . '/stop');
128
129
        if ($res === true) {
130
            $this->setMessage('timer stopped');
131
        } else {
132
            $this->setMessage('could not stop timer! [' . $this->message . ']');
133
        }
134
135
        return $res;
136
    }
137
138
    /**
139
     * @param $message
140
     */
141
    private function setMessage($message = null)
142
    {
143
        $this->message = '- Toggl: ' . $message;
144
    }
145
146
    /**
147
     * @param  $action
148
     * @param  $apiUri
149
     * @return mixed
150
     */
151
    private function timerAction($action, $apiUri, array $options = [])
152
    {
153
        $res = false;
154
        $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...
155
        $returnDataFor = ['start', 'get_recent_timers', 'get_online_data'];
156
157
        switch ($action) {
158
            case 'delete':
159
                $method = 'delete';
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...
160
                break;
161
            case 'stop':
162
                $method = 'put';
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...
163
                break;
164
            case 'start':
165
                $method = 'post';
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...
166
                break;
167
            case 'get_recent_timers':
168
            // no break
169
            case 'get_online_data':
170
                $method = 'get';
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...
171
                break;
172
        }
173
174
        if ($this->serviceApiCall->send($methods, $apiUri, $options) === true) {
0 ignored issues
show
Bug introduced by
The variable $methods does not exist. Did you mean $method?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
175
            $res = $this->serviceApiCall->last('success');
176
177
            if (in_array($action, $returnDataFor) === true) {
178
                $res = $this->serviceApiCall->getData();
179
            }
180
        } else {
181
            $this->message = $this->serviceApiCall->getMessage();
182
        }
183
184
        return $res;
185
    }
186
}
187