Completed
Push — master ( 5a29a0...22ab66 )
by Dmitry
10:00
created

QueueController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 6
dl 0
loc 46
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A actionRun() 0 4 1
A actionTest() 0 5 1
B attachEventHandlers() 0 27 1
1
<?php
2
3
namespace hiqdev\assetpackagist\console;
4
5
use hiqdev\assetpackagist\commands\AbstractPackageCommand;
6
use Yii;
7
use yii\base\Event;
8
use yii\console\Controller;
9
use yii\helpers\Console;
10
use zhuravljov\yii\queue\ErrorEvent;
11
use zhuravljov\yii\queue\JobEvent;
12
use zhuravljov\yii\queue\Queue;
13
14
class QueueController extends Controller
15
{
16
    public function init()
17
    {
18
        $this->attachEventHandlers();
19
    }
20
21
    public function actionRun($channel)
22
    {
23
        Yii::$app->queue->run($channel);
24
    }
25
26
    public function actionTest()
27
    {
28
        Yii::info(Console::renderColoredString('Lorem ipsum for time %y' . time() . '%n ' . "\n"), \hiqdev\assetpackagist\commands\CollectDependenciesCommand::class);
29
        sleep(1);
30
    }
31
32
    private function attachEventHandlers()
33
    {
34
        $out = function ($string) {
35
            $this->stdout(Console::renderColoredString($string));
36
        };
37
38
        Event::on(Queue::class, Queue::EVENT_BEFORE_WORK, function ($event) use ($out) {
39
            /** @var JobEvent $event */
40
            $out("%Y[{$event->channel}]%n %GNew job%n '" . get_class($event->job) . "'\n");
41
        });
42
43
        Event::on(Queue::class, Queue::EVENT_AFTER_WORK, function ($event) use ($out) {
44
            /** @var JobEvent $event */
45
            $out("%Y[{$event->channel}]%n %GJob%n '" . get_class($event->job) . "' %Gis completed%n\n");
46
        });
47
48
        Event::on(Queue::class, Queue::EVENT_AFTER_ERROR, function ($event) use ($out) {
49
            /** @var ErrorEvent $event */
50
            $out("%Y[{$event->channel}]%n %RJob '" . get_class($event->job) . "' finished with error:%n '" . $event->error . "'\n");
51
        });
52
53
        Event::on(AbstractPackageCommand::class, AbstractPackageCommand::EVENT_BEFORE_RUN, function ($event) use ($out) {
54
            /** @var AbstractPackageCommand $command */
55
            $command = $event->sender;
56
            $out("%g[" . get_class($command) . "]%n Working on package %N" . $command->getPackage()->getFullName() . "%n\n");
57
        });
58
    }
59
}
60
61