Passed
Push — master ( 9dab76...0fb1f2 )
by Biao
04:16
created

CustomProcessTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A addCustomProcesses() 0 40 5
1
<?php
2
3
namespace Hhxsv5\LaravelS\Swoole\Traits;
4
5
use Hhxsv5\LaravelS\Illuminate\Laravel;
6
use Hhxsv5\LaravelS\Swoole\Process\CustomProcessInterface;
7
8
trait CustomProcessTrait
9
{
10
    use ProcessTitleTrait;
11
    use LogTrait;
12
13
    public function addCustomProcesses(\swoole_server $swoole, $processPrefix, array $processes, array $laravelConfig)
14
    {
15
        Laravel::autoload($laravelConfig['root_path']);
16
17
        /**
18
         * @var []CustomProcessInterface $processList
19
         */
20
        $processList = [];
21
        foreach ($processes as $process) {
22
            if (!isset(class_implements($process)[CustomProcessInterface::class])) {
23
                throw new \Exception(sprintf(
24
                        '%s must implement the interface %s',
25
                        $process,
26
                        CustomProcessInterface::class
27
                    )
28
                );
29
            }
30
31
            $processHandler = function () use ($swoole, $processPrefix, $process, $laravelConfig) {
32
                // Inject the global variables
33
                $_SERVER = $laravelConfig['_SERVER'];
34
                $_ENV = $laravelConfig['_ENV'];
35
36
                $name = $process::getName() ?: 'custom';
37
                $this->setProcessTitle(sprintf('%s laravels: %s process', $processPrefix, $name));
38
                $this->initLaravel($laravelConfig, $swoole);
0 ignored issues
show
Bug introduced by
It seems like initLaravel() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

38
                $this->/** @scrutinizer ignore-call */ 
39
                       initLaravel($laravelConfig, $swoole);
Loading history...
39
                $this->callWithCatchException(function () use ($process, $swoole) {
40
                    return $process::callback($swoole);
41
                }, true);
42
            };
43
            $customProcess = new \swoole_process(
44
                $processHandler,
45
                $process::isRedirectStdinStdout(),
46
                $process::getPipeType()
47
            );
48
            if ($swoole->addProcess($customProcess)) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $swoole->addProcess($customProcess) 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...
49
                $processList[] = $customProcess;
50
            }
51
        }
52
        return $processList;
53
    }
54
55
}
56