Completed
Push — master ( 97bdbf...e1b2b8 )
by Guillaume
02:19
created

WorkflowHandler   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 128
Duplicated Lines 25 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
lcom 1
cbo 2
dl 32
loc 128
rs 10
c 1
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getProjectName() 0 15 3
A getRecentTimers() 12 12 3
A setNotificationForService() 8 8 2
A syncOnlineDataToLocalCache() 12 12 3
A getRecentServiceTimers() 0 4 1
A saveServiceDataCache() 0 5 1
A syncServiceOnlineDataToLocalCache() 0 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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();
0 ignored issues
show
Bug introduced by
The method getProjects() does not seem to exist on object<AlfredTime\WorkflowHandler>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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 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...
58
    {
59
        $timers = [];
60
61
        foreach ($this->config->implementedServicesForFeature('get_timers') as $service) {
62
            if ($this->config->isServiceActive($service) === true) {
63
                $timers = array_merge($timers, $this->getRecentServiceTimers($service));
64
            }
65
        }
66
67
        return $timers;
68
    }
69
70
    /**
71
     * @param $service
72
     * @param null       $action
73
     * @param null       $success
74
     */
75 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...
76
    {
77
        if (empty($success) === true) {
78
            return '- ' . ucfirst($service) . ': cannot ' . $action . ' [' . $this->$service->getLastMessage() . ']' . "\r\n";
79
        }
80
81
        return '- ' . ucfirst($service) . ': ' . $action . "\r\n";
82
    }
83
84
    /**
85
     * @return mixed
86
     */
87 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...
88
    {
89
        $message = '';
90
91
        foreach ($this->config->implementedServicesForFeature('sync_data') as $service) {
92
            if ($this->config->isServiceActive($service) === true) {
93
                $message .= $this->syncServiceOnlineDataToLocalCache($service);
94
            }
95
        }
96
97
        return $message;
98
    }
99
100
    /**
101
     * @return mixed
102
     */
103
    private function getRecentServiceTimers($service)
104
    {
105
        return $this->$service->getRecentTimers();
106
    }
107
108
    /**
109
     * @param $data
110
     * @param string  $service
111
     */
112
    private function saveServiceDataCache($service, $data)
113
    {
114
        $cacheFile = getenv('alfred_workflow_data') . '/' . $service . '_cache.json';
115
        file_put_contents($cacheFile, json_encode($data));
116
    }
117
118
    /**
119
     * @param  $service
120
     * @return mixed
121
     */
122
    private function syncServiceOnlineDataToLocalCache($service)
123
    {
124
        $data = $this->$service->getOnlineData();
125
126
        if (empty($data) === true) {
127
            return $this->setNotificationForService($service, 'data', false);
128
        }
129
130
        $this->saveServiceDataCache($service, $data);
131
132
        return $this->setNotificationForService($service, 'data', true);
133
    }
134
}
135