Completed
Push — master ( f55883...4285c1 )
by Thomas Mauro
13:12 queued 10:06
created

MessageMapper::toMessage()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 24
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
ccs 21
cts 21
cp 1
rs 8.9713
cc 2
eloc 21
nc 1
nop 1
crap 2
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 2
    public function getMessagePrototype()
20
    {
21 2
        if (!$this->messagePrototype) {
22 1
            $this->messagePrototype = new Message();
23
        }
24 2
        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 AMQPEnvelope $libMessage
39
     * @return Message
40
     */
41 1
    public function toMessage(AMQPEnvelope $libMessage)
42
    {
43 1
        $message = clone $this->getMessagePrototype();
44 1
        $message->setBody($libMessage->getBody());
45 1
        $message->setRoutingKey($libMessage->getRoutingKey());
46 1
        $message->setDeliveryTag($libMessage->getDeliveryTag());
47 1
        $message->setDeliveryMode($libMessage->getDeliveryMode());
48 1
        $message->setExchangeName($libMessage->getExchangeName());
49 1
        $message->setRedelivered($libMessage->isRedelivery());
50 1
        $message->setContentType($libMessage->getContentType());
51 1
        $message->setContentEncoding($libMessage->getContentEncoding());
52 1
        $message->setType($libMessage->getType());
53 1
        $message->setDateTime((new \DateTime())->setTimestamp($libMessage->getTimestamp()));
54 1
        $message->setPriority($libMessage->getPriority());
55 1
        $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 1
        $message->setUserId($libMessage->getUserId());
57 1
        $message->setAppId($libMessage->getAppId());
58 1
        $message->setMessageId($libMessage->getMessageId());
59 1
        $message->setReplyTo($libMessage->getReplyTo());
60 1
        $message->setCorrelationId($libMessage->getCorrelationId());
61 1
        $message->setHeaders($libMessage->getHeaders());
62
63 1
        return $message;
64
    }
65
}
66