Completed
Push — master ( 0a8060...8cca57 )
by Guillaume
02:13
created

Timer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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