Completed
Push — master ( 537205...0fe0f9 )
by Guillaume
02:15
created

Harvest::isTimerRunning()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 13
nc 9
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;
15
    private $message;
16
17 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...
18
    {
19
        $this->client = new Client([
20
            'base_uri' => 'https://' . $domain . '.harvestapp.com/daily/',
21
            'headers' => [
22
                'Content-type' => 'application/json',
23
                'Accept' => 'application/json',
24
                'Authorization' => 'Basic ' . $apiToken,
25
            ],
26
        ]);
27
28
        $this->setMessage('Just init');
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
        try {
42
            $response = $this->client->post('add', [
43
                'json' => $item,
44
            ]);
45
46 View Code Duplication
            if ($response->getStatusCode() !== 201) {
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...
47
                $this->setMessage('cannot start timer!');
48
            } else {
49
                $data = json_decode($response->getBody(), true);
50
                $harvestId = $data['id'];
51
                $this->setMessage('timer started');
52
            }
53
        } catch (ConnectException $e) {
54
            $this->setMessage('cannot connect to api!');
55
        } catch (ClientException $e) {
56
            $this->setMessage($e->getResponse()->getBody());
57
        }
58
59
        return $harvestId;
60
    }
61
62
    public function stopTimer($timerId = null)
63
    {
64
        $res = false;
65
66
        if ($this->isTimerRunning($timerId) === true) {
67
            try {
68
                $response = $this->client->get('timer/' . $timerId);
69
70
                if ($response->getStatusCode() !== 200) {
71
                    $this->setMessage('could not stop timer!');
72
                } else {
73
                    $this->setMessage('timer stopped');
74
                    $res = true;
75
                }
76
            } catch (ConnectException $e) {
77
                $this->setMessage('cannot connect to api!');
78
79
            } catch (ClientException $e) {
80
                $this->setMessage($e->getRequest()->getBody());
81
            }
82
        } else {
83
            $this->setMessage('timer was not running');
84
        }
85
86
        return $res;
87
    }
88
89 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...
90
    {
91
        $res = false;
92
93
        try {
94
        $response = $this->client->delete('delete/' . $timerId);
95
96
        if ($response->getStatusCode() !== 200) {
97
            $this->setMessage('could not delete timer!');
98
        } else {
99
            $this->setMessage('timer deleted');
100
            $res = true;
101
        }
102
        } catch (ConnectException $e) {
103
            $this->setMessage('cannot connect to api!');
104
        } catch (ClientException $e) {
105
            $this->setMessage($e->getResponse()->getBody());
106
        }
107
108
        return $res;
109
    }
110
111
    private function isTimerRunning($timerId)
112
    {
113
        $res = false;
114
115
        try {
116
            $response = $this->client->get('show/' . $timerId);
117
118
            if ($response->getStatusCode() === 200) {
119
                $data = json_decode($response->getBody(), true);
120
                if (isset($data['timer_started_at']) === true) {
121
                    $res = true;
122
                }
123
            }
124
        } catch (ConnectException $e) {
125
            $this->setMessage('cannot connect to api!');
126
        } catch (ClientException $e) {
127
            $this->setMessage($e->getResponse()->getBody());
128
        }
129
130
        return $res;
131
    }
132
133
    public function getLastMessage()
134
    {
135
        return $this->message;
136
    }
137
138
    private function setMessage($message = null)
139
    {
140
        $this->message = '- Harvest: ' . $message;
141
    }
142
}
143