Passed
Pull Request — master (#30)
by Guillaume
34:24 queued 30:24
created

Workflow   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Test Coverage

Coverage 82.54%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 55
c 2
b 0
f 0
dl 0
loc 144
ccs 52
cts 63
cp 0.8254
rs 10
wmc 26

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A do() 0 13 3
A enableService() 0 3 1
A getMenuClassName() 0 3 2
A getDefaultConfig() 0 3 1
A getCurrentMenuClass() 0 12 3
A getConfig() 0 3 1
A disableService() 0 3 1
A serviceStatus() 0 11 2
A disableAllServices() 0 4 2
A services() 0 3 1
A notify() 0 11 2
A classExistsForService() 0 3 1
A serviceEnabled() 0 28 5
1
<?php
2
3
namespace Godbout\Alfred\Time;
4
5
use Godbout\Alfred\Time\Services\Everhour;
6
use Godbout\Alfred\Time\Services\Harvest;
7
use Godbout\Alfred\Time\Services\Toggl;
8
use Godbout\Alfred\Workflow\BaseWorkflow;
9
use Godbout\Alfred\Workflow\Config;
10
11
class Workflow extends BaseWorkflow
12
{
13
    const SERVICES = [
14
        'toggl',
15
        'harvest',
16
        'everhour',
17
        'clockify',
18
    ];
19
20
    private $config = null;
21
22
23 535
    protected function __construct()
24
    {
25 535
        $this->config = Config::ifEmptyStartWith(self::getDefaultConfig());
26
        
27 535
        parent::__construct();
28 535
    }
29
30 2
    public static function do()
31
    {
32 2
        $action = getenv('action');
33
34 2
        if ($timerId = getenv('timer_id')) {
35
            return Timer::$action($timerId);
36
        }
37
38 2
        if (method_exists(Timer::class, $action)) {
39 2
            return Timer::$action();
40
        }
41
42
        return true;
43
    }
44
45
    public static function notify($result = false)
46
    {
47
        $action = getenv('action');
48
49
        $service = ucfirst(self::serviceEnabled());
50
51
        if ($result === false) {
52
            return "Oops... $service cannot $action.";
53
        }
54
55
        return "$service $action!";
56
    }
57
58 535
    private static function getDefaultConfig()
59
    {
60 535
        return include __DIR__ . '/../config/default.php';
61
    }
62
63 535
    public static function getConfig()
64
    {
65 535
        return self::getInstance()->config;
66
    }
67
68 463
    public static function enableService($service = '')
69
    {
70 463
        return self::getInstance()->serviceStatus($service, true);
71
    }
72
73 28
    public static function disableService($service = '')
74
    {
75 28
        return self::getInstance()->serviceStatus($service, false);
76
    }
77
78 4
    public static function services()
79
    {
80 4
        return self::SERVICES;
81
    }
82
83 481
    protected function serviceStatus($service, $status = false)
84
    {
85 481
        self::getInstance()->disableAllServices();
86
87 481
        if (self::getInstance()->classExistsForService($service)) {
88 481
            Workflow::getConfig()->write("$service.is_active", $status);
89
90 481
            return true;
91
        }
92
93
        return false;
94
    }
95
96 481
    protected function classExistsForService($service = '')
97
    {
98 481
        return class_exists(__NAMESPACE__ . '\\Services\\' . ucfirst($service));
99
    }
100
101 78
    public static function serviceEnabled()
102
    {
103 78
        if (self::getInstance()->getConfig()->read('toggl.is_active')) {
104 70
            return new Toggl(
105 70
                Workflow::getConfig()->read('toggl.api_token')
106
            );
107
        }
108
109 14
        if (self::getInstance()->getConfig()->read('harvest.is_active')) {
110 4
            return new Harvest(
111 4
                Workflow::getConfig()->read('harvest.account_id'),
112 4
                Workflow::getConfig()->read('harvest.api_token')
113
            );
114
        }
115
116 10
        if (self::getInstance()->getConfig()->read('everhour.is_active')) {
117 2
            return new Everhour(
118 2
                Workflow::getConfig()->read('everhour.api_token')
119
            );
120
        }
121
122 8
        if (self::getInstance()->getConfig()->read('clockify.is_active')) {
123
            return new Clockify(
0 ignored issues
show
Bug introduced by
The type Godbout\Alfred\Time\Clockify was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
124
                Workflow::getConfig()->read('clockify.api_token')
125
            );
126
        }
127
128 8
        return null;
129
    }
130
131 535
    public static function disableAllServices()
132
    {
133 535
        foreach (self::SERVICES as $service) {
134 535
            Workflow::getConfig()->write("$service.is_active", false);
135
        }
136 535
    }
137
138 130
    protected static function getCurrentMenuClass()
139
    {
140 130
        $args = explode('_', getenv('next'));
141
142 130
        if (in_array($args[0], self::SERVICES)) {
143 98
            $service = ucfirst($args[0]);
144 98
            $action = substr(getenv('next'), strlen($args[0]));
145
146 98
            return __NAMESPACE__ . "\\Menus\\$service\\" . self::getMenuClassName($action);
147
        }
148
149 32
        return __NAMESPACE__ . "\\Menus\\" . (self::getMenuClassName(getenv('next')) ?: 'Entrance');
150
    }
151
152 130
    private static function getMenuClassName($action)
153
    {
154 130
        return str_replace('_', '', ucwords($action === false ? 'entrance' : $action, '_'));
155
    }
156
}
157