Issues (1019)

src/Swoole/Task/BaseTask.php (21 issues)

1
<?php
2
0 ignored issues
show
Missing file doc comment
Loading history...
3
namespace Hhxsv5\LaravelS\Swoole\Task;
4
5
use Swoole\Timer;
6
7
abstract class BaseTask
0 ignored issues
show
Missing doc comment for class BaseTask
Loading history...
8
{
9
    /**
10
     * The number of seconds before the task should be delayed.
11
     * @var int
0 ignored issues
show
There must be exactly one blank line before the tags in a doc comment
Loading history...
12
     */
13
    protected $delay = 0;
14
15
    /**
16
     * The number of tries.
17
     * @var int
0 ignored issues
show
There must be exactly one blank line before the tags in a doc comment
Loading history...
18
     */
19
    protected $tries = 1;
20
21
    /**
22
     * Delay in seconds, null means no delay.
23
     * @param int $delay
0 ignored issues
show
There must be exactly one blank line before the tags in a doc comment
Loading history...
Missing parameter comment
Loading history...
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
24
     * @return $this
0 ignored issues
show
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
25
     */
26
    public function delay($delay)
27
    {
28
        if ($delay < 0) {
29
            throw new \InvalidArgumentException('The delay must be greater than or equal to 0');
30
        }
31
        $this->delay = (int)$delay;
32
        return $this;
33
    }
34
35
    /**
36
     * Return the delay time.
37
     * @return int
0 ignored issues
show
There must be exactly one blank line before the tags in a doc comment
Loading history...
38
     */
39
    public function getDelay()
40
    {
41
        return $this->delay;
42
    }
43
44
    /**
45
     * Set the number of tries.
46
     * @param int $tries
0 ignored issues
show
There must be exactly one blank line before the tags in a doc comment
Loading history...
Missing parameter comment
Loading history...
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
47
     * @return $this
0 ignored issues
show
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
48
     */
49
    public function setTries($tries)
50
    {
51
        if ($tries < 1) {
52
            throw new \InvalidArgumentException('The number of attempts must be greater than or equal to 1');
53
        }
54
        $this->tries = (int)$tries;
55
        return $this;
56
    }
57
58
    /**
59
     * Get the number of tries.
60
     * @return int
0 ignored issues
show
There must be exactly one blank line before the tags in a doc comment
Loading history...
61
     */
62
    public function getTries()
63
    {
64
        return $this->tries;
65
    }
66
67
    /**
68
     * Deliver a task
69
     * @param mixed $task The task object
0 ignored issues
show
There must be exactly one blank line before the tags in a doc comment
Loading history...
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
70
     * @return bool
0 ignored issues
show
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
71
     */
72
    protected function task($task)
73
    {
74
        static $dispatch;
75
        if (!$dispatch) {
76
            $dispatch = static function ($task) {
77
                /**@var \Swoole\Http\Server $swoole */
0 ignored issues
show
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
The open comment tag must be the only content on the line
Loading history...
78
                $swoole = app('swoole');
0 ignored issues
show
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

78
                $swoole = /** @scrutinizer ignore-call */ app('swoole');
Loading history...
79
                // The worker_id of timer process is -1
80
                if ($swoole->worker_id === -1 || $swoole->taskworker) {
81
                    $workerNum = isset($swoole->setting['worker_num']) ? $swoole->setting['worker_num'] : 0;
82
                    $availableId = mt_rand(0, $workerNum - 1);
83
                    return $swoole->sendMessage($task, $availableId);
84
                }
85
                $taskId = $swoole->task($task);
86
                return $taskId !== false;
87
            };
88
        }
89
90
        if ($this->delay > 0) {
91
            Timer::after($this->delay * 1000, $dispatch, $task);
92
            return true;
93
        }
94
95
        return $dispatch($task);
96
    }
97
}