Completed
Push — master ( 749838...dd43ff )
by Guillaume
02:53
created

Harvest::stopTimer()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 1
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
require __DIR__ . '/../vendor/autoload.php';
4
5
use GuzzleHttp\Client;
6
7
/**
8
 *
9
 */
10
class Harvest
11
{
12
    private $message;
13
    private $domain;
0 ignored issues
show
Unused Code introduced by
The property $domain is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
14
15 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...
16
    {
17
        $this->client = new Client([
0 ignored issues
show
Bug introduced by
The property client does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
18
            'base_uri' => 'https://' . $domain . '.harvestapp.com/daily/',
19
            'headers' => [
20
                'Content-type' => 'application/json',
21
                'Accept' => 'application/json',
22
                'Authorization' => 'Basic ' . $apiToken,
23
            ],
24
        ]);
25
        $this->message = '';
26
    }
27
28
    public function startTimer($description, $projectId, $taskId)
29
    {
30
        $harvestId = null;
31
32
        $item = [
33
            'notes' => $description,
34
            'project_id' => $projectId,
35
            'task_id' => $taskId,
36
        ];
37
38
        $response = $this->client->post('add', [
39
            'json' => $item,
40
        ]);
41
42 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...
43
            $this->message = '- Cannot start Harvest timer!';
44
        } else {
45
            $data = json_decode($response->getBody(), true);
46
            $harvestId = $data['id'];
47
            $this->message = '- Harvest timer started';
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
            $response = $this->client->get('timer/' . $timerId);
59
60
            if ($response->getStatusCode() !== 200) {
61
                $this->message = '- Could not stop Harvest timer!';
62
            } else {
63
                $this->message = '- Harvest timer stopped';
64
                $res = true;
65
            }
66
        } else {
67
            $this->message = '- Harvest timer was not running';
68
        }
69
70
        return $res;
71
    }
72
73
    public function getLastMessage()
74
    {
75
        return $this->message;
76
    }
77
78
    public function deleteTimer($timerId = null)
79
    {
80
        $res = false;
81
82
        $response = $this->client->delete('delete/' . $timerId);
83
84
        if ($response->getStatusCode() !== 200) {
85
            $this->message = '- Could not delete Harvest timer!';
86
        } else {
87
            $this->message = '- Harvest timer deleted';
88
            $res = true;
89
        }
90
91
        return $res;
92
    }
93
94
    private function isTimerRunning($timerId)
95
    {
96
        $res = false;
97
98
        $response = $this->client->get('show/' . $timerId);
99
100
        if ($response->getStatusCode() === 200) {
101
            $data = json_decode($response->getBody(), true);
102
            if (isset($data['timer_started_at']) === true) {
103
                $res = true;
104
            }
105
        }
106
107
        return $res;
108
    }
109
}
110