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

WorkflowHandler::getNotification()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 2
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace AlfredTime;
4
5
use AlfredTime\Config;
6
7
class WorkflowHandler
8
{
9
    /**
10
     * @var mixed
11
     */
12
    private $config;
13
14
    /**
15
     * @var mixed
16
     */
17
    private $harvest;
18
19
    /**
20
     * @var mixed
21
     */
22
    private $toggl;
23
24
    /**
25
     * @param Config $config
26
     */
27 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...
28
    {
29
        $this->config = $config;
30
        $this->harvest = new Harvest(
31
            $this->config->get('harvest', 'domain'),
32
            $this->config->get('harvest', 'api_token')
33
        );
34
        $this->toggl = new Toggl($this->config->get('toggl', 'api_token'));
35
    }
36
37
    /**
38
     * @param  $projectId
39
     * @return mixed
40
     */
41
    public function getProjectName($projectId)
42
    {
43
        $projectName = '';
44
45
        $projects = $this->getProjects();
46
47
        foreach ($projects as $project) {
48
            if ($project['id'] === $projectId) {
49
                $projectName = $project['name'];
50
                break;
51
            }
52
        }
53
54
        return $projectName;
55
    }
56
57
    /**
58
     * @return mixed
59
     */
60
    public function getProjects()
61
    {
62
        return $this->getItems('projects');
63
    }
64
65
    /**
66
     * @return mixed
67
     */
68 View Code Duplication
    public function getRecentTimers()
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...
69
    {
70
        $timers = [];
71
72
        foreach ($this->config->implementedServicesForFeature('get_timers') as $service) {
73
            if ($this->config->isServiceActive($service) === true) {
74
                $timers = array_merge($timers, $this->getRecentServiceTimers($service));
75
            }
76
        }
77
78
        return $timers;
79
    }
80
81
    /**
82
     * @param  $service
83
     * @return mixed
84
     */
85
    public function getServiceDataCache($service)
86
    {
87
        $cacheFile = getenv('alfred_workflow_data') . '/' . $service . '_cache.json';
88
89
        if (file_exists($cacheFile) === false) {
90
            $this->syncServiceOnlineDataToLocalCache($service);
91
        }
92
93
        return json_decode(file_get_contents($cacheFile), true);
94
    }
95
96
    /**
97
     * @return mixed
98
     */
99
    public function getTags()
100
    {
101
        return $this->getItems('tags');
102
    }
103
104
    /**
105
     * @param $service
106
     * @param null       $action
107
     * @param null       $success
0 ignored issues
show
Bug introduced by
There is no parameter named $success. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
108
     */
109
    public function getNotification(array $results = [], $action = null)
110
    {
111
        $notification = '';
112
113
        if (empty($results) || empty($action)) {
114
            return;
115
        }
116
117
        foreach ($results as $service => $status) {
118
            $notification .= $this->getNotificationForService($service, $action, $status);
119
        }
120
121
        return $notification;
122
    }
123
124
    /**
125
     * @param $service
126
     * @param null       $action
127
     * @param null       $success
128
     */
129
    public function getNotificationForService($service = null, $action = null, $success = null)
130
    {
131
        if (empty($success) === true) {
132
            return '- ' . ucfirst($service) . ': cannot ' . $action
133
            . ' [' . $this->$service->getLastMessage() . ']'
134
                . "\r\n";
135
        }
136
137
        return '- ' . ucfirst($service) . ': ' . $action . "\r\n";
138
    }
139
140
    /**
141
     * @return mixed
142
     */
143 View Code Duplication
    public function syncOnlineDataToLocalCache()
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...
144
    {
145
        $message = '';
146
147
        foreach ($this->config->implementedServicesForFeature('sync_data') as $service) {
148
            if ($this->config->isServiceActive($service) === true) {
149
                $message .= $this->syncServiceOnlineDataToLocalCache($service);
150
            }
151
        }
152
153
        return $message;
154
    }
155
156
    /**
157
     * @param  $needle
158
     * @return mixed
159
     */
160
    private function getItems($needle)
161
    {
162
        $items = [];
163
        $services = [];
164
165
        foreach ($this->config->implementedServicesForFeature('get_' . $needle) as $service) {
166
            if ($this->config->isServiceActive($service) === true) {
167
                $services[$service] = call_user_func_array(
168
                    [$this->$service, 'get' . ucfirst($needle)],
169
                    [$this->getServiceDataCache($service)]
170
                );
171
            }
172
        }
173
174
        foreach ($services as $serviceName => $serviceItems) {
175
            foreach ($serviceItems as $serviceItem) {
176
                $items[$serviceItem['name']][$serviceName . '_id'] = $serviceItem['id'];
177
            }
178
        }
179
180
        return $items;
181
    }
182
183
    /**
184
     * @return mixed
185
     */
186
    private function getRecentServiceTimers($service)
187
    {
188
        return $this->$service->getRecentTimers();
189
    }
190
191
    /**
192
     * @param $data
193
     * @param string  $service
194
     */
195
    private function saveServiceDataCache($service, $data)
196
    {
197
        $cacheFile = getenv('alfred_workflow_data') . '/' . $service . '_cache.json';
198
        file_put_contents($cacheFile, json_encode($data));
199
    }
200
201
    /**
202
     * @param  $service
203
     * @return mixed
204
     */
205
    private function syncServiceOnlineDataToLocalCache($service)
206
    {
207
        $data = $this->$service->getOnlineData();
208
209
        if (empty($data) === true) {
210
            return $this->getNotificationForService($service, 'data', false);
211
        }
212
213
        $this->saveServiceDataCache($service, $data);
214
215
        return $this->getNotificationForService($service, 'data', true);
216
    }
217
}
218