Completed
Push — master ( a455c0...4a0425 )
by Charles
01:57
created

QueueController::actionIndex()   B

Complexity

Conditions 9
Paths 18

Size

Total Lines 55
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 7.2446
c 0
b 0
f 0
cc 9
eloc 33
nc 18
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace app\commands;
4
5
use yii\console\Controller;
6
use ReflectionClass;
7
use Yii;
8
9
/**
10
 * Handles processing of Queue Events
11
 */
12
class QueueController extends Controller
13
{
14
    /**
15
     * Main action to call for queue scanning
16
     * @param $queueName    Defaults to the app queue
17
     */
18
    public function actionIndex($queueName = 'app')
19
    {
20
        $queue = Yii::$app->queue->get()->queue($queueName);
21
        while (true) {
22
            try {
23
                $job = $queue->pull(1000);
24
                if (is_null($job)) {
25
                    // There's nothing presently in the queue, so don't do anything
26
                    if (YII_DEBUG) {
27
                        Yii::info("There aren't any jobs in the queue. Waiting for additional jobs...");
28
                    }
29
                    continue;
30
                }
31
            } catch (\Disque\Queue\JobNotAvailableException $e) {
0 ignored issues
show
Bug introduced by
The class Disque\Queue\JobNotAvailableException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
32
                if (YII_DEBUG) {
33
                    Yii::info("[" . date('Y-m-d H:i:s') . "] A scheduled job is available, but we should wait before running it.");
34
                }
35
                continue;
36
            }
37
            
38
            $body = $job->getBody();
39
            if (!isset($body['class'])) {
40
                Yii::error("An error occured when trying to run an event with the following payload:" . print_r($body, true));
41
                continue;
42
            }
43
44
            try {
45
                $class = $body['class'];
46
                unset($body['class']);
47
48
                $event = new $class($body, $queue, $job);
49
                $reflect = new ReflectionClass($event);
50
                $eventName = $reflect->getShortName();
51
                
52
                // Bind an event handler
53
                $this->on($eventName, function ($event) {
54
                    $event->run();
55
                    // The event should be handled in some way, if it isn't, called the global event handler and mark it as processed
56
                    if (!$event->handled) {
57
                        $event->handled();
58
                    }
59
                });
60
61
                // Trigger the event
62
                $this->trigger($eventName, $event);
63
64
                // Unbind the event handler
65
                $this->off($eventName);
66
            } catch (\Exception $e) {
67
                Yii::error("Event $class failed encountered an error: " . $e->getMessage());
68
                $queue->processed($job);
69
                continue;
70
            }
71
        }
72
    }
73
}