MessageMapper::toMessage()   D
last analyzed

Complexity

Conditions 19
Paths 2

Size

Total Lines 28
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 19

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 28
ccs 24
cts 24
cp 1
rs 4.9141
cc 19
eloc 24
nc 2
nop 1
crap 19

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace AMQPAL\Adapter\PhpAmqpLib;
4
5
use PhpAmqpLib\Message\AMQPMessage;
6
use AMQPAL\Adapter\Message;
7
use DateTime;
8
9
class MessageMapper
10
{
11
    /**
12
     * @var Message
13
     */
14
    protected $messagePrototype;
15
16
    /**
17
     * @return Message
18
     */
19 7
    public function getMessagePrototype()
20
    {
21 7
        if (!$this->messagePrototype) {
22 6
            $this->messagePrototype = new Message();
23
        }
24 7
        return $this->messagePrototype;
25
    }
26
27
    /**
28
     * @param Message $messagePrototype
29
     * @return $this
30
     */
31 2
    public function setMessagePrototype(Message $messagePrototype)
32
    {
33 2
        $this->messagePrototype = $messagePrototype;
34 2
        return $this;
35
    }
36
37
    /**
38
     * @param AMQPMessage $libMessage
39
     * @return Message
40
     * @throws \OutOfBoundsException
41
     */
42 6
    public function toMessage(AMQPMessage $libMessage)
43
    {
44 6
        $message = clone $this->getMessagePrototype();
45 6
        $message->setBody($libMessage->getBody());
46 6
        $message->setRoutingKey($libMessage->get($libMessage->has('routing_key') ? 'routing_key' : null));
47 6
        $message->setDeliveryTag($libMessage->has('delivery_tag') ? $libMessage->get('delivery_tag') : null);
48 6
        $message->setDeliveryMode($libMessage->has('delivery_mode') ? $libMessage->get('delivery_mode') : null);
49 6
        $message->setExchangeName($libMessage->has('exchange') ? $libMessage->get('exchange') : null);
50 6
        $message->setRedelivered($libMessage->has('redelivered') ? $libMessage->get('redelivered') : false);
51 6
        $message->setContentType($libMessage->has('content_type') ? $libMessage->get('content_type') : null);
52 6
        $message->setContentEncoding(
53 6
            $libMessage->has('content_encoding') ? $libMessage->get('content_encoding') : null
54
        );
55 6
        $message->setType($libMessage->has('type') ? $libMessage->get('type') : null);
56 6
        $message->setDateTime(
57 6
            $libMessage->has('timestamp') ? (new DateTime())->setTimestamp($libMessage->get('timestamp')) : null
58
        );
59 6
        $message->setPriority($libMessage->has('priority') ? $libMessage->get('priority') : null);
60 6
        $expiration = $libMessage->has('expiration') ? $libMessage->get('expiration') : null;
61 6
        $message->setExpiration(!empty($expiration) ? new \DateTime($libMessage->get('expiration')) : null);
62 6
        $message->setUserId($libMessage->has('user_id') ? $libMessage->get('user_id') : null);
63 6
        $message->setAppId($libMessage->has('app_id') ? $libMessage->get('app_id') : null);
64 6
        $message->setMessageId($libMessage->has('message_id') ? $libMessage->get('message_id') : null);
65 6
        $message->setReplyTo($libMessage->has('reply_to') ? $libMessage->get('reply_to') : null);
66 6
        $message->setCorrelationId($libMessage->has('correlation_id') ? $libMessage->get('correlation_id') : null);
67 6
        $message->setHeaders($libMessage->has('application_headers') ? $libMessage->get('application_headers') : []);
68 6
        return $message;
69
    }
70
}
71