Completed
Push — master ( 8070d4...a125b0 )
by Guillaume
02:37
created

Toggl::getProjects()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 11

Duplication

Lines 24
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 24
loc 24
rs 8.6845
cc 4
eloc 11
nc 4
nop 1
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
    /**
63
     * @param  $data
64
     * @return mixed
65
     */
66 View Code Duplication
    public function getProjects($data)
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...
67
    {
68
        $projects = [];
69
70
        if (isset($data['data']['projects']) === false) {
71
            return [];
72
        }
73
74
        /*
75
         * To only show projects that are currently active
76
         * The Toggl API is slightly weird on that
77
         */
78
        foreach ($data['data']['projects'] as $key => $project) {
79
            if (isset($project['server_deleted_at']) === true) {
80
                unset($data['data']['projects'][$key]);
81
            }
82
83
            $item['name'] = $project['name'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$item was never initialized. Although not strictly required by PHP, it is generally a good practice to add $item = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
84
            $item['id'] = $project['id'];
85
            $projects[] = $item;
86
        }
87
88
        return $projects;
89
    }
90
91
    public function getRecentTimers()
92
    {
93
        return array_reverse($this->timerAction('get_recent_timers', 'time_entries'));
94
    }
95
96
    /**
97
     * @param  $data
98
     * @return mixed
99
     */
100 View Code Duplication
    public function getTags($data)
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...
101
    {
102
        $tags = [];
103
104
        if (isset($data['data']['tags']) === false) {
105
            return [];
106
        }
107
108
        /*
109
         * To only show projects that are currently active
110
         * The Toggl API is slightly weird on that
111
         */
112
        foreach ($data['data']['tags'] as $key => $tag) {
113
            if (isset($tag['server_deleted_at']) === true) {
114
                unset($data['data']['tags'][$key]);
115
            }
116
117
            $item['name'] = $tag['name'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$item was never initialized. Although not strictly required by PHP, it is generally a good practice to add $item = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
118
            /**
119
             * Toggl API works with tag names, not IDs
120
             */
121
            $item['id'] = $tag['name'];
122
            $tags[] = $item;
123
        }
124
125
        return $tags;
126
    }
127
128
    /**
129
     * @param  $description
130
     * @param  $projectId
131
     * @param  $tagNames
132
     * @return mixed
133
     */
134
    public function startTimer($description, $projectId, $tagData)
135
    {
136
        $togglId = null;
137
        $item = [
138
            'time_entry' => [
139
                'description'  => $description,
140
                'pid'          => $projectId,
141
                'tags'         => explode(', ', $tagData),
142
                'created_with' => 'Alfred Time Workflow',
143
            ],
144
        ];
145
146
        $data = $this->timerAction('start', 'time_entries/start', ['json' => $item]);
147
148
        if (isset($data['data']['id']) === true) {
149
            $togglId = $data['data']['id'];
150
        }
151
152
        return $togglId;
153
    }
154
155
    /**
156
     * @param  $timerId
157
     * @return mixed
158
     */
159
    public function stopTimer($timerId = null)
160
    {
161
        return $this->timerAction('stop', 'time_entries/' . $timerId . '/stop');
162
    }
163
164
    /**
165
     * @param  string  $action
166
     * @param  string  $apiUri
167
     * @return mixed
168
     */
169
    public function timerAction($action, $apiUri, array $options = [])
170
    {
171
        $returnDataFor = ['start', 'get_recent_timers', 'get_online_data'];
172
        $methods = [
173
            'start'             => 'post',
174
            'stop'              => 'put',
175
            'delete'            => 'delete',
176
            'get_recent_timers' => 'get',
177
            'get_online_data'   => 'get',
178
        ];
179
        $method = isset($methods[$action]) ? $methods[$action] : '';
180
181 View Code Duplication
        if ($this->serviceApiCall->send($method, $apiUri, $options) === false) {
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...
182
            $this->message = $this->serviceApiCall->getMessage();
183
            return false;
184
        }
185
186
        if (in_array($action, $returnDataFor) === true) {
187
            return $this->serviceApiCall->getData();
188
        }
189
190
        return $this->serviceApiCall->last('success');
191
    }
192
}
193