Completed
Push — master ( e1b2b8...0da715 )
by Guillaume
02:13
created

Timer::getItems()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 9
nop 1
1
<?php
2
3
namespace AlfredTime;
4
5
use AlfredTime\Toggl;
6
use AlfredTime\Config;
7
use AlfredTime\Harvest;
8
9
class Timer
10
{
11
    /**
12
     * @var mixed
13
     */
14
    private $config;
15
16
    /**
17
     * @var mixed
18
     */
19
    private $harvest;
20
21
    /**
22
     * @var mixed
23
     */
24
    private $toggl;
25
26
    /**
27
     * @param Config $config
28
     */
29
    public function __construct(Config $config = null)
30
    {
31
        $this->config = $config;
32
        $this->harvest = new Harvest($this->config->get('harvest', 'domain'), $this->config->get('harvest', 'api_token'));
33
        $this->toggl = new Toggl($this->config->get('toggl', 'api_token'));
34
    }
35
36
    /**
37
     * @param  $timerId
38
     * @return string
39
     */
40
    public function delete(array $timerData = [])
41
    {
42
        $message = '';
43
        $oneTimerDeleted = false;
44
45
        foreach ($timerData as $service => $id) {
46
            $res = $this->deleteServiceTimer($service, $id);
47
            $message .= $this->setNotificationForService($service, 'delete', $res);
48
            $oneTimerDeleted = $oneTimerDeleted || $res;
49
        }
50
51
        if ($oneTimerDeleted === true) {
52
            $this->updateProperty('is_running', false);
53
        }
54
55
        return $message;
56
    }
57
58
    /**
59
     * @param  $service
60
     * @param  $timerId
61
     * @return boolean
62
     */
63
    public function deleteServiceTimer($service, $timerId)
64
    {
65
        if ($this->$service->deleteTimer($timerId) === false) {
66
            return false;
67
        }
68
69
        if ($timerId === $this->getProperty($service . '_id')) {
70
            $this->updateProperty($service . '_id', null);
71
        }
72
73
        return true;
74
    }
75
76
    /**
77
     * @return mixed
78
     */
79
    public function getDescription()
80
    {
81
        return $this->getProperty('description');
82
    }
83
84
    /**
85
     * @return mixed
86
     */
87
    public function getPrimaryService()
88
    {
89
        return $this->getProperty('primary_service');
90
    }
91
92
    /**
93
     * @return mixed
94
     */
95
    public function isRunning()
96
    {
97
        return $this->getProperty('is_running');
98
    }
99
100
    /**
101
     * @param $service
102
     * @param null       $action
103
     * @param null       $success
104
     */
105 View Code Duplication
    public function setNotificationForService($service = null, $action = null, $success = 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...
106
    {
107
        if (empty($success) === true) {
108
            return '- ' . ucfirst($service) . ': cannot ' . $action . ' [' . $this->$service->getLastMessage() . ']' . "\r\n";
109
        }
110
111
        return '- ' . ucfirst($service) . ': ' . $action . "\r\n";
112
    }
113
114
    /**
115
     * @param  $description
116
     * @param  $projectsData
117
     * @param  $tagData
118
     * @return string
119
     */
120
    public function start($description = '', array $projectData = [], array $tagData = [], $specificService = null)
121
    {
122
        $message = '';
123
        $oneServiceStarted = false;
124
125
        $servicesToRun = ($specificService === null) ? $this->config->implementedServicesForFeature('start') : [$specificService];
126
127
        /**
128
         * When starting a new timer, all the services timer IDs have to be put to null
129
         * so that when the user uses the UNDO feature, it doesn't delete old previous
130
         * other services timers. The timer IDs are used for the UNDO feature and
131
         * should then contain the IDs of the last starts through the workflow, not
132
         * through each individual sefrvice
133
         */
134
        if (empty($servicesToRun) === true) {
135
            return '';
136
        }
137
138
        foreach ($this->config->activatedServices() as $service) {
139
            $this->updateProperty($service . '_id', null);
140
        }
141
142
        foreach ($servicesToRun as $service) {
143
            $timerId = $this->$service->startTimer($description, $projectData[$service . '_id'], $tagData[$service . '_id']);
144
            $this->updateProperty($service . '_id', $timerId);
145
            $message .= $this->setNotificationForService($service, 'start', $timerId);
146
            $oneServiceStarted = $oneServiceStarted || ($timerId !== null);
147
        }
148
149
        if ($oneServiceStarted === true) {
150
            $this->updateProperty('description', $description);
151
            $this->updateProperty('is_running', true);
152
        }
153
154
        return $message;
155
    }
156
157
    /**
158
     * @return string
159
     */
160
    public function stop()
161
    {
162
        $message = '';
163
        $oneServiceStopped = false;
164
165
        foreach ($this->config->runningServices() as $service) {
166
            $timerId = $this->getProperty($service . '_id');
167
168
            $res = $this->$service->stopTimer($timerId);
169
            $message .= $this->setNotificationForService($service, 'stop', $res);
170
            $oneServiceStopped = $oneServiceStopped || $res;
171
        }
172
173
        if ($oneServiceStopped === true) {
174
            $this->updateProperty('is_running', false);
175
        }
176
177
        return $message;
178
    }
179
180
    /**
181
     * @return string
182
     */
183
    public function undo()
184
    {
185
        $timerData = [];
186
187
        foreach ($this->config->runningServices() as $service) {
188
            $timerData[$service] = $this->getProperty($service . '_id');
189
        }
190
191
        return $this->delete($timerData);
192
    }
193
194
    /**
195
     * @param $name
196
     * @param $value
197
     */
198
    public function updateProperty($name, $value)
199
    {
200
        $this->config->update('timer', $name, $value);
201
    }
202
203
    /**
204
     * @param  $name
205
     * @return mixed
206
     */
207
    private function getProperty($name)
208
    {
209
        return $this->config->get('timer', $name);
210
    }
211
}
212