Completed
Push — master ( 2ba21e...9f54f5 )
by Dmitry
01:44
created

QueueController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 4
crap 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
24
/**
25
 * Manages service Queue.
26
 */
27
class QueueController extends Controller
28
{
29
    /**
30
     * @var Queue|\yii\queue\db\Queue
31
     */
32
    private $queue;
33
34
    /**
35
     * How many jobs will be executed without process restart
36
     */
37
    const MAX_EXECUTED_JOBS = 100;
38
39
    /**
40
     * @var int
41
     */
42
    private $executedJobsCount = 0;
43
44
    public function __construct($id, Module $module, Queue $queue, array $config = [])
45
    {
46
        parent::__construct($id, $module, $config);
47
        $this->queue = $queue;
48
    }
49
50
    public function init()
51
    {
52
        $this->attachEventHandlers();
53
    }
54
55
    /**
56
     * Runs the queue.
57
     */
58
    public function actionRun()
59
    {
60
        $this->queue->run();
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...
61
    }
62
63
    /**
64
     * Test action to ensure that ErrorHandler flushes stack immediately.
65
     */
66
    public function actionTestErrorHandler()
67
    {
68
        Yii::info(Console::renderColoredString('Lorem ipsum for time %y' . time() . '%n ' . "\n"), \hiqdev\assetpackagist\commands\CollectDependenciesCommand::class);
69
        sleep(1);
70
    }
71
72
73
    private function ensureLimits()
74
    {
75
        if (++$this->executedJobsCount >= static::MAX_EXECUTED_JOBS && function_exists('posix_kill')) {
76
            $this->stdout('Reached limit of ' . static::MAX_EXECUTED_JOBS . " executed jobs. Stopping process.\n");
77
            Signal::setExitFlag();
0 ignored issues
show
Bug introduced by
The method setExitFlag() does not seem to exist on object<yii\queue\cli\Signal>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
        }
79
    }
80
81
    /**
82
     * Attaches handlers on Queue events.
83
     */
84
    private function attachEventHandlers()
85
    {
86
        $out = function ($string) {
87
            $this->stdout(Console::renderColoredString($string));
88
        };
89
90
        Event::on(Queue::class, Queue::EVENT_BEFORE_EXEC, function ($event) use ($out) {
91
            /** @var JobEvent $event */
92
            $out("%GNew job%n '" . get_class($event->job) . "'\n");
93
            $this->ensureLimits();
94
        });
95
96
        Event::on(Queue::class, Queue::EVENT_AFTER_EXEC, function ($event) use ($out) {
97
            /** @var JobEvent $event */
98
            $out("%GJob%n '" . get_class($event->job) . "' %Gis completed%n\n");
99
        });
100
101
        Event::on(Queue::class, Queue::EVENT_AFTER_ERROR, function ($event) use ($out) {
102
            /** @var ErrorEvent $event */
103
            $out("%RJob '" . get_class($event->job) . "' finished with error:%n '" . $event->error . "'\n");
104
        });
105
106
        Event::on(AbstractPackageCommand::class, AbstractPackageCommand::EVENT_BEFORE_RUN, function ($event) use ($out) {
107
            /** @var AbstractPackageCommand $command */
108
            $command = $event->sender;
109
            $out('%g[' . get_class($command) . ']%n Working on package %N' . $command->getPackage()->getFullName() . "%n\n");
110
        });
111
    }
112
}
113