Completed
Push — master ( e1b2b8...0da715 )
by Guillaume
02:13
created

WorkflowHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 4
nc 1
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
    public function __construct(Config $config = null)
28
    {
29
        $this->config = $config;
30
        $this->harvest = new Harvest($this->config->get('harvest', 'domain'), $this->config->get('harvest', 'api_token'));
31
        $this->toggl = new Toggl($this->config->get('toggl', 'api_token'));
32
    }
33
34
    /**
35
     * @param  $projectId
36
     * @return mixed
37
     */
38
    public function getProjectName($projectId)
39
    {
40
        $projectName = '';
41
42
        $projects = $this->getProjects();
43
44
        foreach ($projects as $project) {
45
            if ($project['id'] === $projectId) {
46
                $projectName = $project['name'];
47
                break;
48
            }
49
        }
50
51
        return $projectName;
52
    }
53
54
    /**
55
     * @return mixed
56
     */
57
    public function getProjects()
58
    {
59
        return $this->getItems('projects');
60
    }
61
62
    /**
63
     * @return mixed
64
     */
65 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...
66
    {
67
        $timers = [];
68
69
        foreach ($this->config->implementedServicesForFeature('get_timers') as $service) {
70
            if ($this->config->isServiceActive($service) === true) {
71
                $timers = array_merge($timers, $this->getRecentServiceTimers($service));
72
            }
73
        }
74
75
        return $timers;
76
    }
77
78
    /**
79
     * @param  $service
80
     * @return mixed
81
     */
82
    public function getServiceDataCache($service)
83
    {
84
        $cacheFile = getenv('alfred_workflow_data') . '/' . $service . '_cache.json';
85
86
        if (file_exists($cacheFile) === false) {
87
            $this->syncServiceOnlineDataToLocalCache($service);
88
        }
89
90
        return json_decode(file_get_contents($cacheFile), true);
91
    }
92
93
    /**
94
     * @return mixed
95
     */
96
    public function getTags()
97
    {
98
        return $this->getItems('tags');
99
    }
100
101
    /**
102
     * @param $service
103
     * @param null       $action
104
     * @param null       $success
105
     */
106 View Code Duplication
    public function setNotificationForService($service = null, $action = null, $success = 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...
107
    {
108
        if (empty($success) === true) {
109
            return '- ' . ucfirst($service) . ': cannot ' . $action . ' [' . $this->$service->getLastMessage() . ']' . "\r\n";
110
        }
111
112
        return '- ' . ucfirst($service) . ': ' . $action . "\r\n";
113
    }
114
115
    /**
116
     * @return mixed
117
     */
118 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...
119
    {
120
        $message = '';
121
122
        foreach ($this->config->implementedServicesForFeature('sync_data') as $service) {
123
            if ($this->config->isServiceActive($service) === true) {
124
                $message .= $this->syncServiceOnlineDataToLocalCache($service);
125
            }
126
        }
127
128
        return $message;
129
    }
130
131
    /**
132
     * @param  $needle
133
     * @return mixed
134
     */
135
    private function getItems($needle)
136
    {
137
        $items = [];
138
        $services = [];
139
140
        foreach ($this->config->implementedServicesForFeature('get_' . $needle) as $service) {
141
            if ($this->config->isServiceActive($service) === true) {
142
                $services[$service] = call_user_func_array([$this->$service, 'get' . ucfirst($needle)], [$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->setNotificationForService($service, 'data', false);
183
        }
184
185
        $this->saveServiceDataCache($service, $data);
186
187
        return $this->setNotificationForService($service, 'data', true);
188
    }
189
}
190