Completed
Push — master ( e9a21a...557ba8 )
by Guillaume
02:41
created

WorkflowHandler::getProjectName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 15
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
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  array     $results
39
     * @param  $action
40
     * @return mixed
41
     */
42
    public function getNotification(array $results = [], $action = null)
43
    {
44
        $notification = '';
45
46
        if (empty($results) || empty($action)) {
47
            return '';
48
        }
49
50
        foreach ($results as $service => $status) {
51
            $notification .= $this->getNotificationForService($service, $action, $status);
52
        }
53
54
        return $notification;
55
    }
56
57
    /**
58
     * @param $service
59
     * @param null       $action
60
     * @param null       $success
61
     */
62
    public function getNotificationForService($service = null, $action = null, $success = null)
63
    {
64
        if (empty($success) === true) {
65
            return '- ' . ucfirst($service) . ': cannot ' . $action . ' []' . "\r\n";
66
        }
67
68
        return '- ' . ucfirst($service) . ': ' . $action . "\r\n";
69
    }
70
71
    /**
72
     * @return mixed
73
     */
74
    public function getProjects()
75
    {
76
        return $this->getItems('projects');
77
    }
78
79
    /**
80
     * @return mixed
81
     */
82
    public function getRecentTimers()
83
    {
84
        $timers = [];
85
86
        foreach ($this->config->activatedServices() as $service) {
87
            $timers[$service] = $this->getRecentServiceTimers($service);
88
        }
89
90
        return $timers;
91
    }
92
93
    /**
94
     * @param  $service
95
     * @return mixed
96
     */
97
    public function getServiceDataCache($service)
98
    {
99
        $cacheFile = getenv('alfred_workflow_data') . '/' . $service . '_cache.json';
100
101
        if (file_exists($cacheFile) === false) {
102
            $this->syncServiceOnlineDataToLocalCache($service);
103
        }
104
105
        return json_decode(file_get_contents($cacheFile), true);
106
    }
107
108
    /**
109
     * @return mixed
110
     */
111
    public function getTags()
112
    {
113
        return $this->getItems('tags');
114
    }
115
116
    /**
117
     * @return mixed
118
     */
119
    public function syncOnlineDataToLocalCache()
120
    {
121
        $message = '';
122
123
        foreach ($this->config->activatedServices() as $service) {
124
            $message .= $this->syncServiceOnlineDataToLocalCache($service);
125
        }
126
127
        return $message;
128
    }
129
130
    /**
131
     * @param  $needle
132
     * @return mixed
133
     */
134
    private function getItems($needle)
135
    {
136
        $items = [];
137
        $services = [];
138
139
        foreach ($this->config->activatedServices() as $service) {
140
            $services[$service] = call_user_func_array(
141
                [$this->$service, 'get' . ucfirst($needle)],
142
                [$this->getServiceDataCache($service)]
143
            );
144
        }
145
146
        foreach ($services as $serviceName => $serviceItems) {
147
            foreach ($serviceItems as $serviceItem) {
148
                $items[$serviceItem['name']][$serviceName . '_id'] = $serviceItem['id'];
149
            }
150
        }
151
152
        return $items;
153
    }
154
155
    /**
156
     * @return mixed
157
     */
158
    private function getRecentServiceTimers($service)
159
    {
160
        return $this->$service->getRecentTimers();
161
    }
162
163
    /**
164
     * @param $data
165
     * @param string  $service
166
     */
167
    private function saveServiceDataCache($service, $data)
168
    {
169
        $cacheFile = getenv('alfred_workflow_data') . '/' . $service . '_cache.json';
170
        file_put_contents($cacheFile, json_encode($data));
171
    }
172
173
    /**
174
     * @param  $service
175
     * @return mixed
176
     */
177
    private function syncServiceOnlineDataToLocalCache($service)
178
    {
179
        $data = $this->$service->getOnlineData();
180
181
        if (empty($data) === true) {
182
            return $this->getNotificationForService($service, 'data', false);
183
        }
184
185
        $this->saveServiceDataCache($service, $data);
186
187
        return $this->getNotificationForService($service, 'data', true);
188
    }
189
}
190