RunTasks::generateStepItem()
last analyzed

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
nc 1
nop 2
dl 0
loc 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A RunTasks.php$0 ➔ __construct() 0 4 1
1
<?php
2
3
namespace BFW;
4
5
class RunTasks extends Subject
6
{
7
    /**
8
     * @var object[] $runSteps All steps used for run the application
9
     */
10
    protected $runSteps;
11
    
12
    /**
13
     * @var string $notifyPrefix A prefix to use for task name
14
     */
15
    protected $notifyPrefix;
16
    
17
    /**
18
     * Constructor
19
     * 
20
     * @param object[] $runSteps All step to call
21
     * @param string $notifyPrefix The prefix to use for task name
22
     */
23
    public function __construct(array $runSteps, string $notifyPrefix)
24
    {
25
        $this->runSteps     = $runSteps;
26
        $this->notifyPrefix = $notifyPrefix;
27
    }
28
    
29
    /**
30
     * Getter to access to the run step array
31
     * 
32
     * @return object[]
33
     */
34
    public function getRunSteps(): array
35
    {
36
        return $this->runSteps;
37
    }
38
    
39
    /**
40
     * Setter to re-define the run step array
41
     * 
42
     * @param object[] $runSteps The new list of run steps
43
     * 
44
     * @return $this
45
     */
46
    public function setRunSteps(array $runSteps): self
47
    {
48
        $this->runSteps = $runSteps;
49
        return $this;
50
    }
51
    
52
    /**
53
     * Add a new run step to the list
54
     * 
55
     * @param string $name          The name of the new run step
56
     * @param object $runStepsToAdd The run step to add
57
     * 
58
     * @return $this
59
     */
60
    public function addToRunSteps(string $name, $runStepsToAdd): self
61
    {
62
        $this->runSteps[$name] = $runStepsToAdd;
63
        return $this;
64
    }
65
    
66
    /**
67
     * Getter to property notifyPrefix
68
     * 
69
     * @return string
70
     */
71
    public function getNotifyPrefix(): string
72
    {
73
        return $this->notifyPrefix;
74
    }
75
    
76
    /**
77
     * Setter to re-define the notifyPrefix
78
     * 
79
     * @param string $notifyPrefix
80
     * 
81
     * @return $this
82
     */
83
    public function setNotifyPrefix(string$notifyPrefix): self
84
    {
85
        $this->notifyPrefix = $notifyPrefix;
86
        return $this;
87
    }
88
    
89
    /**
90
     * Run all steps declared and notify for each step
91
     * Call the callback if declared for each step
92
     * 
93
     * @return void
94
     */
95
    public function run()
96
    {
97
        $prefix = $this->notifyPrefix;
98
        
99
        $this->addNotification($prefix.'_start_run_tasks');
100
        
101
        foreach ($this->runSteps as $actionName => $stepInfos) {
102
            $context = null;
103
            if ($stepInfos->context !== null) {
104
                $context = $stepInfos->context;
105
            }
106
            
107
            if ($stepInfos->callback === null) {
108
                $this->addNotification($prefix.'_exec_'.$actionName, $context);
109
                continue;
110
            }
111
            
112
            $this->addNotification($prefix.'_run_'.$actionName, $context);
113
114
            if (is_callable($stepInfos->callback)) {
115
                $callback = $stepInfos->callback;
116
                $callback();
117
            }
118
119
            $this->addNotification($prefix.'_done_'.$actionName, $context);
120
        }
121
        
122
        $this->addNotification($prefix.'_end_run_tasks');
123
    }
124
    
125
    /**
126
     * Send a notification to all observers connected to the subject
127
     * 
128
     * @param string $action The action name
129
     * @param mixed $context (default null) The context to pass to the subject
130
     * 
131
     * @return void
132
     */
133
    public function sendNotify(string $action, $context = null)
134
    {
135
        \BFW\Application::getInstance()
136
            ->getMonolog()
137
            ->getLogger()
138
            ->debug(
139
                'RunTask notify',
140
                [
141
                    'prefix' => $this->notifyPrefix,
142
                    'action' => $action
143
                ]
144
            )
145
        ;
146
        
147
        $this->addNotification($action, $context);
148
    }
149
    
150
    /**
151
     * Generate the anonymous class with the structure to use for each item
152
     * 
153
     * @param mixed $context The context to add to the notify
154
     * @param callable|null $callback The callback to call when
155
     *  the task is runned
156
     * 
157
     * @return object
158
     */
159
    public static function generateStepItem($context = null, $callback = null)
160
    {
161
        return new class ($context, $callback) {
162
            public $context;
163
            public $callback;
164
            
165
            public function __construct($context, $callback)
166
            {
167
                $this->context  = $context;
168
                $this->callback = $callback;
169
            }
170
        };
171
    }
172
}
173