Passed
Push — master ( 0fb1f2...5691e3 )
by Biao
03:57
created

CustomProcessTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B addCustomProcesses() 0 46 6
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
            $processHandler = function () use ($swoole, $processPrefix, $process, $laravelConfig) {
31
                // Inject the global variables
32
                $_SERVER = $laravelConfig['_SERVER'];
33
                $_ENV = $laravelConfig['_ENV'];
34
35
                $name = $process::getName() ?: 'custom';
36
                $this->setProcessTitle(sprintf('%s laravels: %s process', $processPrefix, $name));
37
                $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

37
                $this->/** @scrutinizer ignore-call */ 
38
                       initLaravel($laravelConfig, $swoole);
Loading history...
38
                $maxTry = 10;
39
                $i = 0;
40
                do {
41
                    $this->callWithCatchException(function () use ($process, $swoole) {
42
                        return $process::callback($swoole);
43
                    });
44
                    ++$i;
45
                    sleep(1);
46
                } while ($i < $maxTry);
47
                $this->log(sprintf('The custom process "%s" reaches maximum number of retries, and will be restarted by the manager process.', $name), 'WARN');
48
            };
49
            $customProcess = new \swoole_process(
50
                $processHandler,
51
                $process::isRedirectStdinStdout(),
52
                $process::getPipeType()
53
            );
54
            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...
55
                $processList[] = $customProcess;
56
            }
57
        }
58
        return $processList;
59
    }
60
61
}
62