Completed
Push — master ( b0e7fe...884141 )
by Guillaume
02:00
created

Harvest::getRecentTimers()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 25
Code Lines 16

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 2
nop 0
dl 25
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 */
5
class Harvest
6
{
7
    private $message;
8
    private $domain;
9
    private $apiToken;
10
11
    public function __construct($domain = null, $apiToken = null)
12
    {
13
        $this->message = '';
14
        $this->domain = $domain;
15
        $this->apiToken = $apiToken;
16
    }
17
18
    public function startTimer($description, $projectId, $taskId)
19
    {
20
        $harvestId = null;
21
22
        $url = 'https://' . $this->domain . '.harvestapp.com/daily/add';
23
24
        $base64Token = $this->apiToken;
25
26
        $headers = [
27
            "Content-type: application/json",
28
            "Accept: application/json",
29
            'Authorization: Basic ' . $base64Token,
30
        ];
31
32
        $item = [
33
            'notes' => $description,
34
            'project_id' => $projectId,
35
            'task_id' => $taskId,
36
        ];
37
38
        $ch = curl_init($url);
39
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
40
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
41
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($item, true));
42
        $response = curl_exec($ch);
43
        $lastHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
44
        curl_close($ch);
45
46
        if ($response === false || $lastHttpCode !== 201) {
47
            $this->message = '- Cannot start Harvest timer!';
48
        } else {
49
            $data = json_decode($response, true);
50
            $harvestId = $data['id'];
51
            $this->message = '- Harvest timer started';
52
        }
53
54
        return $harvestId;
55
    }
56
57
    public function stopTimer($timerId = null)
58
    {
59
        $res = false;
60
61
        if ($this->isTimerRunning($timerId) === true) {
62
            $url = 'https://' . $this->domain . '.harvestapp.com/daily/timer/' . $timerId;
63
64
            $base64Token = $this->apiToken;
65
66
            $headers = [
67
                "Content-type: application/json",
68
                "Accept: application/json",
69
                'Authorization: Basic ' . $base64Token,
70
            ];
71
72
            $ch = curl_init($url);
73
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
74
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
75
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
76
            $response = curl_exec($ch);
77
            $lastHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
78
            curl_close($ch);
79
80
            if ($response === false || $lastHttpCode !== 200) {
81
                $this->message = '- Could not stop Harvest timer!';
82
            } else {
83
                $this->message = '- Harvest timer stopped';
84
                $res = true;
85
            }
86
        } else {
87
            $this->message = '- Harvest timer was not running';
88
        }
89
90
        return $res;
91
    }
92
93
    public function getLastMessage()
94
    {
95
        return $this->message;
96
    }
97
98
    public function deleteTimer($timerId = null)
99
    {
100
        $res = false;
101
102
        $url = 'https://' . $this->domain . '.harvestapp.com/daily/delete/' . $timerId;
103
104
        $base64Token = $this->apiToken;
105
106
        $headers = [
107
            "Content-type: application/json",
108
            "Accept: application/json",
109
            'Authorization: Basic ' . $base64Token,
110
        ];
111
112
        $ch = curl_init($url);
113
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
114
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
115
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
116
        $response = curl_exec($ch);
117
        $lastHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
118
        curl_close($ch);
119
120
        if ($response === false || $lastHttpCode !== 200) {
121
            $this->message = '- Could not delete Harvest timer!';
122
        } else {
123
            $this->message = '- Harvest timer deleted';
124
            $res = true;
125
        }
126
127
        return $res;
128
    }
129
130
    private function isTimerRunning($timerId)
131
    {
132
        $res = false;
133
134
        $url = 'https://' . $this->domain . '.harvestapp.com/daily/show/' . $timerId;
135
136
        $base64Token = $this->apiToken;
137
138
        $headers = [
139
            "Content-type: application/json",
140
            "Accept: application/json",
141
            'Authorization: Basic ' . $base64Token,
142
        ];
143
144
        $ch = curl_init($url);
145
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
146
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
147
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
148
        $response = curl_exec($ch);
149
        $lastHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
150
        curl_close($ch);
151
152
        if ($response !== false && $lastHttpCode === 200) {
153
            $data = json_decode($response, true);
154
            if (isset($data['timer_started_at']) === true) {
155
                $res = true;
156
            }
157
        }
158
159
        return $res;
160
    }
161
}
162