Test Setup Failed
Push — master ( 79a996...894c42 )
by Php Easy Api
04:27
created

WorkerManager::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 5
rs 10
c 1
b 1
f 0
1
<?php
2
3
namespace Resta\Worker;
4
5
use Resta\Support\Utils;
6
use Resta\Contracts\WorkerContracts;
7
use Resta\Contracts\WorkerManagerContracts;
8
9
class WorkerManager extends WorkerManagerAbstract implements WorkerManagerContracts
10
{
11
    /**
12
     * @var null|object
13
     */
14
    protected $resolve;
15
16
    /**
17
     * execute for worker
18
     *
19
     * @return void|mixed
20
     */
21
    public function execute()
22
    {
23
        //we check the presence of worker on container.
24
        if($this->isWorkerAvailable()){
25
            $this->{$this->getApply()}();
26
        }
27
    }
28
29
    /**
30
     * execute closure for worker
31
     *
32
     * @return void|mixed
33
     */
34
    public function executeClosure()
35
    {
36
        $this->isWorkerClosure();
37
38
        if($this->resolve instanceof \Closure){
39
            $resolve = $this->resolve;
40
            echo $resolve($this->getData()).''.PHP_EOL;
41
            $this->pause();
42
        }
43
    }
44
45
    /**
46
     * execute object for worker
47
     *
48
     * @return void|mixed
49
     */
50
    public function executeObject()
51
    {
52
        $this->isWorkerObject();
53
54
        if($this->resolve instanceof WorkerContracts){
55
            $this->resolve->handle();
56
            echo $this->getWorker().' Worker : Ok'.PHP_EOL;
57
            $this->pause($this->resolve->getSleep());
0 ignored issues
show
Bug introduced by
$this->resolve->getSleep() of type integer is incompatible with the type int|null expected by parameter $pause of Resta\Worker\WorkerManagerAbstract::pause(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
            $this->pause(/** @scrutinizer ignore-type */ $this->resolve->getSleep());
Loading history...
58
        }
59
    }
60
61
    /**
62
     * is worker closure
63
     *
64
     * @return void
65
     */
66
    public function isWorkerClosure()
67
    {
68
        //we check the presence of worker on container.
69
        if($this->isWorkerAvailable()){
70
71
            // if worker comes as an object, this object will be executed.
72
            // worker can have a closure function.
73
            if(is_callable($worker = $this->worker[$this->getWorker()])) {
74
                $this->resolve = $worker;
75
            }
76
        }
77
    }
78
79
    /**
80
     * is worker object
81
     *
82
     * @return void
83
     */
84
    public function isWorkerObject()
85
    {
86
        //we check the presence of worker on container.
87
        if($this->isWorkerAvailable()){
88
89
            // if worker comes as an object, this object will be executed.
90
            // worker can have a closure function.
91
            if(!is_callable($worker = $this->worker[$this->getWorker()]) && Utils::isNamespaceExists($worker)) {
92
                $this->resolve = $this->app->resolve($worker,['data'=>$this->getData()]);
93
            }
94
        }
95
    }
96
}
97