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

MessageMapper::getMessagePrototype()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
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 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 AMQPMessage $libMessage
39
     * @return Message
40
     * @throws \OutOfBoundsException
41
     */
42 1
    public function toMessage(AMQPMessage $libMessage)
43
    {
44 1
        $message = clone $this->getMessagePrototype();
45 1
        $message->setBody($libMessage->getBody());
46 1
        $message->setRoutingKey($libMessage->get($libMessage->has('routing_key') ? 'routing_key' : null));
47 1
        $message->setDeliveryTag($libMessage->has('delivery_tag') ? $libMessage->get('delivery_tag') : null);
48 1
        $message->setDeliveryMode($libMessage->has('delivery_mode') ? $libMessage->get('delivery_mode') : null);
49 1
        $message->setExchangeName($libMessage->has('exchange') ? $libMessage->get('exchange') : null);
50 1
        $message->setRedelivered($libMessage->has('redelivered') ? $libMessage->get('redelivered') : false);
51 1
        $message->setContentType($libMessage->has('content_type') ? $libMessage->get('content_type') : null);
52 1
        $message->setContentEncoding(
53 1
            $libMessage->has('content_encoding') ? $libMessage->get('content_encoding') : null
54
        );
55 1
        $message->setType($libMessage->has('type') ? $libMessage->get('type') : null);
56 1
        $message->setDateTime((new DateTime())->setTimestamp($libMessage->get('timestamp')));
57 1
        $message->setPriority($libMessage->has('priority') ? $libMessage->get('priority') : null);
58 1
        $message->setExpiration($libMessage->has('expiration') ? new \DateTime($libMessage->get('expiration')) : null);
0 ignored issues
show
Bug introduced by
It seems like $libMessage->has('expira...t('expiration')) : 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...
59 1
        $message->setUserId($libMessage->has('user_id') ? $libMessage->get('user_id') : null);
60 1
        $message->setAppId($libMessage->has('app_id') ? $libMessage->get('app_id') : null);
61 1
        $message->setMessageId($libMessage->has('message_id') ? $libMessage->get('message_id') : null);
62 1
        $message->setReplyTo($libMessage->has('reply_to') ? $libMessage->get('reply_to') : null);
63 1
        $message->setCorrelationId($libMessage->has('correlation_id') ? $libMessage->get('correlation_id') : null);
64 1
        $message->setHeaders($libMessage->has('application_headers') ? $libMessage->get('application_headers') : []);
65 1
        return $message;
66
    }
67
}
68