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

Harvest::getProjects()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 16
loc 16
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
3
namespace AlfredTime;
4
5
use AlfredTime\ServiceApiCall;
6
7
/**
8
 *
9
 */
10
class Harvest
11
{
12
    /**
13
     * @var string
14
     */
15
    private $message = '';
16
17
    /**
18
     * @var mixed
19
     */
20
    private $serviceApiCall = null;
21
22
    /**
23
     * @param $domain
24
     * @param null      $apiToken
25
     */
26 View Code Duplication
    public function __construct($domain = null, $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...
27
    {
28
        $this->serviceApiCall = new ServiceApiCall([
29
            'base_uri' => 'https://' . $domain . '.harvestapp.com/',
30
            'headers'  => [
31
                'Content-type'  => 'application/json',
32
                'Accept'        => 'application/json',
33
                'Authorization' => 'Basic ' . $apiToken,
34
            ],
35
        ]);
36
    }
37
38
    /**
39
     * @param  $timerId
40
     * @return mixed
41
     */
42
    public function deleteTimer($timerId = null)
43
    {
44
        return $this->timerAction('delete', 'daily/delete/' . $timerId);
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    public function getLastMessage()
51
    {
52
        return $this->message;
53
    }
54
55
    /**
56
     * @return mixed
57
     */
58
    public function getOnlineData()
59
    {
60
        $data['projects'] = $this->timerAction('get_projects', 'projects');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = 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...
61
        $data['tasks'] = $this->timerAction('get_tags', 'tasks');
62
63
        return $data;
64
    }
65
66
    /**
67
     * @param $data
68
     * @return mixed
69
     */
70 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...
71
    {
72
        $projects = [];
73
74
        if (isset($data['projects']) === false) {
75
            return [];
76
        }
77
78
        foreach ($data['projects'] as $project) {
79
            $item['name'] = $project['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...
80
            $item['id'] = $project['project']['id'];
81
            $projects[] = $item;
82
        }
83
84
        return $projects;
85
    }
86
87 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...
88
    {
89
        $tags = [];
90
91
        if (isset($data['tasks']) === false) {
92
            return [];
93
        }
94
95
        foreach ($data['tasks'] as $tag) {
96
            $item['name'] = $tag['task']['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...
97
            $item['id'] = $tag['task']['id'];
98
            $tags[] = $item;
99
        }
100
101
        return $tags;
102
    }
103
104
    /**
105
     * @param  $description
106
     * @param  $projectId
107
     * @param  $taskId
108
     * @return mixed
109
     */
110
    public function startTimer($description, $projectId, $taskId)
111
    {
112
        $harvestId = null;
113
114
        $item = [
115
            'notes'      => $description,
116
            'project_id' => $projectId,
117
            'task_id' => $taskId,
118
        ];
119
120
        $data = $this->timerAction('start', 'daily/add/', ['json' => $item]);
121
122
        if (isset($data['id']) === true) {
123
            $harvestId = $data['id'];
124
        }
125
126
        return $harvestId;
127
    }
128
129
    /**
130
     * @param  $timerId
131
     * @return mixed
132
     */
133
    public function stopTimer($timerId = null)
134
    {
135
        if ($this->isTimerRunning($timerId) === false) {
136
            return false;
137
        }
138
139
        return $this->timerAction('stop', 'daily/timer/' . $timerId);
140
    }
141
142
    /**
143
     * @param  $timerId
144
     * @return boolean
145
     */
146
    private function isTimerRunning($timerId)
147
    {
148
        $data = $this->timerAction('timer_running', 'daily/show/' . $timerId);
149
150
        return isset($data['timer_started_at']);
151
    }
152
153
    /**
154
     * @param  string  $action
155
     * @param  string  $apiUri
156
     * @return mixed
157
     */
158
    private function timerAction($action, $apiUri, array $options = [])
159
    {
160
        $returnDataFor = [
161
            'start',
162
            'timer_running',
163
            'get_projects',
164
            'get_tags',
165
        ];
166
        $methods = [
167
            'start'         => 'post',
168
            'stop'          => 'get',
169
            'delete'        => 'delete',
170
            'timer_running' => 'get',
171
            'get_projects'  => 'get',
172
            'get_tags'      => 'get',
173
            '',
174
        ];
175
176
        $method = isset($methods[$action]) ? $methods[$action] : '';
177
178 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...
179
            $this->message = $this->serviceApiCall->getMessage();
180
181
            return false;
182
        }
183
184
        if (in_array($action, $returnDataFor) === true) {
185
            return $this->serviceApiCall->getData();
186
        }
187
188
        return $this->serviceApiCall->last('success');
189
    }
190
}
191