Passed
Push — clockify ( 42cad4...653eb0 )
by Guillaume
19:35
created

Workflow::serviceEnabled()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.0592

Importance

Changes 0
Metric Value
cc 5
eloc 14
c 0
b 0
f 0
nc 5
nop 0
dl 0
loc 28
ccs 13
cts 15
cp 0.8667
crap 5.0592
rs 9.4888
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 920
    protected function __construct()
24
    {
25 920
        $this->config = Config::ifEmptyStartWith(self::getDefaultConfig());
26
        
27 920
        parent::__construct();
28 920
    }
29
30 11
    public static function do()
31
    {
32 11
        $action = getenv('action');
33
34 11
        if ($timerId = getenv('timer_id')) {
35
            return Timer::$action($timerId);
36
        }
37
38 11
        if (method_exists(Timer::class, $action)) {
39 11
            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 920
    private static function getDefaultConfig()
59
    {
60 920
        return include __DIR__ . '/../config/default.php';
61
    }
62
63 920
    public static function getConfig()
64
    {
65 920
        return self::getInstance()->config;
66
    }
67
68 524
    public static function enableService($service = '')
69
    {
70 524
        return self::getInstance()->serviceStatus($service, true);
71
    }
72
73 154
    public static function disableService($service = '')
74
    {
75 154
        return self::getInstance()->serviceStatus($service, false);
76
    }
77
78 22
    public static function services()
79
    {
80 22
        return self::SERVICES;
81
    }
82
83 623
    protected function serviceStatus($service, $status = false)
84
    {
85 623
        self::getInstance()->disableAllServices();
86
87 623
        if (self::getInstance()->classExistsForService($service)) {
88 623
            Workflow::getConfig()->write("$service.is_active", $status);
89
90 623
            return true;
91
        }
92
93
        return false;
94
    }
95
96 623
    protected function classExistsForService($service = '')
97
    {
98 623
        return class_exists(__NAMESPACE__ . '\\Services\\' . ucfirst($service));
99
    }
100
101 159
    public static function serviceEnabled()
102
    {
103 159
        if (self::getInstance()->getConfig()->read('toggl.is_active')) {
104 115
            return new Toggl(
105 115
                Workflow::getConfig()->read('toggl.api_token')
106
            );
107
        }
108
109 77
        if (self::getInstance()->getConfig()->read('harvest.is_active')) {
110 22
            return new Harvest(
111 22
                Workflow::getConfig()->read('harvest.account_id'),
112 22
                Workflow::getConfig()->read('harvest.api_token')
113
            );
114
        }
115
116 55
        if (self::getInstance()->getConfig()->read('everhour.is_active')) {
117 11
            return new Everhour(
118 11
                Workflow::getConfig()->read('everhour.api_token')
119
            );
120
        }
121
122 44
        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 44
        return null;
129
    }
130
131 920
    public static function disableAllServices()
132
    {
133 920
        foreach (self::SERVICES as $service) {
134 920
            Workflow::getConfig()->write("$service.is_active", false);
135
        }
136 920
    }
137
138 661
    protected static function getCurrentMenuClass()
139
    {
140 661
        $args = explode('_', getenv('next'));
141
142 661
        if (in_array($args[0], self::SERVICES)) {
143 539
            $service = ucfirst($args[0]);
144 539
            $action = substr(getenv('next'), strlen($args[0]));
145
146 539
            return __NAMESPACE__ . "\\Menus\\$service\\" . self::getMenuClassName($action);
147
        }
148
149 122
        return __NAMESPACE__ . "\\Menus\\" . (self::getMenuClassName(getenv('next')) ?: 'Entrance');
150
    }
151
152 661
    private static function getMenuClassName($action)
153
    {
154 661
        return str_replace('_', '', ucwords($action === false ? 'entrance' : $action, '_'));
155
    }
156
}
157