Helpers   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 42
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createFulfilledPromise() 0 7 1
A redisListToArray() 0 11 1
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets;
4
5
use React\Promise\PromiseInterface;
6
7
class Helpers
8
{
9
    /**
10
     * The loop used to create the Fulfilled Promise.
11
     *
12
     * @var null|\React\EventLoop\LoopInterface
13
     */
14
    public static $loop = null;
15
16
    /**
17
     * Transform the Redis' list of key after value
18
     * to key-value pairs.
19
     *
20
     * @param  array  $list
21
     * @return array
22
     */
23
    public static function redisListToArray(array $list)
24
    {
25
        // Redis lists come into a format where the keys are on even indexes
26
        // and the values are on odd indexes. This way, we know which
27
        // ones are keys and which ones are values and their get combined
28
        // later to form the key => value array.
29
        [$keys, $values] = collect($list)->partition(function ($value, $key) {
30
            return $key % 2 === 0;
31
        });
32
33
        return array_combine($keys->all(), $values->all());
34
    }
35
36
    /**
37
     * Create a new fulfilled promise with a value.
38
     *
39
     * @param  mixed  $value
40
     * @return \React\Promise\PromiseInterface
41
     */
42
    public static function createFulfilledPromise($value): PromiseInterface
43
    {
44
        $resolver = config(
45
            'websockets.promise_resolver', \React\Promise\FulfilledPromise::class
46
        );
47
48
        return new $resolver($value, static::$loop);
49
    }
50
}
51