Passed
Push — master ( 3c6e92...0c8524 )
by Guillaume
57:03
created

Workflow::currentMenu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
    ];
18
19
    private $config = null;
20
21
22 582
    protected function __construct()
23
    {
24 582
        $this->config = Config::ifEmptyStartWith(self::getDefaultConfig());
25
        
26 582
        parent::__construct();
27 582
    }
28
29 6
    public static function do()
30
    {
31 6
        $action = getenv('action');
32
33 6
        if ($timerId = getenv('timer_id')) {
34
            return Timer::$action($timerId);
35
        }
36
37 6
        if (method_exists(Timer::class, $action)) {
38 6
            return Timer::$action();
39
        }
40
41
        return true;
42
    }
43
44
    public static function notify($result = false)
45
    {
46
        $action = getenv('action');
47
48
        $service = ucfirst(self::serviceEnabled());
49
50
        if ($result === false) {
51
            return "Oops... $service cannot $action.";
52
        }
53
54
        return "$service $action!";
55
    }
56
57 582
    private static function getDefaultConfig()
58
    {
59 582
        return include __DIR__ . '/../config/default.php';
60
    }
61
62 582
    public static function getConfig()
63
    {
64 582
        return self::getInstance()->config;
65
    }
66
67 408
    public static function enableService($service = '')
68
    {
69 408
        return self::getInstance()->serviceStatus($service, true);
70
    }
71
72 66
    public static function disableService($service = '')
73
    {
74 66
        return self::getInstance()->serviceStatus($service, false);
75
    }
76
77 12
    public static function services()
78
    {
79 12
        return self::SERVICES;
80
    }
81
82 450
    protected function serviceStatus($service, $status = false)
83
    {
84 450
        self::getInstance()->disableAllServices();
85
86 450
        if (self::getInstance()->classExistsForService($service)) {
87 450
            Workflow::getConfig()->write("$service.is_active", $status);
88
89 450
            return true;
90
        }
91
92
        return false;
93
    }
94
95 450
    protected function classExistsForService($service = '')
96
    {
97 450
        return class_exists(__NAMESPACE__ . '\\Services\\' . ucfirst($service));
98
    }
99
100 114
    public static function serviceEnabled()
101
    {
102 114
        if (self::getInstance()->getConfig()->read('toggl.is_active')) {
103 90
            return new Toggl(
104 90
                Workflow::getConfig()->read('toggl.api_token')
105
            );
106
        }
107
108 42
        if (self::getInstance()->getConfig()->read('harvest.is_active')) {
109 12
            return new Harvest(
110 12
                Workflow::getConfig()->read('harvest.account_id'),
111 12
                Workflow::getConfig()->read('harvest.api_token')
112
            );
113
        }
114
115 30
        if (self::getInstance()->getConfig()->read('everhour.is_active')) {
116 6
            return new Everhour(
117 6
                Workflow::getConfig()->read('everhour.api_token')
118
            );
119
        }
120
121 24
        return null;
122
    }
123
124 582
    public static function disableAllServices()
125
    {
126 582
        foreach (self::SERVICES as $service) {
127 582
            Workflow::getConfig()->write("$service.is_active", false);
128
        }
129 582
    }
130
131 300
    protected static function getCurrentMenuClass()
132
    {
133 300
        $args = explode('_', getenv('next'));
134
135 300
        if (in_array($args[0], self::SERVICES)) {
136 228
            $service = ucfirst($args[0]);
137 228
            $action = substr(getenv('next'), strlen($args[0]));
138
139 228
            return __NAMESPACE__ . "\\Menus\\$service\\" . self::getMenuClassName($action);
140
        }
141
142 72
        return __NAMESPACE__ . "\\Menus\\" . (self::getMenuClassName(getenv('next')) ?: 'Entrance');
143
    }
144
145 300
    private static function getMenuClassName($action)
146
    {
147 300
        return str_replace('_', '', ucwords($action === false ? 'entrance' : $action, '_'));
148
    }
149
}
150