QueueWriter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 61
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A write() 0 6 2
A send() 0 13 2
1
<?php
2
3
namespace Cmp\Queues\Infrastructure\AWS\v20121105\Queue;
4
5
use Aws\Sns\SnsClient;
6
use Cmp\Queues\Domain\Queue\Exception\WriterException;
7
use Cmp\Queues\Domain\Queue\Message;
8
use Cmp\Queues\Domain\Queue\QueueWriter as DomainQueueWriter;
9
use Psr\Log\LoggerInterface;
10
11
class QueueWriter implements DomainQueueWriter
12
{
13
    /**
14
     * @var SnsClient
15
     */
16
    protected $sns;
17
18
    /**
19
     * @var string
20
     */
21
    private $topicArn;
22
23
    /**
24
     * @var LoggerInterface
25
     */
26
    protected $logger;
27
28
    /**
29
     * @param SnsClient       $sns
30
     * @param string          $topicArn
31
     * @param LoggerInterface $logger
32
     */
33
    public function __construct(SnsClient $sns, $topicArn, LoggerInterface $logger)
34
    {
35
        $this->sns = $sns;
36
        $this->topicArn = $topicArn;
37
        $this->logger = $logger;
38
    }
39
40
    /**
41
     * @param Message[] $messages
42
     *
43
     * @throws WriterException
44
     * @return null
45
     */
46
    public function write(array $messages)
47
    {
48
        foreach ($messages as $message) {
49
            $this->send($message);
50
        }
51
    }
52
53
    /**
54
     * @param Message $message
55
     *
56
     * @throws WriterException
57
     */
58
    protected function send(Message $message)
59
    {
60
        try {
61
            $this->sns->publish([
62
                'TopicArn'          => $this->topicArn,
63
                'Message'           => json_encode($message),
64
                'MessageAttributes' => $message->getAttributes()
65
            ]);
66
        } catch(\Exception $e) {
67
            $this->logger->error('Error writing messages', ['exception' => $e]);
68
            throw new WriterException($e->getMessage(), $e->getCode());
69
        }
70
    }
71
}