Completed
Push — master ( 945b79...1bf282 )
by Guillaume
03:07 queued 52s
created

Harvest::deleteTimer()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

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 11
nc 3
nop 1
1
<?php
2
3
namespace AlfredTime;
4
5
use AlfredTime\ServiceApiCall;
6
7
/**
8
 *
9
 */
10
class Harvest
11
{
12
    /**
13
     * @var string
14
     */
15
    private $message = '';
16
17
    /**
18
     * @var mixed
19
     */
20
    private $serviceApiCall = null;
21
22
    /**
23
     * @param $domain
24
     * @param null      $apiToken
25
     */
26 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...
27
    {
28
        $this->serviceApiCall = new serviceApiCall([
29
            'base_uri' => 'https://' . $domain . '.harvestapp.com/daily/',
30
            'headers'  => [
31
                'Content-type'  => 'application/json',
32
                'Accept'        => 'application/json',
33
                'Authorization' => 'Basic ' . $apiToken,
34
            ],
35
        ]);
36
    }
37
38
    /**
39
     * @param  $timerId
40
     * @return mixed
41
     */
42 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...
43
    {
44
        $res = false;
45
46
        if ($this->serviceApiCall->send('delete', 'delete/' . $timerId) === true) {
47
            if ($this->serviceApiCall->last('success') === true) {
48
                $this->setMessage('timer deleted');
49
                $res = true;
50
            } else {
51
                $this->setMessage('could not delete timer!');
52
            }
53
        } else {
54
            $this->setMessage($this->serviceApiCall->getMessage());
55
        }
56
57
        return $res;
58
    }
59
60
    /**
61
     * @return mixed
62
     */
63
    public function getLastMessage()
64
    {
65
        return $this->message;
66
    }
67
68
    /**
69
     * @param  $description
70
     * @param  $projectId
71
     * @param  $taskId
72
     * @return mixed
73
     */
74
    public function startTimer($description, $projectId, $taskId)
75
    {
76
        $harvestId = null;
77
78
        $item = [
79
            'notes'      => $description,
80
            'project_id' => $projectId,
81
            'task_id'    => $taskId,
82
        ];
83
84 View Code Duplication
        if ($this->serviceApiCall->send('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...
85
            if ($this->serviceApiCall->last('success') === true) {
86
                $this->setMessage('timer started');
87
                $harvestId = $this->serviceApiCall->getData()['id'];
88
            } else {
89
                $this->setMessage('cannot start timer!');
90
            }
91
        } else {
92
            $this->setMessage($this->serviceApiCall->getMessage());
93
        }
94
95
        return $harvestId;
96
    }
97
98
    /**
99
     * @param  $timerId
100
     * @return mixed
101
     */
102
    public function stopTimer($timerId = null)
103
    {
104
        $res = false;
105
106
        if ($this->isTimerRunning($timerId) === true) {
107
            if ($this->serviceApiCall->send('get', 'timer/' . $timerId) === true) {
108
                if ($this->serviceApiCall->last('success') === true) {
109
                    $this->setMessage('timer stopped');
110
                    $res = true;
111
                } else {
112
                    $this->setMessage('could not stop timer!');
113
                }
114
            } else {
115
                $this->setMessage($this->serviceApiCall->getMessage());
116
            }
117
        } else {
118
            $this->setMessage('timer was not running');
119
        }
120
121
        return $res;
122
    }
123
124
    /**
125
     * @param  $timerId
126
     * @return mixed
127
     */
128 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...
129
    {
130
        $res = false;
131
132
        if ($this->serviceApiCall->send('get', 'show/' . $timerId) === true) {
133
            if ($this->serviceApiCall->last('success') === true) {
134
                if (isset($this->serviceApiCall->getData()['timer_started_at']) === true) {
135
                    $res = true;
136
                }
137
            }
138
        } else {
139
            $this->setMessage($this->serviceApiCall->getMessage());
140
        }
141
142
        return $res;
143
    }
144
145
    /**
146
     * @param $message
147
     */
148
    private function setMessage($message = null)
149
    {
150
        $this->message = '- Harvest: ' . $message;
151
    }
152
}
153