Completed
Push — master ( f21ee5...8a9213 )
by Guillaume
02:10
created

Harvest::startTimer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 6
Ratio 28.57 %

Importance

Changes 0
Metric Value
dl 6
loc 21
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 3
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
    public function deleteTimer($timerId = null)
43
    {
44
        $res = $this->timerAction('delete', 'delete/' . $timerId);
45
46
        if ($res === true) {
47
            $this->setMessage('timer deleted');
48
        } else {
49
            $this->setMessage('could not delete timer! [' . $this->message . ']');
50
        }
51
52
        return $res;
53
    }
54
55
    /**
56
     * @return mixed
57
     */
58
    public function getLastMessage()
59
    {
60
        return $this->message;
61
    }
62
63
    /**
64
     * @param  $description
65
     * @param  $projectId
66
     * @param  $taskId
67
     * @return mixed
68
     */
69
    public function startTimer($description, $projectId, $taskId)
70
    {
71
        $harvestId = null;
72
73
        $item = [
74
            'notes'      => $description,
75
            'project_id' => $projectId,
76
            'task_id'    => $taskId,
77
        ];
78
79
        $data = $this->timerAction('start', 'add', ['json' => $item]);
80
81 View Code Duplication
        if (isset($data['id']) === 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...
82
            $this->setMessage('timer started');
83
            $harvestId = $data['id'];
84
        } else {
85
            $this->setMessage('could not start timer! [' . $this->message . ']');
86
        }
87
88
        return $harvestId;
89
    }
90
91
    /**
92
     * @param  $timerId
93
     * @return mixed
94
     */
95
    public function stopTimer($timerId = null)
96
    {
97
        $res = false;
98
99
        if ($this->isTimerRunning($timerId) === true) {
100
            $res = $this->timerAction('stop', 'timer/' . $timerId);
101
102
            if ($res === true) {
103
                $this->setMessage('timer stopped');
104
            } else {
105
                $this->setMessage('could not stop timer! [' . $this->message . ']');
106
            }
107
        } else {
108
            $this->setMessage('timer was not running');
109
        }
110
111
        return $res;
112
    }
113
114
    /**
115
     * @param  $timerId
116
     * @return mixed
117
     */
118
    private function isTimerRunning($timerId)
119
    {
120
        $data = $this->timerAction('timer_running', 'show/' . $timerId);
121
        $res = isset($data['timer_started_at']);
122
123
        return $res;
124
    }
125
126
    /**
127
     * @param $message
128
     */
129
    private function setMessage($message = null)
130
    {
131
        $this->message = '- Harvest: ' . $message;
132
    }
133
134
    /**
135
     * @param  string  $action
136
     * @param  string  $apiUri
137
     * @return mixed
138
     */
139
    private function timerAction($action, $apiUri, array $options = [])
140
    {
141
        $res = false;
142
        $returnDataFor = ['start', 'timer_running'];
143
        $methods = [
144
            'start'         => 'post',
145
            'stop'          => 'get',
146
            'delete'        => 'delete',
147
            'timer_running' => 'get',
148
        ];
149
        $method = isset($methods[$action]) ? $methods[$action] : '';
150
151 View Code Duplication
        if ($this->serviceApiCall->send($method, $apiUri, $options) === 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...
152
            $res = $this->serviceApiCall->last('success');
153
154
            if (in_array($action, $returnDataFor) === true) {
155
                $res = $this->serviceApiCall->getData();
156
            }
157
        } else {
158
            $this->message = $this->serviceApiCall->getMessage();
159
        }
160
161
        return $res;
162
    }
163
}
164