forked from librecores/librecores-web
/
AbstractNotificationConsumer.php
87 lines (76 loc) · 1.88 KB
/
AbstractNotificationConsumer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
namespace App\Consumer;
use App\Util\Notification;
use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
use PhpAmqpLib\Message\AMQPMessage;
use Psr\Log\LoggerInterface;
use Exception;
/**
* Class AbstractNotificationConsumer
*
* Base class for all the Notification Consumers that we have
*
* @author Aquib Baig <aquibbaig97@gmail.com>
*/
abstract class AbstractNotificationConsumer implements ConsumerInterface
{
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @var Notification $notification
*/
protected $notification;
/**
* AbstractNotificationConsumer constructor
*
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* @param AMQPMessage $msg
*
* @return bool
*
* @throws Exception
*/
public function execute(AMQPMessage $msg)
{
$this->notification = unserialize($msg->body);
try {
if ($this->shouldHandle()) {
return $this->handle();
}
return true; //don't requeue
} catch (Exception $e) {
// Log out exceptions if they occur, and keep Consumers running for
// next requests
$this->logger->error(
"Processing the Notification resulted in an ".get_class($e)
);
$this->logger->error("Message: ".$e->getMessage());
$this->logger->error("Trace: ".$e->getTraceAsString());
return false;
}
}
/**
* Should a Notification be handled by a the Consumer?
*
* @return bool
*/
protected function shouldHandle()
{
return true;
}
/**
* Actually processes a Notification to its sink
*
* @return mixed
*/
abstract protected function handle():bool;
}
?>