Completed
Pull Request — master (#544)
by Alexandru
01:26
created

Helpers   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 1
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A redisListToArray() 0 12 1
A createFulfilledPromise() 0 8 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) {
0 ignored issues
show
Bug introduced by
The variable $keys does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $values does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
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