Test Setup Failed
Push — master ( 4eeb1f...47843a )
by Php Easy Api
05:54
created

Worker   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 17 2
B run() 0 30 7
1
<?php
2
3
namespace Resta\Console\Source\Worker;
4
5
use Resta\Console\ConsoleOutputter;
6
use Resta\Console\ConsoleListAccessor;
7
use Resta\Support\Utils;
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 = ['worker'];
27
28
    /**
29
     * @inheritDoc
30
     */
31
    public function run()
32
    {
33
        $worker = strtolower($this->argument['worker']);
34
35
        $registeredWorkers = app()->get('worker');
36
37
        if(isset($registeredWorkers[$worker])){
38
39
            if(!is_callable($registeredWorkers[$worker]) && Utils::isNamespaceExists($registeredWorkers[$worker])) {
40
                $resolve = app()->resolve($registeredWorkers[$worker],['data'=>(array)$this->argument['data']]);
41
            }
42
43
            while(true){
44
45
                if(isset($resolve)){
46
                    $result = $resolve->handle();
47
                    echo $this->classical($worker.' worker called : '.$result);
48
                    sleep($resolve->getSleep());
49
                }
50
51
                if(is_callable($registeredWorkers[$worker])){
52
                    echo $this->classical($registeredWorkers[$worker](1));
53
                    sleep(10);
54
                }
55
56
            }
57
            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...
58
        }
59
60
        exception()->runtime('Any worker is not available');
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function create()
67
    {
68
        if(!file_exists(app()->path()->workers())){
69
            $this->directory['worker'] = app()->path()->workers();
70
            $this->file->makeDirectory($this);
71
        }
72
73
        $this->argument['workerNamespace'] = app()->namespace()->workers();
74
        $this->argument['workerClass'] = ucfirst($this->argument['worker']).'';
75
        $this->argument['projectName'] = strtolower($this->projectName());
76
77
        $this->touch['worker/worker']= app()->path()->workers().'/'.$this->argument['worker'].'.php';
78
79
80
        $this->file->touch($this);
81
82
        echo $this->classical(' > Worker file called as "'.$this->argument['worker'].'" has been successfully created in the '.app()->path()->workers().'');
83
    }
84
85
86
}