Completed
Push — master ( cef50b...fa3cc9 )
by Thomas Mauro
02:58
created

MessageMapper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 57
ccs 0
cts 28
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B toMessage() 0 24 2
A getMessagePrototype() 0 7 2
A setMessagePrototype() 0 5 1
1
<?php
2
3
namespace AMQPAL\Adapter\AMQP;
4
5
use AMQPEnvelope;
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
    public function getMessagePrototype()
20
    {
21
        if (!$this->messagePrototype) {
22
            $this->messagePrototype = new Message();
23
        }
24
        return $this->messagePrototype;
25
    }
26
27
    /**
28
     * @param Message $messagePrototype
29
     * @return $this
30
     */
31
    public function setMessagePrototype(Message $messagePrototype)
32
    {
33
        $this->messagePrototype = $messagePrototype;
34
        return $this;
35
    }
36
37
    /**
38
     * @param AMQPEnvelope $libMessage
39
     * @return Message
40
     */
41
    public function toMessage(AMQPEnvelope $libMessage)
42
    {
43
        $message = clone $this->getMessagePrototype();
44
        $message->setBody($libMessage->getBody());
45
        $message->setRoutingKey($libMessage->getRoutingKey());
46
        $message->setDeliveryTag($libMessage->getDeliveryTag());
47
        $message->setDeliveryMode($libMessage->getDeliveryMode());
48
        $message->setExchangeName($libMessage->getExchangeName());
49
        $message->setRedelivered($libMessage->isRedelivery());
50
        $message->setContentType($libMessage->getContentType());
51
        $message->setContentEncoding($libMessage->getContentEncoding());
52
        $message->setType($libMessage->getType());
53
        $message->setDateTime((new \DateTime())->setTimestamp($libMessage->getTimestamp()));
54
        $message->setPriority($libMessage->getPriority());
55
        $message->setExpiration($libMessage->getExpiration() ? new \DateTime($libMessage->getExpiration()) : null);
0 ignored issues
show
Bug introduced by
It seems like $libMessage->getExpirati...getExpiration()) : null can be null; however, setExpiration() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
56
        $message->setUserId($libMessage->getUserId());
57
        $message->setAppId($libMessage->getAppId());
58
        $message->setMessageId($libMessage->getMessageId());
59
        $message->setReplyTo($libMessage->getReplyTo());
60
        $message->setCorrelationId($libMessage->getCorrelationId());
61
        $message->setHeaders($libMessage->getHeaders());
62
63
        return $message;
64
    }
65
}
66