Completed
Push — master ( ebc585...e96b46 )
by
unknown
17s queued 12s
created

Context::getRequestedCoroutineId()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 13
rs 10
1
<?php
2
3
namespace SwooleTW\Http\Coroutine;
4
5
use Illuminate\Contracts\Container\Container;
6
use Swoole\Coroutine;
7
8
class Context
9
{
10
    protected const MAX_RECURSE_COROUTINE_ID = 50;
11
12
    /**
13
     * The app containers in different coroutine environment.
14
     *
15
     * @var array
16
     */
17
    protected static $apps = [];
18
19
    /**
20
     * The data in different coroutine environment.
21
     *
22
     * @var array
23
     */
24
    protected static $data = [];
25
26
    /**
27
     * Get app container by current coroutine id.
28
     */
29
    public static function getApp()
30
    {
31
        return static::$apps[static::getRequestedCoroutineId()] ?? null;
32
    }
33
34
    /**
35
     * Set app container by current coroutine id.
36
     *
37
     * @param \Illuminate\Contracts\Container\Container $app
38
     */
39
    public static function setApp(Container $app)
40
    {
41
        static::$apps[static::getRequestedCoroutineId()] = $app;
42
    }
43
44
    /**
45
     * Get data by current coroutine id.
46
     *
47
     * @param string $key
48
     *
49
     * @return mixed|null
50
     */
51
    public static function getData(string $key)
52
    {
53
        return static::$data[static::getRequestedCoroutineId()][$key] ?? null;
54
    }
55
56
    /**
57
     * Set data by current coroutine id.
58
     *
59
     * @param string $key
60
     * @param $value
61
     */
62
    public static function setData(string $key, $value)
63
    {
64
        static::$data[static::getRequestedCoroutineId()][$key] = $value;
65
    }
66
67
    /**
68
     * Remove data by current coroutine id.
69
     *
70
     * @param string $key
71
     */
72
    public static function removeData(string $key)
73
    {
74
        unset(static::$data[static::getRequestedCoroutineId()][$key]);
75
    }
76
77
    /**
78
     * Get data keys by current coroutine id.
79
     */
80
    public static function getDataKeys()
81
    {
82
        return array_keys(static::$data[static::getRequestedCoroutineId()] ?? []);
83
    }
84
85
    /**
86
     * Clear data by current coroutine id.
87
     */
88
    public static function clear()
89
    {
90
        unset(static::$apps[static::getRequestedCoroutineId()]);
91
        unset(static::$data[static::getRequestedCoroutineId()]);
92
    }
93
94
    public static function getCoroutineId(): int
95
    {
96
        return Coroutine::getuid();
97
    }
98
99
    /**
100
     * Get current coroutine id.
101
     */
102
    public static function getRequestedCoroutineId(): int
103
    {
104
        $currentId = static::getCoroutineId();
105
        if ($currentId === -1) {
106
            return -1;
107
        }
108
109
        $counter = 0;
110
        while (($topCoroutineId = Coroutine::getPcid($currentId)) !== -1 && $counter <= static::MAX_RECURSE_COROUTINE_ID) {
0 ignored issues
show
Bug introduced by
The method getPcid() does not exist on Swoole\Coroutine. ( Ignorable by Annotation )

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

110
        while (($topCoroutineId = Coroutine::/** @scrutinizer ignore-call */ getPcid($currentId)) !== -1 && $counter <= static::MAX_RECURSE_COROUTINE_ID) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
111
            $currentId = $topCoroutineId;
112
            $counter++;
113
        }
114
        return $currentId;
115
    }
116
}
117