Passed
Push — master ( 382112...948075 )
by Guillaume
05:20
created

BaseWorkflow   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 96.67%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 14
eloc 23
c 3
b 0
f 1
dl 0
loc 70
ccs 29
cts 30
cp 0.9667
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getInstance() 0 7 2
A __clone() 0 2 1
A destroy() 0 5 1
A currentMenu() 0 5 1
A getMenuClassName() 0 3 2
A getCurrentMenuClass() 0 5 2
A do() 0 9 2
A notify() 0 9 2
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
    protected static $instance = null;
11
    protected $scriptFilter = null;
12
13
14 12
    protected function __construct()
15
    {
16 12
        $this->scriptFilter = ScriptFilter::create();
17 12
    }
18
19 24
    final public static function getInstance()
20
    {
21 24
        if (is_null(self::$instance)) {
22 12
            self::$instance = new static();
23
        }
24
25 24
        return self::$instance;
26
    }
27
28 6
    public static function currentMenu()
29
    {
30 6
        static::getCurrentMenuClass()::scriptFilter();
31
32 6
        return static::getInstance()->scriptFilter->output();
33
    }
34
35 6
    private static function getCurrentMenuClass()
36
    {
37 6
        $menu = static::getMenuClassName(getenv('next')) ?: 'Entrance';
38
39 6
        return (new ReflectionClass(static::class))->getNamespaceName() . "\\Menus\\" . $menu;
40
    }
41
42 6
    private static function getMenuClassName($action)
43
    {
44 6
        return str_replace('_', '', ucwords($action === false ? 'entrance' : $action, '_'));
45
    }
46
47 12
    public static function do()
48
    {
49 12
        $action = getenv('action');
50
51 12
        if (method_exists(static::class, $action)) {
52 6
            return static::$action();
53
        }
54
55 6
        return false;
56
    }
57
58 6
    public static function notify($result = false)
59
    {
60 6
        $action = getenv('action');
61
62 6
        if ($result === false) {
63 6
            return "Oops... cannot $action.";
64
        }
65
66 6
        return "$action is done!";
67
    }
68
69 6
    public static function destroy()
70
    {
71 6
        ScriptFilter::destroy();
72
73 6
        self::$instance = null;
74 6
    }
75
76
    final private function __clone()
77
    {
78
    }
79
}
80