Completed
Push — master ( e10f18...83f77e )
by Guillaume
02:30
created

Timer::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 View Code Duplication
    public function __construct(Config $config = 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...
30
    {
31
        $this->config = $config;
32
        $this->harvest = new Harvest(
33
            $this->config->get('harvest', 'domain'),
34
            $this->config->get('harvest', 'api_token')
35
        );
36
        $this->toggl = new Toggl($this->config->get('toggl', 'api_token'));
37
    }
38
39
    /**
40
     * @param  $timerId
41
     * @return string
42
     */
43
    public function delete(array $timerData = [])
44
    {
45
        $res = [];
46
        $oneTimerDeleted = false;
47
48
        foreach ($timerData as $service => $id) {
49
            $res[$service] = $this->deleteServiceTimer($service, $id);
50
            $oneTimerDeleted = $oneTimerDeleted || $res;
0 ignored issues
show
Bug Best Practice introduced by
The expression $res of type array<*,boolean> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
51
        }
52
53
        if ($oneTimerDeleted === true) {
54
            $this->updateProperty('is_running', false);
55
        }
56
57
        return $res;
58
    }
59
60
    /**
61
     * @param  $service
62
     * @param  $timerId
63
     * @return boolean
64
     */
65
    public function deleteServiceTimer($service, $timerId)
66
    {
67
        if ($this->$service->deleteTimer($timerId) === false) {
68
            return false;
69
        }
70
71
        if ($timerId === $this->getProperty($service . '_id')) {
72
            $this->updateProperty($service . '_id', null);
73
        }
74
75
        return true;
76
    }
77
78
    /**
79
     * @return mixed
80
     */
81
    public function getDescription()
82
    {
83
        return $this->getProperty('description');
84
    }
85
86
    /**
87
     * @return mixed
88
     */
89
    public function getPrimaryService()
90
    {
91
        return $this->getProperty('primary_service');
92
    }
93
94
    /**
95
     * @return mixed
96
     */
97
    public function isRunning()
98
    {
99
        return $this->getProperty('is_running');
100
    }
101
102
    /**
103
     * @param  $description
104
     * @param  $projectsData
105
     * @param  $tagData
106
     * @return string
107
     */
108
    public function start($description = '', array $projectData = [], array $tagData = [], $specificService = null)
109
    {
110
        $res = [];
111
        $oneServiceStarted = false;
112
113
        $servicesToRun = ($specificService === null)
114
            ? $this->config->implementedServicesForFeature('start')
115
            : [$specificService];
116
117
        /**
118
         * When starting a new timer, all the services timer IDs have to be put to null
119
         * so that when the user uses the UNDO feature, it doesn't delete old previous
120
         * other services timers. The timer IDs are used for the UNDO feature and
121
         * should then contain the IDs of the last starts through the workflow, not
122
         * through each individual sefrvice
123
         */
124
        if (empty($servicesToRun) === true) {
125
            return '';
126
        }
127
128
        foreach ($this->config->activatedServices() as $service) {
129
            $this->updateProperty($service . '_id', null);
130
        }
131
132
        foreach ($servicesToRun as $service) {
133
            $timerId = $this->$service->startTimer(
134
                $description,
135
                $projectData[$service . '_id'],
136
                $tagData[$service . '_id']
137
            );
138
            $this->updateProperty($service . '_id', $timerId);
139
            $res[$service] = $timerId;
140
            $oneServiceStarted = $oneServiceStarted || ($timerId !== null);
141
        }
142
143
        if ($oneServiceStarted === true) {
144
            $this->updateProperty('description', $description);
145
            $this->updateProperty('is_running', true);
146
        }
147
148
        return $res;
149
    }
150
151
    /**
152
     * @return string
153
     */
154
    public function stop()
155
    {
156
        $res = [];
157
        $oneServiceStopped = false;
158
159
        foreach ($this->config->runningServices() as $service) {
160
            $timerId = $this->getProperty($service . '_id');
161
            $res[$service] = $this->$service->stopTimer($timerId);
162
            $oneServiceStopped = $oneServiceStopped || $res;
0 ignored issues
show
Bug Best Practice introduced by
The expression $res of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
163
        }
164
165
        if ($oneServiceStopped === true) {
166
            $this->updateProperty('is_running', false);
167
        }
168
169
        return $res;
170
    }
171
172
    /**
173
     * @return string
174
     */
175
    public function undo()
176
    {
177
        $timerData = [];
178
179
        foreach ($this->config->runningServices() as $service) {
180
            $timerData[$service] = $this->getProperty($service . '_id');
181
        }
182
183
        return $this->delete($timerData);
184
    }
185
186
    /**
187
     * @param $name
188
     * @param $value
189
     */
190
    public function updateProperty($name, $value)
191
    {
192
        $this->config->update('timer', $name, $value);
193
    }
194
195
    /**
196
     * @param  $name
197
     * @return mixed
198
     */
199
    private function getProperty($name)
200
    {
201
        return $this->config->get('timer', $name);
202
    }
203
}
204