Completed
Push — master ( 4a9dc0...9a2704 )
by Guillaume
02:11
created

Harvest::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
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 = null;
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
            if ($this->sendApiCall('get', 'timer/' . $timerId) === true) {
59
                if ($this->lastApiCall('success') === true) {
60
                    $this->setMessage('timer stopped');
61
                    $res = true;
62
                } else {
63
                    $this->setMessage('could not stop timer!');
64
                }
65
            }
66
        } else {
67
            $this->setMessage('timer was not running');
68
        }
69
70
        return $res;
71
    }
72
73 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...
74
    {
75
        $res = false;
76
77
        if ($this->sendApiCall('delete', 'delete/' . $timerId) === true) {
78
            if ($this->lastApiCall('success') === true) {
79
                $this->setMessage('timer deleted');
80
                $res = true;
81
            } else {
82
                $this->setMessage('could not delete timer!');
83
            }
84
        }
85
86
        return $res;
87
    }
88
89
    private function isTimerRunning($timerId)
90
    {
91
        $res = false;
92
93
        if ($this->sendApiCall('get', 'show/' . $timerId) === true) {
94
            if ($this->lastApiCall('success') === true) {
95
                if (isset($this->data['timer_started_at']) === true) {
96
                    $res = true;
97
                }
98
            }
99
        }
100
101
        return $res;
102
    }
103
104
    public function getLastMessage()
105
    {
106
        return $this->message;
107
    }
108
109
    private function setMessage($message = null)
110
    {
111
        $this->message = '- Harvest: ' . $message;
112
    }
113
114 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...
115
    {
116
        $res = true;
117
118
        try {
119
            $response = $this->client->request(strtoupper($method), $uri, $options);
120
            $this->code = $response->getStatusCode();
121
            $this->data = json_decode($response->getBody(), true);
122
        } catch (ConnectException $e) {
123
            $this->setMessage('cannot connect to api!');
124
            $res = false;
125
        } catch (ClientException $e) {
126
            $this->setMessage($e->getResponse()->getBody());
127
        }
128
129
        return $res;
130
    }
131
132 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...
133
    {
134
        $res = false;
135
136
        switch ($status) {
137
            case 'success':
138
                if ($this->code === 200 || $this->code === 201) {
139
                    $res = true;
140
                }
141
                break;
142
        }
143
144
        return $res;
145
    }
146
}
147