Passed
Push — master ( 64ec0c...19f165 )
by Biao
03:10
created

TimerTrait::addTimerProcess()   C

Complexity

Conditions 12
Paths 3

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 27
nc 3
nop 3
dl 0
loc 41
rs 6.9666
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
            $this->setProcessTitle(sprintf('%s laravels: timer process', $config['process_prefix']));
21
            $this->initLaravel($laravelConfig, $swoole);
22
            foreach ($config['jobs'] as $jobClass) {
23
                if (is_array($jobClass) && isset($jobClass[0])) {
24
                    $job = new $jobClass[0](isset($jobClass[1]) ? $jobClass[1] : []);
25
                } else {
26
                    $job = new $jobClass();
27
                }
28
                if (!($job instanceof CronJob)) {
29
                    throw new \Exception(sprintf('%s must implement the abstract class %s', get_class($job), CronJob::class));
30
                }
31
                $timerId = swoole_timer_tick($job->interval(), function () use ($job) {
32
                    try {
33
                        $job->run();
34
                    } catch (\Exception $e) {
35
                        $this->logException($e);
36
                    }
37
                });
38
                $job->setTimerId($timerId);
39
                if ($job->isImmediate()) {
40
                    swoole_timer_after(1, function () use ($job) {
41
                        try {
42
                            $job->run();
43
                        } catch (\Exception $e) {
44
                            $this->logException($e);
45
                        }
46
                    });
47
                }
48
            }
49
        };
50
51
        $timerProcess = new \swoole_process($startTimer, false, false);
52
        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...
53
            return $timerProcess;
54
        }
55
    }
56
57
}