Completed
Push — master ( 8a9213...8197b4 )
by Guillaume
02:13
created

Toggl::startTimer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 12
nc 2
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
    public function deleteTimer($timerId = null)
42
    {
43
        return $this->timerAction('delete', 'time_entries/' . $timerId);
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function getLastMessage()
50
    {
51
        return $this->message;
52
    }
53
54
    /**
55
     * @return mixed
56
     */
57
    public function getOnlineData()
58
    {
59
        return $this->timerAction('get_online_data', 'me?with_related_data=true');
60
    }
61
62
    public function getProjects()
63
    {
64
        # code...
65
    }
66
67
    public function getRecentTimers()
68
    {
69
        return array_reverse($this->timerAction('get_recent_timers', 'time_entries'));
70
    }
71
72
    /**
73
     * @param  $description
74
     * @param  $projectId
75
     * @param  $tagNames
76
     * @return mixed
77
     */
78
    public function startTimer($description, $projectId, $tagNames)
79
    {
80
        $togglId = null;
81
82
        $item = [
83
            'time_entry' => [
84
                'description'  => $description,
85
                'pid'          => $projectId,
86
                'tags'         => explode(', ', $tagNames),
87
                'created_with' => 'Alfred Time Workflow',
88
            ],
89
        ];
90
91
        $data = $this->timerAction('start', 'time_entries/start', ['json' => $item]);
92
93
        if (isset($data['data']['id']) === true) {
94
            $togglId = $data['data']['id'];
95
        }
96
97
        return $togglId;
98
    }
99
100
    /**
101
     * @param  $timerId
102
     * @return mixed
103
     */
104
    public function stopTimer($timerId = null)
105
    {
106
        return $this->timerAction('stop', 'time_entries/' . $timerId . '/stop');
107
    }
108
109
    /**
110
     * @param  string  $action
111
     * @param  string  $apiUri
112
     * @return mixed
113
     */
114
    private function timerAction($action, $apiUri, array $options = [])
115
    {
116
        $res = false;
117
        $returnDataFor = ['start', 'get_recent_timers', 'get_online_data'];
118
        $methods = [
119
            'start'             => 'post',
120
            'stop'              => 'put',
121
            'delete'            => 'delete',
122
            'get_recent_timers' => 'get',
123
            'get_online_data'   => 'get',
124
        ];
125
        $method = isset($methods[$action]) ? $methods[$action] : '';
126
127 View Code Duplication
        if ($this->serviceApiCall->send($method, $apiUri, $options) === 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...
128
            $res = $this->serviceApiCall->last('success');
129
130
            if (in_array($action, $returnDataFor) === true) {
131
                $res = $this->serviceApiCall->getData();
132
            }
133
        } else {
134
            $this->message = $this->serviceApiCall->getMessage();
135
        }
136
137
        return $res;
138
    }
139
}
140