Completed
Push — master ( da6153...10ffd2 )
by Guillaume
02:11
created

Harvest::lastApiCall()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 3
nop 1
1
<?php
2
3
require __DIR__ . '/../vendor/autoload.php';
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\ClientException;
7
use GuzzleHttp\Exception\ConnectException;
8
9
/**
10
 *
11
 */
12
class Harvest
13
{
14
    private $client = null;
15
    private $code = 0;
16
    private $message = '';
17
    private $data = [];
18
19 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...
20
    {
21
        $this->client = new Client([
22
            'base_uri' => 'https://' . $domain . '.harvestapp.com/daily/',
23
            'headers' => [
24
                'Content-type' => 'application/json',
25
                'Accept' => 'application/json',
26
                'Authorization' => 'Basic ' . $apiToken,
27
            ],
28
        ]);
29
    }
30
31
    public function startTimer($description, $projectId, $taskId)
32
    {
33
        $harvestId = null;
34
35
        $item = [
36
            'notes' => $description,
37
            'project_id' => $projectId,
38
            'task_id' => $taskId,
39
        ];
40
41 View Code Duplication
        if ($this->sendApiCall('post', 'add', ['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...
42
            if ($this->lastApiCall('success') === true) {
43
                $this->setMessage('timer started');
44
                $harvestId = $this->data['id'];
45
            } else {
46
                $this->setMessage('cannot start timer!');
47
            }
48
        }
49
50
        return $harvestId;
51
    }
52
53
    public function stopTimer($timerId = null)
54
    {
55
        $res = false;
56
57
        if ($this->isTimerRunning($timerId) === true) {
58
            try {
59
                $response = $this->client->get('timer/' . $timerId);
60
61
                if ($response->getStatusCode() !== 200) {
62
                    $this->setMessage('could not stop timer!');
63
                } else {
64
                    $this->setMessage('timer stopped');
65
                    $res = true;
66
                }
67
            } catch (ConnectException $e) {
68
                $this->setMessage('cannot connect to api!');
69
70
            } catch (ClientException $e) {
71
                $this->setMessage($e->getRequest()->getBody());
72
            }
73
        } else {
74
            $this->setMessage('timer was not running');
75
        }
76
77
        return $res;
78
    }
79
80 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...
81
    {
82
        $res = false;
83
84
        if ($this->sendApiCall('delete', 'delete/' . $timerId) === true) {
85
            if ($this->lastApiCall('success') === true) {
86
                $this->setMessage('timer deleted');
87
                $res = true;
88
            } else {
89
                $this->setMessage('could not delete timer!');
90
            }
91
        }
92
93
        return $res;
94
    }
95
96
    private function isTimerRunning($timerId)
97
    {
98
        $res = false;
99
100
        if ($this->sendApiCall('get', 'show/' .$timerId) === true) {
101
            if ($this->lastApiCall('success') === true) {
102
                if (isset($this->data['timer_started_at']) === true) {
103
                    $res = true;
104
                }
105
            }
106
        }
107
108
        return $res;
109
    }
110
111
    public function getLastMessage()
112
    {
113
        return $this->message;
114
    }
115
116
    private function setMessage($message = null)
117
    {
118
        $this->message = '- Harvest: ' . $message;
119
    }
120
121 View Code Duplication
    private function sendApiCall($method, $uri = '', array $options = [])
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...
122
    {
123
        $res = true;
124
125
        try {
126
            $response = $this->client->request(strtoupper($method), $uri, $options);
127
            $this->code = $response->getStatusCode();
128
            $this->data = json_decode($response->getBody(), true);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode($response->getBody(), true) of type * is incompatible with the declared type array of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
129
        } catch (ConnectException $e) {
130
            $this->setMessage('cannot connect to api!');
131
            $res = false;
132
        } catch (ClientException $e) {
133
            $this->setMessage($e->getResponse()->getBody());
134
        }
135
136
        return $res;
137
    }
138
139 View Code Duplication
    private function lastApiCall($status = '')
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...
140
    {
141
        $res = false;
142
143
        switch ($status) {
144
            case 'success':
145
                if ($this->code === 200 || $this->code === 201) {
146
                    $res = true;
147
                }
148
                break;
149
        }
150
151
        return $res;
152
    }
153
}
154