ConsoleSubscriber::onShutdown()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
namespace Workana\AsyncJobs\EventListener;
3
4
use Workana\AsyncJobs\AsyncJobsEvents;
5
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Workana\AsyncJobs\Event;
8
use Workana\AsyncJobs\Exception\AggregateException;
9
use Workana\AsyncJobs\ExecutionInfo;
10
use Workana\AsyncJobs\Formatter\Formatter;
11
use Workana\AsyncJobs\Job;
12
13
/**
14
 * @author Carlos Frutos <[email protected]>
15
 */
16
class ConsoleSubscriber implements EventSubscriberInterface
17
{
18
    /**
19
     * @var OutputInterface
20
     */
21
    protected $output;
22
23
    /**
24
     * @var Formatter
25
     */
26
    protected $formatter;
27
28
    /**
29
     * @var OutputInterface $output
30
     */
31
    public function __construct(OutputInterface $output)
32
    {
33
        $this->output = $output;
34
        $this->formatter = new Formatter();
35
    }
36
37
    /**
38
     * @return array
39
     */
40
    public static function getSubscribedEvents()
41
    {
42
        return [
43
            AsyncJobsEvents::WORKER_SHUTDOWN => 'onShutdown',
44
            AsyncJobsEvents::SUCCESSFUL_EXECUTION => 'onSuccessfulExecution',
45
            AsyncJobsEvents::REJECTED_EXECUTION => 'onRejectedExecution',
46
            AsyncJobsEvents::PING => 'onPing'
47
        ];
48
    }
49
50
    public function onShutdown()
51
    {
52
        if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
53
            $this->output->writeln('Worker shutting down');
54
        }
55
    }
56
57
    public function onSuccessfulExecution(Event\SuccessfulExecutionEvent $e)
58
    {
59
        if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
60
            $this->output->writeln($this->formatJob($e->getJob(), $e->getInfo()));
61
        }
62
    }
63
64
    /**
65
     * @param Job $job
66
     *
67
     * @return string
68
     */
69
    protected function formatJob(Job $job, ExecutionInfo $info)
70
    {
71
        $message = '[:date] Job handled <fg=green;options=bold>successfully</>: :jobDescription and options :optionsDescription';
72
73
        return strtr($message, [
74
            ':date' => date('m/d/Y H:i:s'),
75
            ':jobDescription' => $this->formatter->format($job),
76
            ':optionsDescription' => $this->formatter->format($info),
77
        ]);
78
    }
79
80
    public function onRejectedExecution(Event\RejectedExecutionEvent $e)
81
    {
82
        if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
83
            $this->output->writeln($this->formatRejection($e->getJob(), $e->getInfo(), $e->getError()));
84
        }
85
    }
86
87
    protected function formatRejection(Job $job, ExecutionInfo $info, $error)
88
    {
89
        if ($error instanceof AggregateException) {
90
            return $this->formatAggregatedError($job, $info, $error);
91
        }
92
93
        $message = '[:date] <fg=red>[ERROR]</> Executing :jobDescription with :infoDescription. Error: :errorDescription';
94
95
        return strtr($message, [
96
            ':date' => date('m/d/Y H:i:s'),
97
            ':jobDescription' => $this->formatter->format($job),
98
            ':infoDescription' => $this->formatter->format($info),
99
            ':errorDescription' => $this->formatter->format($error)
100
        ]);
101
    }
102
103
    protected function formatAggregatedError(Job $job, ExecutionInfo $info, AggregateException $error)
104
    {
105
        $message = '[:date] <fg=red>[AGGREGATED ERROR]</> Executing :jobDescription with :infoDescription. Error: :errorDescription';
106
        $builtMessage = strtr($message, [
107
            ':date' => date('m/d/Y H:i:s'),
108
            ':jobDescription' => $this->formatter->format($job),
109
            ':infoDescription' => $this->formatter->format($info),
110
            ':errorDescription' => $error->getMessage(),
111
        ]);
112
113
        $childErrors = array_map([$this, 'formatChildError'], $error->getChildren());
114
        array_unshift($childErrors, $builtMessage);
115
116
        return implode(PHP_EOL, $childErrors);
117
    }
118
119
    protected function formatChildError($error)
120
    {
121
        return strtr('<fg=red>[CHILD ERROR]</> :errorDescription', [
122
            ':errorDescription' => $this->formatter->format($error)
123
        ]);
124
    }
125
126
    public function onPing()
127
    {
128
        if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
129
            $this->output->writeln('Ping received');
130
        }
131
    }
132
}