Passed
Pull Request — master (#30)
by Guillaume
36:15 queued 34:05
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\Clockify;
6
use Godbout\Alfred\Time\Services\Everhour;
7
use Godbout\Alfred\Time\Services\Harvest;
8
use Godbout\Alfred\Time\Services\Toggl;
9
use Godbout\Alfred\Workflow\BaseWorkflow;
10
use Godbout\Alfred\Workflow\Config;
11
12
class Workflow extends BaseWorkflow
13
{
14
    const SERVICES = [
15
        'toggl',
16
        'harvest',
17
        'everhour',
18
        'clockify',
19
    ];
20
21
    private $config = null;
22
23
24 906
    protected function __construct()
25
    {
26 906
        $this->config = Config::ifEmptyStartWith(self::getDefaultConfig());
27
        
28 906
        parent::__construct();
29 906
    }
30
31 10
    public static function do()
32
    {
33 10
        $action = getenv('action');
34
35 10
        if ($timerId = getenv('timer_id')) {
36
            return Timer::$action($timerId);
37
        }
38
39 10
        if (method_exists(Timer::class, $action)) {
40 10
            return Timer::$action();
41
        }
42
43
        return true;
44
    }
45
46
    public static function notify($result = false)
47
    {
48
        $action = getenv('action');
49
50
        $service = ucfirst(self::serviceEnabled());
51
52
        if ($result === false) {
53
            return "Oops... $service cannot $action.";
54
        }
55
56
        return "$service $action!";
57
    }
58
59 906
    private static function getDefaultConfig()
60
    {
61 906
        return include __DIR__ . '/../config/default.php';
62
    }
63
64 906
    public static function getConfig()
65
    {
66 906
        return self::getInstance()->config;
67
    }
68
69 546
    public static function enableService($service = '')
70
    {
71 546
        return self::getInstance()->serviceStatus($service, true);
72
    }
73
74 140
    public static function disableService($service = '')
75
    {
76 140
        return self::getInstance()->serviceStatus($service, false);
77
    }
78
79 20
    public static function services()
80
    {
81 20
        return self::SERVICES;
82
    }
83
84 636
    protected function serviceStatus($service, $status = false)
85
    {
86 636
        self::getInstance()->disableAllServices();
87
88 636
        if (self::getInstance()->classExistsForService($service)) {
89 636
            Workflow::getConfig()->write("$service.is_active", $status);
90
91 636
            return true;
92
        }
93
94
        return false;
95
    }
96
97 636
    protected function classExistsForService($service = '')
98
    {
99 636
        return class_exists(__NAMESPACE__ . '\\Services\\' . ucfirst($service));
100
    }
101
102 150
    public static function serviceEnabled()
103
    {
104 150
        if (self::getInstance()->getConfig()->read('toggl.is_active')) {
105 110
            return new Toggl(
106 110
                Workflow::getConfig()->read('toggl.api_token')
107
            );
108
        }
109
110 70
        if (self::getInstance()->getConfig()->read('harvest.is_active')) {
111 20
            return new Harvest(
112 20
                Workflow::getConfig()->read('harvest.account_id'),
113 20
                Workflow::getConfig()->read('harvest.api_token')
114
            );
115
        }
116
117 50
        if (self::getInstance()->getConfig()->read('everhour.is_active')) {
118 10
            return new Everhour(
119 10
                Workflow::getConfig()->read('everhour.api_token')
120
            );
121
        }
122
123 40
        if (self::getInstance()->getConfig()->read('clockify.is_active')) {
124
            return new Clockify(
125
                Workflow::getConfig()->read('clockify.api_token')
126
            );
127
        }
128
129 40
        return null;
130
    }
131
132 906
    public static function disableAllServices()
133
    {
134 906
        foreach (self::SERVICES as $service) {
135 906
            Workflow::getConfig()->write("$service.is_active", false);
136
        }
137 906
    }
138
139 602
    protected static function getCurrentMenuClass()
140
    {
141 602
        $args = explode('_', getenv('next'));
142
143 602
        if (in_array($args[0], self::SERVICES)) {
144 490
            $service = ucfirst($args[0]);
145 490
            $action = substr(getenv('next'), strlen($args[0]));
146
147 490
            return __NAMESPACE__ . "\\Menus\\$service\\" . self::getMenuClassName($action);
148
        }
149
150 112
        return __NAMESPACE__ . "\\Menus\\" . (self::getMenuClassName(getenv('next')) ?: 'Entrance');
151
    }
152
153 602
    private static function getMenuClassName($action)
154
    {
155 602
        return str_replace('_', '', ucwords($action === false ? 'entrance' : $action, '_'));
156
    }
157
}
158