QueueController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 7
dl 0
loc 95
ccs 0
cts 51
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A init() 0 4 1
A actionRun() 0 4 1
A actionTestErrorHandler() 0 5 1
A ensureLimits() 0 8 3
A attachEventHandlers() 0 37 2
1
<?php
2
/**
3
 * Asset Packagist.
4
 *
5
 * @link      https://github.com/hiqdev/asset-packagist
6
 * @package   asset-packagist
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\assetpackagist\console;
12
13
use hiqdev\assetpackagist\commands\AbstractPackageCommand;
14
use Yii;
15
use yii\base\Event;
16
use yii\base\Module;
17
use yii\console\Controller;
18
use yii\helpers\Console;
19
use yii\queue\cli\Signal;
20
use yii\queue\ErrorEvent;
21
use yii\queue\JobEvent;
22
use yii\queue\Queue;
23
use yii\queue\cli\Queue as CliQueue;
24
25
/**
26
 * Manages service Queue.
27
 */
28
class QueueController extends Controller
29
{
30
    /**
31
     * @var Queue|\yii\queue\db\Queue
32
     */
33
    private $queue;
34
35
    /**
36
     * How many jobs will be executed without process restart
37
     */
38
    const MAX_EXECUTED_JOBS = 100;
39
40
    /**
41
     * @var int
42
     */
43
    private $executedJobsCount = 0;
44
45
    public function __construct($id, Module $module, Queue $queue, array $config = [])
46
    {
47
        parent::__construct($id, $module, $config);
48
        $this->queue = $queue;
49
    }
50
51
    public function init()
52
    {
53
        $this->attachEventHandlers();
54
    }
55
56
    /**
57
     * Runs the queue.
58
     */
59
    public function actionRun()
60
    {
61
        $this->queue->run(true);
0 ignored issues
show
Bug introduced by
The method run does only exist in yii\queue\db\Queue, but not in yii\queue\Queue.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
62
    }
63
64
    /**
65
     * Test action to ensure that ErrorHandler flushes stack immediately.
66
     */
67
    public function actionTestErrorHandler()
68
    {
69
        Yii::info(Console::renderColoredString('Lorem ipsum for time %y' . time() . '%n ' . "\n"), \hiqdev\assetpackagist\commands\CollectDependenciesCommand::class);
70
        sleep(1);
71
    }
72
73
    private function ensureLimits()
74
    {
75
        if ($this->executedJobsCount > static::MAX_EXECUTED_JOBS && function_exists('posix_kill')) {
76
            return 15; // SIGTERM
77
        }
78
79
        return null;
80
    }
81
82
    /**
83
     * Attaches handlers on Queue events.
84
     */
85
    private function attachEventHandlers()
86
    {
87
        $out = function ($string) {
88
            $this->stdout(Console::renderColoredString($string));
89
        };
90
91
        Event::on(Queue::class, Queue::EVENT_BEFORE_EXEC, function ($event) use ($out) {
92
            /** @var JobEvent $event */
93
            $out("%GNew job%n '" . get_class($event->job) . "'\n");
94
            $this->executedJobsCount++;
95
            $this->ensureLimits();
96
        });
97
98
        Event::on(Queue::class, Queue::EVENT_AFTER_EXEC, function ($event) use ($out) {
99
            /** @var JobEvent $event */
100
            $out("%GJob%n '" . get_class($event->job) . "' %Gis completed%n\n");
101
        });
102
103
        Event::on(Queue::class, Queue::EVENT_AFTER_ERROR, function ($event) use ($out) {
104
            /** @var ErrorEvent $event */
105
            $out("%RJob '" . get_class($event->job) . "' finished with error:%n '" . $event->error . "'\n");
106
        });
107
108
        Event::on(Queue::class, CliQueue::EVENT_WORKER_LOOP, function (\yii\queue\cli\WorkerEvent $event) use ($out) {
109
            $exitCode = $this->ensureLimits();
110
            if ($exitCode !== null) {
111
                $out('Reached limit of ' . static::MAX_EXECUTED_JOBS . " executed jobs. Stopping process.\n");
112
                $event->exitCode = $exitCode;
113
            }
114
        });
115
116
        Event::on(AbstractPackageCommand::class, AbstractPackageCommand::EVENT_BEFORE_RUN, function ($event) use ($out) {
117
            /** @var AbstractPackageCommand $command */
118
            $command = $event->sender;
119
            $out('%g[' . get_class($command) . ']%n Working on package %N' . $command->getPackage()->getFullName() . "%n\n");
120
        });
121
    }
122
}
123