Completed
Push — master ( fead31...49a506 )
by Clinton
01:39
created

RedisQ::listen()   B

Complexity

Conditions 8
Paths 15

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 15
nop 4
dl 0
loc 44
rs 7.9715
c 0
b 0
f 0
1
<?php
2
3
namespace RedisQ;
4
5
class RedisQ
6
{
7
    public function queueObject($object)
8
    {
9
        global $redis;
10
11
        $wrapped = serialize($object);
12
13
        $allQueues = new RedisTtlSortedSet('redisQ:allQueues');
14
        $objectQueues = new RedisTtlSortedSet('objectQueues');
15
        $queues = $allQueues->getMembers();
16
17
        $multi = $redis->multi();
18
19
        // Store an instance of the object
20
        $objectID = 'redisQ:objectID:'.uniqID().md5($wrapped);
21
        $objectQueues->add(time(), $objectID);
22
        $multi->setex($objectID, 9600, $wrapped);
23
24
        // Add objectID to all queues
25
        foreach ($queues as $queueID) {
26
            $multi->lPush($queueID, $objectID);
27
            $multi->expire($queueID, 9600);
28
        }
29
        $multi->exec();
30
    }
31
32
    public function registerListener($queueID)
33
    {
34
        $allQueues = new RedisTtlSortedSet('redisQ:allQueues');
35
        $allQueues->add(time(), $queueID);
36
    }
37
38
    public function listen($queueID, $ip, $timeToWait = 10, $filterValue = null)
39
    {
40
        global $redis;
41
42
        $timeToWait = max(1, min(10, $timeToWait));
43
            
44
        if ($ip != "37.59.50.21" && $redis->get("redisQ:banned:$ip") == "true")  {
45
            sleep(10);
46
            header("Location: /banned.html", 302);
47
            exit();
48
        }
49
50
        $rQueueID = "redisQ:queueID:$queueID";
51
        $wQueueID = "redisQ:accessID:$ip";
52
        if ($redis->set($wQueueID, true, Array('nx', 'ex'=>15)) === false) {
53
            $redis->incr("redisQ:queueID:429count:$ip");
54
            $redis->expire("redisQ:queueID:429count:$ip", 300);
55
            if ($redis->get("redisQ:queueID:429count:$ip") > 30) $redis->setex("redisQ:banned:$ip", 10000, "true");
56
            header('HTTP/1.1 429 Too many requests.');
57
            exit();
58
        }
59
60
        self::registerListener($rQueueID);
61
62
        try {
63
            $time = time();
64
            do {
65
                $object = false;
66
                $pop = $redis->blPop($rQueueID, 1);
67
                if (!isset($pop[1])) {
68
                    if (time() >= ($time + $timeToWait)) return;
69
                } else {
70
                    $objectID = $pop[1];
71
                    $object = $redis->get($objectID);
72
                    $object = unserialize($object);
73
                    $object = self::matchesFilter($filterValue, $object);
74
                }
75
            } while ($object === false);
76
        } finally {
77
            $redis->del($wQueueID);
78
        }
79
80
        return $object;
0 ignored issues
show
Bug introduced by
The variable $object does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
81
    }
82
83
    protected function matchesFilter($filterValue, $object)
84
    {
85
        if ($filterValue == null) return $object;
86
        return self::recursive_array_search($filterValue, unserialize($object)) ? $object : false;
87
    }
88
89
    protected function recursive_array_search($needle, $haystack) {
90
        foreach($haystack as $key=>$value) {
91
            $current_key = $key;
0 ignored issues
show
Unused Code introduced by
$current_key is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
92
            if ($needle === $value || (is_array($value) && self::recursive_array_search($needle, $value) !== false)) {
93
                return true;
94
            } else if ($needle === $value) {
95
                return true;
96
            }
97
        }
98
        return false;
99
    }
100
}
101