1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Equip\Queue; |
4
|
|
|
|
5
|
|
|
use Equip\Command\OptionsInterface; |
6
|
|
|
use Exception; |
7
|
|
|
use League\Event\EmitterInterface; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
|
10
|
|
|
class Event |
11
|
|
|
{ |
12
|
|
|
const MESSAGE_ACKNOWLEDGE = 'message.acknowledge'; |
13
|
|
|
const MESSAGE_FINISH = 'message.finish'; |
14
|
|
|
const MESSAGE_REJECT = 'message.reject'; |
15
|
|
|
const QUEUE_SHUTDOWN = 'queue.shutdown'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var EmitterInterface |
19
|
|
|
*/ |
20
|
|
|
private $emitter; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var LoggerInterface |
24
|
|
|
*/ |
25
|
|
|
protected $logger; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param EmitterInterface $emitter |
29
|
|
|
* @param LoggerInterface $logger |
30
|
|
|
*/ |
31
|
4 |
|
public function __construct(EmitterInterface $emitter, LoggerInterface $logger) |
32
|
|
|
{ |
33
|
4 |
|
$this->emitter = $emitter; |
34
|
4 |
|
$this->logger = $logger; |
35
|
4 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Emits message acknowledgement events |
39
|
|
|
* |
40
|
|
|
* @param string $command |
41
|
|
|
* @param OptionsInterface $options |
42
|
|
|
*/ |
43
|
1 |
View Code Duplication |
public function acknowledge($command, OptionsInterface $options) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
array_map(function ($name) use ($options) { |
46
|
1 |
|
$this->emitter->emit($name, $options); |
|
|
|
|
47
|
1 |
|
}, [ |
48
|
1 |
|
static::MESSAGE_ACKNOWLEDGE, |
49
|
1 |
|
sprintf('%s.%s', static::MESSAGE_ACKNOWLEDGE, $command) |
50
|
|
|
]); |
51
|
|
|
|
52
|
1 |
|
$this->logger->info(sprintf('`%s` job started', $command)); |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Emits message finished events |
57
|
|
|
* |
58
|
|
|
* @param string $command |
59
|
|
|
* @param OptionsInterface $options |
60
|
|
|
*/ |
61
|
1 |
View Code Duplication |
public function finish($command, OptionsInterface $options) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
array_map(function ($name) use ($options) { |
64
|
1 |
|
$this->emitter->emit($name, $options) ; |
|
|
|
|
65
|
1 |
|
}, [ |
66
|
1 |
|
static::MESSAGE_FINISH, |
67
|
1 |
|
sprintf('%s.%s', static::MESSAGE_FINISH, $command) |
68
|
|
|
]); |
69
|
|
|
|
70
|
1 |
|
$this->logger->info(sprintf('`%s` job finished', $command)); |
71
|
1 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Emits message rejection events |
75
|
|
|
* |
76
|
|
|
* @param string $command |
77
|
|
|
* @param OptionsInterface $options |
78
|
|
|
* @param Exception $exception |
79
|
|
|
*/ |
80
|
1 |
View Code Duplication |
public function reject($command, OptionsInterface $options, Exception $exception) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
array_map(function ($name) use ($options, $exception) { |
83
|
1 |
|
$this->emitter->emit($name, $options, $exception); |
|
|
|
|
84
|
1 |
|
}, [ |
85
|
1 |
|
static::MESSAGE_REJECT, |
86
|
1 |
|
sprintf('%s.%s', static::MESSAGE_REJECT, $command) |
87
|
|
|
]); |
88
|
|
|
|
89
|
1 |
|
$this->logger->error($exception->getMessage()); |
90
|
1 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Emits message shutdown events |
94
|
|
|
* |
95
|
|
|
* @param string $command |
96
|
|
|
*/ |
97
|
|
View Code Duplication |
public function shutdown($command) |
|
|
|
|
98
|
|
|
{ |
99
|
1 |
|
array_map(function ($name) { |
100
|
1 |
|
$this->emitter->emit($name) ; |
101
|
1 |
|
}, [ |
102
|
1 |
|
static::QUEUE_SHUTDOWN, |
103
|
1 |
|
sprintf('%s.%s', static::QUEUE_SHUTDOWN, $command) |
104
|
|
|
]); |
105
|
|
|
|
106
|
1 |
|
$this->logger->notice(sprintf('shutting down by request of `%s`', $command)); |
107
|
1 |
|
} |
108
|
|
|
} |
109
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.