1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\NotificationBundle\Backend; |
15
|
|
|
|
16
|
|
|
use Sonata\NotificationBundle\Model\MessageInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Base class for queue backent dispatchers. |
20
|
|
|
* |
21
|
|
|
* @author Kevin Nedelec <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
abstract class QueueBackendDispatcher implements QueueDispatcherInterface, BackendInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $queues; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $defaultQueue; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var BackendInterface[] |
37
|
|
|
*/ |
38
|
|
|
protected $backends; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $defaultQueue |
42
|
|
|
* @param BackendInterface[] $backends |
43
|
|
|
*/ |
44
|
|
|
public function __construct(array $queues, $defaultQueue, array $backends) |
45
|
|
|
{ |
46
|
|
|
$this->queues = $queues; |
47
|
|
|
$this->backends = $backends; |
48
|
|
|
$this->defaultQueue = $defaultQueue; |
49
|
|
|
|
50
|
|
|
foreach ($this->backends as $backend) { |
51
|
|
|
$backend['backend']->setDispatcher($this); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function publish(MessageInterface $message): void |
59
|
|
|
{ |
60
|
|
|
$this->getBackend($message->getType())->publish($message); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function create($type, array $body) |
67
|
|
|
{ |
68
|
|
|
return $this->getBackend($type)->create($type, $body); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function createAndPublish($type, array $body): void |
75
|
|
|
{ |
76
|
|
|
$this->getBackend($type)->createAndPublish($type, $body); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
public function getQueues() |
83
|
|
|
{ |
84
|
|
|
return $this->queues; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|