Completed
Push — master ( 945b79...1bf282 )
by Guillaume
03:07 queued 52s
created

Toggl::startTimer()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 17

Duplication

Lines 10
Ratio 38.46 %

Importance

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