Test Setup Failed
Push — master ( 1dec6d...a46529 )
by Php Easy Api
03:50
created

Worker::allWorkers()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Console\Source\Worker;
4
5
use Resta\Worker\WorkerManager;
6
use Resta\Console\ConsoleOutputter;
7
use Resta\Console\ConsoleListAccessor;
8
9
class Worker extends ConsoleOutputter {
10
11
    use ConsoleListAccessor;
12
13
    /**
14
     * @var $type
0 ignored issues
show
Documentation Bug introduced by
The doc comment $type at position 0 could not be parsed: Unknown type name '$type' at position 0 in $type.
Loading history...
15
     */
16
    public $type = 'worker';
17
18
    /**
19
     * @var $define
0 ignored issues
show
Documentation Bug introduced by
The doc comment $define at position 0 could not be parsed: Unknown type name '$define' at position 0 in $define.
Loading history...
20
     */
21
    public $define = 'begins worker for application';
22
23
    /**
24
     * @var $commandRule
0 ignored issues
show
Documentation Bug introduced by
The doc comment $commandRule at position 0 could not be parsed: Unknown type name '$commandRule' at position 0 in $commandRule.
Loading history...
25
     */
26
    public $commandRule = [];
27
28
    /**
29
     * run all workers
30
     *
31
     * @param string $method
32
     * @param bool $break
33
     */
34
    private function allWorkers($method='start',$break=false)
35
    {
36
        foreach(app()->get('worker') as $key=>$item){
37
            $this->argument['worker'] = ucfirst($key);
38
            $this->argument[$key] = ucfirst($key);
39
            $this->{$method}();
40
41
            if($break) break;
42
        }
43
        exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49
    public function start()
50
    {
51
        if(is_null($this->argument['worker'])){
52
            $this->allWorkers();
53
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
54
        }
55
56
        $workerName = $this->projectName().'-'.$this->argument['worker'];
57
        app()->terminate('WORKER');
58
        app()->register('WORKER',$workerName);
59
        app()->terminate('PROJECT_NAME');
60
        app()->register('PROJECT_NAME',strtolower($this->projectName()));
61
        app()->register('WORKER_START',true);
62
        app()->register('WORKER_STOP',false);
63
        app()->register('WORKER_STATUS',false);
64
        app()->register('WORKER_CLEAR',false);
65
66
        app()->resolve(WorkerManager::class,['args'=>$this->argument])->execute();
67
    }
68
69
    /**
70
     * @inheritDoc
71
     */
72
    public function stop()
73
    {
74
        if(is_null($this->argument['worker'])){
75
            $this->allWorkers('stop');
76
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
77
        }
78
79
        $workerName = $this->projectName().'-'.$this->argument['worker'];
80
        app()->terminate('WORKER');
81
        app()->register('WORKER',$workerName);
82
        app()->terminate('PROJECT_NAME');
83
        app()->register('PROJECT_NAME',strtolower($this->projectName()));
84
        app()->register('WORKER_START',false);
85
        app()->register('WORKER_STOP',true);
86
        app()->register('WORKER_STATUS',false);
87
        app()->register('WORKER_CLEAR',false);
88
89
        app()->resolve(WorkerManager::class,['args'=>$this->argument])->execute();
90
    }
91
92
    /**
93
     * @inheritDoc
94
     */
95
    public function status()
96
    {
97
        if(is_null($this->argument['worker'])){
98
            $this->allWorkers('status',true);
99
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
100
        }
101
102
        $workerName = $this->projectName().'-'.$this->argument['worker'];
103
        app()->register('WORKER',$workerName);
104
        app()->register('PROJECT_NAME',strtolower($this->projectName()));
105
        app()->register('WORKER_START',false);
106
        app()->register('WORKER_STOP',false);
107
        app()->register('WORKER_STATUS',true);
108
        app()->register('WORKER_CLEAR',false);
109
110
        app()->resolve(WorkerManager::class,['args'=>$this->argument])->execute();
111
    }
112
113
    /**
114
     * @inheritDoc
115
     */
116
    public function clear()
117
    {
118
        if(is_null($this->argument['worker'])){
119
            $this->allWorkers('clear');
120
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
121
        }
122
123
        $workerName = $this->projectName().'-'.$this->argument['worker'];
124
        app()->terminate('WORKER');
125
        app()->register('WORKER',$workerName);
126
        app()->terminate('PROJECT_NAME');
127
        app()->register('PROJECT_NAME',strtolower($this->projectName()));
128
        app()->register('WORKER_START',false);
129
        app()->register('WORKER_STOP',false);
130
        app()->register('WORKER_STATUS',false);
131
        app()->register('WORKER_CLEAR',true);
132
133
        app()->resolve(WorkerManager::class,['args'=>$this->argument])->execute();
134
    }
135
136
    /**
137
     * @inheritDoc
138
     */
139
    public function create()
140
    {
141
        if(!file_exists(app()->path()->workers())){
142
            $this->directory['worker'] = app()->path()->workers();
143
            $this->file->makeDirectory($this);
144
        }
145
146
        $this->argument['workerNamespace'] = app()->namespace()->workers();
147
        $this->argument['workerClass'] = ucfirst($this->argument['worker']).'';
148
        $this->argument['projectName'] = strtolower($this->projectName());
149
150
        $this->touch['worker/worker']= app()->path()->workers().'/'.$this->argument['worker'].'.php';
151
152
153
        $this->file->touch($this);
154
155
        echo $this->classical(' > Worker file called as "'.$this->argument['worker'].'" has been successfully created in the '.app()->path()->workers().'');
156
    }
157
158
159
}