Issues (6)

src/Workflow.php (1 issue)

Labels
Severity
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 568
    protected function __construct()
25
    {
26 568
        $this->config = Config::ifEmptyStartWith(self::getDefaultConfig());
27
        
28 568
        parent::__construct();
29 568
    }
30
31 6
    public static function do()
32
    {
33 6
        $action = getenv('action');
34
35 6
        if ($timerId = getenv('timer_id')) {
36
            return Timer::$action($timerId);
37
        }
38
39 6
        if (method_exists(Timer::class, $action)) {
40 6
            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());
0 ignored issues
show
It seems like self::serviceEnabled() can also be of type null; however, parameter $string of ucfirst() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        $service = ucfirst(/** @scrutinizer ignore-type */ self::serviceEnabled());
Loading history...
51
52
        if ($result === false) {
53
            return "Oops... $service cannot $action.";
54
        }
55
56
        return "$service $action!";
57
    }
58
59 568
    private static function getDefaultConfig()
60
    {
61 568
        return include __DIR__ . '/../config/default.php';
62
    }
63
64 568
    public static function getConfig()
65
    {
66 568
        return self::getInstance()->config;
67
    }
68
69 352
    public static function enableService($service = '')
70
    {
71 352
        return self::getInstance()->serviceStatus($service, true);
72
    }
73
74 84
    public static function disableService($service = '')
75
    {
76 84
        return self::getInstance()->serviceStatus($service, false);
77
    }
78
79 12
    public static function services()
80
    {
81 12
        return self::SERVICES;
82
    }
83
84 406
    protected function serviceStatus($service, $status = false)
85
    {
86 406
        self::getInstance()->disableAllServices();
87
88 406
        if (self::getInstance()->classExistsForService($service)) {
89 406
            Workflow::getConfig()->write("$service.is_active", $status);
90
91 406
            return true;
92
        }
93
94
        return false;
95
    }
96
97 406
    protected function classExistsForService($service = '')
98
    {
99 406
        return class_exists(__NAMESPACE__ . '\\Services\\' . ucfirst($service));
100
    }
101
102 94
    public static function serviceEnabled()
103
    {
104 94
        if (self::getInstance()->getConfig()->read('toggl.is_active')) {
105 62
            return new Toggl(
106 62
                Workflow::getConfig()->read('toggl.api_token')
107
            );
108
        }
109
110 50
        if (self::getInstance()->getConfig()->read('harvest.is_active')) {
111 12
            return new Harvest(
112 12
                Workflow::getConfig()->read('harvest.account_id'),
113 12
                Workflow::getConfig()->read('harvest.api_token')
114
            );
115
        }
116
117 38
        if (self::getInstance()->getConfig()->read('everhour.is_active')) {
118 6
            return new Everhour(
119 6
                Workflow::getConfig()->read('everhour.api_token')
120
            );
121
        }
122
123 32
        if (self::getInstance()->getConfig()->read('clockify.is_active')) {
124 8
            return new Clockify(
125 8
                Workflow::getConfig()->read('clockify.api_token')
126
            );
127
        }
128
129 24
        return null;
130
    }
131
132 568
    public static function disableAllServices()
133
    {
134 568
        foreach (self::SERVICES as $service) {
135 568
            Workflow::getConfig()->write("$service.is_active", false);
136
        }
137 568
    }
138
139 362
    protected static function getCurrentMenuClass()
140
    {
141 362
        $args = explode('_', getenv('next'));
142
143 362
        if (in_array($args[0], self::SERVICES)) {
144 294
            $service = ucfirst($args[0]);
145 294
            $action = substr(getenv('next'), strlen($args[0]));
146
147 294
            return __NAMESPACE__ . "\\Menus\\$service\\" . self::getMenuClassName($action);
148
        }
149
150 68
        return __NAMESPACE__ . "\\Menus\\" . (self::getMenuClassName(getenv('next')) ?: 'Entrance');
151
    }
152
153 362
    private static function getMenuClassName($action)
154
    {
155 362
        return str_replace('_', '', ucwords($action === false ? 'entrance' : $action, '_'));
156
    }
157
}
158