Completed
Push — master ( 12f138...8f7193 )
by Guillaume
02:15
created

src/WorkflowHandler.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
     * This method shouldn't be in that class but rather
73
     * in the services classes, but those classes don't have
74
     * access to the cache, so, I'd rather sleep than creating
75
     * a new Cache class and refactoring everything again
76
     * @param $service
77
     * @param $projectId
78
     */
79
    public function getProjectName($service, $projectId)
80
    {
81
        return $this->$service->getProjectName($projectId, $this->getServiceDataCache($service));
82
    }
83
84
    /**
85
     * @return mixed
86
     */
87
    public function getProjects()
88
    {
89
        return $this->getItems('projects');
90
    }
91
92
    /**
93
     * @return mixed
94
     */
95
    public function getRecentTimers()
96
    {
97
        $timers = [];
98
99
        foreach ($this->config->activatedServices() as $service) {
100
            $timers[$service] = $this->getRecentServiceTimers($service);
101
        }
102
103
        return $timers;
104
    }
105
106
    /**
107
     * @param  $service
108
     * @return mixed
109
     */
110
    public function getServiceDataCache($service)
111
    {
112
        $cacheFile = getenv('alfred_workflow_data') . '/' . $service . '_cache.json';
113
114
        if (file_exists($cacheFile) === false) {
115
            $this->syncServiceOnlineDataToLocalCache($service);
116
        }
117
118
        return json_decode(file_get_contents($cacheFile), true);
119
    }
120
121
    /**
122
     * @return mixed
123
     */
124
    public function getTags()
125
    {
126
        return $this->getItems('tags');
127
    }
128
129
    /**
130
     * @return mixed
131
     */
132
    public function syncOnlineDataToLocalCache()
133
    {
134
        $message = '';
135
136
        foreach ($this->config->activatedServices() as $service) {
137
            $message .= $this->syncServiceOnlineDataToLocalCache($service);
138
        }
139
140
        return $message;
141
    }
142
143
    /**
144
     * @param  $needle
145
     * @return mixed
146
     */
147
    private function getItems($needle)
148
    {
149
        $items = [];
150
        $services = [];
151
152
        foreach ($this->config->activatedServices() as $service) {
153
            $services[$service] = call_user_func_array(
154
                [$this->$service, 'get' . ucfirst($needle)],
155
                [$this->getServiceDataCache($service)]
156
            );
157
        }
158
159
        foreach ($services as $serviceName => $serviceItems) {
160
            foreach ($serviceItems as $serviceItem) {
161
                $items[$serviceItem['name']][$serviceName] = $serviceItem['id'];
162
            }
163
        }
164
165
        return $items;
166
    }
167
168
    /**
169
     * @return mixed
170
     */
171
    private function getRecentServiceTimers($service)
172
    {
173
        return $this->$service->getRecentTimers();
174
    }
175
176
    /**
177
     * @param $data
178
     * @param string  $service
179
     */
180
    private function saveServiceDataCache($service, $data)
181
    {
182
        $cacheFile = getenv('alfred_workflow_data') . '/' . $service . '_cache.json';
183
        file_put_contents($cacheFile, json_encode($data));
184
    }
185
186
    /**
187
     * @param  $service
188
     * @return mixed
189
     */
190
    private function syncServiceOnlineDataToLocalCache($service)
191
    {
192
        $data = $this->$service->getOnlineData();
193
194
        if (empty($data) === true) {
195
            return $this->getNotificationForService($service, 'data', false);
196
        }
197
198
        $this->saveServiceDataCache($service, $data);
199
200
        return $this->getNotificationForService($service, 'data', true);
201
    }
202
}
203