Passed
Push — master ( 8feb3f...9dab76 )
by Biao
04:03
created

TimerTrait::addTimerProcess()   B

Complexity

Conditions 11
Paths 3

Size

Total Lines 49
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 30
nc 3
nop 3
dl 0
loc 49
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Hhxsv5\LaravelS\Swoole\Traits;
4
5
use Hhxsv5\LaravelS\Swoole\Timer\CronJob;
6
7
trait TimerTrait
8
{
9
    use ProcessTitleTrait;
10
    use LaravelTrait;
11
    use LogTrait;
12
13
    public function addTimerProcess(\swoole_server $swoole, array $config, array $laravelConfig)
14
    {
15
        if (empty($config['enable']) || empty($config['jobs'])) {
16
            return;
17
        }
18
19
        $startTimer = function () use ($swoole, $config, $laravelConfig) {
20
            // Inject the global variables
21
            $_SERVER = $laravelConfig['_SERVER'];
22
            $_ENV = $laravelConfig['_ENV'];
23
24
            $this->setProcessTitle(sprintf('%s laravels: timer process', $config['process_prefix']));
25
            $this->initLaravel($laravelConfig, $swoole);
26
            foreach ($config['jobs'] as $jobClass) {
27
                if (is_array($jobClass) && isset($jobClass[0])) {
28
                    $job = new $jobClass[0](isset($jobClass[1]) ? $jobClass[1] : []);
29
                } else {
30
                    $job = new $jobClass();
31
                }
32
                if (!($job instanceof CronJob)) {
33
                    throw new \Exception(sprintf(
34
                            '%s must extend the abstract class %s',
35
                            get_class($job),
36
                            CronJob::class
37
                        )
38
                    );
39
                }
40
                if (empty($job->interval())) {
41
                    throw new \Exception(sprintf('The interval of %s cannot be empty', get_class($job)));
42
                }
43
                $timerId = swoole_timer_tick($job->interval(), function () use ($job) {
44
                    $this->callWithCatchException(function () use ($job) {
45
                        $job->run();
46
                    });
47
                });
48
                $job->setTimerId($timerId);
49
                if ($job->isImmediate()) {
50
                    swoole_timer_after(1, function () use ($job) {
51
                        $this->callWithCatchException(function () use ($job) {
52
                            $job->run();
53
                        });
54
                    });
55
                }
56
            }
57
        };
58
59
        $timerProcess = new \swoole_process($startTimer, false, false);
60
        if ($swoole->addProcess($timerProcess)) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $swoole->addProcess($timerProcess) targeting Swoole\Server::addProcess() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
61
            return $timerProcess;
62
        }
63
    }
64
65
}