Passed
Push — tests ( 84c3af...6cdbc5 )
by Guillaume
02:04
created

BaseWorkflow::do()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Godbout\Alfred\Workflow;
4
5
use ReflectionClass;
6
use Godbout\Alfred\Workflow\Contracts\Workflow;
7
8
abstract class BaseWorkflow implements Workflow
9
{
10
    private static $instance = null;
11
    private $scriptFilter = null;
12
13
14 2
    final protected function __construct()
15
    {
16 2
        $this->scriptFilter = ScriptFilter::create();
17 2
    }
18
19 4
    final public static function getInstance()
20
    {
21 4
        if (is_null(self::$instance)) {
22 2
            self::$instance = new static();
23
        }
24
25 4
        return self::$instance;
26
    }
27
28 1
    public static function currentMenu()
29
    {
30 1
        static::getCurrentMenuClass()::scriptFilter();
31
32 1
        return static::getInstance()->scriptFilter->output();
33
    }
34
35 1
    private static function getCurrentMenuClass()
36
    {
37 1
        $menu = static::getMenuClassName(getenv('action')) ?: 'Entrance';
38
39 1
        return (new ReflectionClass(static::class))->getNamespaceName() . "\\Menus\\" . $menu;
40
    }
41
42 1
    private static function getMenuClassName($action)
43
    {
44 1
        return str_replace('_', '', ucwords($action === false ? 'entrance' : $action, '_'));
45
    }
46
47 2
    public static function do()
48
    {
49 2
        $action = getenv('workflow_action');
50
51 2
        if (method_exists(static::class, $action)) {
52 1
            return static::$action();
53
        }
54
55 1
        return false;
56
    }
57
58
    public static function notify($result = false)
59
    {
60
        $action = getenv('workflow_action');
61
62
        if ($result === false) {
63
            return "Oops... cannot $action.";
64
        }
65
66
        return "$action is done!";
67
    }
68
69 1
    final public static function destroy()
70
    {
71 1
        ScriptFilter::destroy();
72
73 1
        self::$instance = null;
74 1
    }
75
76
    final private function __clone()
77
    {
78
    }
79
}
80