Completed
Push — master ( 9a2704...923116 )
by Guillaume
02:03
created

Harvest::sendApiCall()   A

Complexity

Conditions 3
Paths 7

Size

Total Lines 17
Code Lines 12

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 7
nop 3
1
<?php
2
3
require_once 'serviceApiCall.class.php';
4
5
/**
6
 *
7
 */
8
class Harvest
9
{
10
    private $serviceApiCall = null;
11
    private $message = '';
12
13 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...
14
    {
15
        $this->serviceApiCall = new serviceApiCall([
16
            'base_uri' => 'https://' . $domain . '.harvestapp.com/daily/',
17
            'headers' => [
18
                'Content-type' => 'application/json',
19
                'Accept' => 'application/json',
20
                'Authorization' => 'Basic ' . $apiToken,
21
            ],
22
        ]);
23
    }
24
25
    public function startTimer($description, $projectId, $taskId)
26
    {
27
        $harvestId = null;
28
29
        $item = [
30
            'notes' => $description,
31
            'project_id' => $projectId,
32
            'task_id' => $taskId,
33
        ];
34
35
        if ($this->serviceApiCall->send('post', 'add', ['json' => $item]) === true) {
36
            if ($this->serviceApiCall->last('success') === true) {
37
                $this->setMessage('timer started');
38
                $harvestId = $this->serviceApiCall->getData()['id'];
39
            } else {
40
                $this->setMessage('cannot start timer!');
41
            }
42
        } else {
43
            $this->setMessage($this->serviceApiCall->getMessage());
44
        }
45
46
        return $harvestId;
47
    }
48
49
    public function stopTimer($timerId = null)
50
    {
51
        $res = false;
52
53
        if ($this->isTimerRunning($timerId) === true) {
54
            if ($this->serviceApiCall->send('get', 'timer/' . $timerId) === true) {
55
                if ($this->serviceApiCall->last('success') === true) {
56
                    $this->setMessage('timer stopped');
57
                    $res = true;
58
                } else {
59
                    $this->setMessage('could not stop timer!');
60
                }
61
            } else {
62
                $this->setMessage($this->serviceApiCall->getMessage());
63
            }
64
        } else {
65
            $this->setMessage('timer was not running');
66
        }
67
68
        return $res;
69
    }
70
71 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...
72
    {
73
        $res = false;
74
75
        if ($this->serviceApiCall->send('delete', 'delete/' . $timerId) === true) {
76
            if ($this->serviceApiCall->last('success') === true) {
77
                $this->setMessage('timer deleted');
78
                $res = true;
79
            } else {
80
                $this->setMessage('could not delete timer!');
81
            }
82
        } else {
83
            $this->setMessage($this->serviceApiCall->getMessage());
84
        }
85
86
        return $res;
87
    }
88
89 View Code Duplication
    private function isTimerRunning($timerId)
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
        if ($this->serviceApiCall->send('get', 'show/' . $timerId) === true) {
94
            if ($this->serviceApiCall->last('success') === true) {
95
                if (isset($this->serviceApiCall->getData()['timer_started_at']) === true) {
96
                    $res = true;
97
                }
98
            }
99
        } else {
100
            $this->setMessage($this->serviceApiCall->getMessage());
101
        }
102
103
        return $res;
104
    }
105
106
    public function getLastMessage()
107
    {
108
        return $this->message;
109
    }
110
111
    private function setMessage($message = null)
112
    {
113
        $this->message = '- Harvest: ' . $message;
114
    }
115
116
}
117