2amigos /
mailer-library
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Da\Mailer\Queue; |
||
| 4 | |||
| 5 | use Da\Mailer\Builder\QueueBuilder; |
||
| 6 | use Da\Mailer\Model\MailMessage; |
||
| 7 | use Da\Mailer\Queue\Backend\AbstractQueueStoreConnection; |
||
| 8 | use Da\Mailer\Queue\Backend\MailJobInterface; |
||
| 9 | use Da\Mailer\Queue\Backend\QueueStoreAdapterInterface; |
||
| 10 | use Da\Mailer\Security\Cypher; |
||
| 11 | use Da\Mailer\Security\CypherInterface; |
||
| 12 | |||
| 13 | final class MailQueue implements QueueStoreAdapterInterface |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var QueueStoreAdapterInterface |
||
| 17 | */ |
||
| 18 | private $adapter; |
||
| 19 | /** |
||
| 20 | * @var CypherInterface|null |
||
| 21 | */ |
||
| 22 | private $cypher; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param QueueStoreAdapterInterface $adapter |
||
| 26 | 2 | */ |
|
| 27 | public function __construct(QueueStoreAdapterInterface $adapter) |
||
| 28 | 2 | { |
|
| 29 | 2 | $this->adapter = $adapter; |
|
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @return MailQueue |
||
| 34 | 1 | * @throws \Da\Mailer\Exception\UndefinedMessageBrokerException |
|
| 35 | */ |
||
| 36 | 1 | public static function make() |
|
| 37 | { |
||
| 38 | return QueueBuilder::make(); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | 1 | * @return AbstractQueueStoreConnection |
|
| 43 | */ |
||
| 44 | 1 | public function getConnection() |
|
| 45 | 1 | { |
|
| 46 | return $this->adapter->getConnection(); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | 2 | * @param CypherInterface $cypher |
|
| 51 | */ |
||
| 52 | 2 | public function setCypher(CypherInterface $cypher) |
|
| 53 | { |
||
| 54 | $this->cypher = $cypher; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | 2 | * @return CypherInterface |
|
| 59 | */ |
||
| 60 | 2 | public function getCypher() |
|
| 61 | 2 | { |
|
| 62 | 1 | return $this->cypher; |
|
| 63 | 1 | } |
|
| 64 | |||
| 65 | 2 | /** |
|
| 66 | * {@inheritdoc} |
||
| 67 | */ |
||
| 68 | public function enqueue(MailJobInterface $mailJob) |
||
| 69 | { |
||
| 70 | $message = $mailJob->getMessage(); |
||
| 71 | 2 | if (null !== $this->getCypher() && $message instanceof MailMessage) { |
|
| 72 | $mailJob->setMessage($this->getCypher()->encodeMailMessage($message)); |
||
| 73 | 2 | } |
|
| 74 | |||
| 75 | 2 | return $this->adapter->enqueue($mailJob); |
|
| 76 | 1 | } |
|
| 77 | 1 | ||
| 78 | /** |
||
| 79 | 2 | * {@inheritdoc} |
|
| 80 | */ |
||
| 81 | public function dequeue() |
||
| 82 | { |
||
| 83 | $mailJob = $this->adapter->dequeue(); |
||
| 84 | |||
| 85 | 2 | if (null !== $this->getCypher()) { |
|
| 86 | $mailJob->setMessage($this->getCypher()->decodeMailMessage($mailJob->getMessage())); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 87 | 2 | } |
|
| 88 | |||
| 89 | return $mailJob; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | 1 | * {@inheritdoc} |
|
| 94 | */ |
||
| 95 | 1 | public function init() |
|
| 96 | { |
||
| 97 | return $this->adapter->init(); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | 2 | * {@inheritdoc} |
|
| 102 | */ |
||
| 103 | 2 | public function ack(MailJobInterface $mailJob) |
|
| 104 | { |
||
| 105 | return $this->adapter->ack($mailJob); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | public function isEmpty() |
||
| 112 | { |
||
| 113 | return $this->adapter->isEmpty(); |
||
| 114 | } |
||
| 115 | } |
||
| 116 |