AbstractMessageProcessor   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 42
dl 0
loc 98
ccs 39
cts 39
cp 1
rs 10
c 3
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getProcessedMessages() 0 3 1
A consume() 0 27 4
A ack() 0 14 2
A nack() 0 8 2
1
<?php
2
namespace NeedleProject\LaravelRabbitMq\Processor;
3
4
use PhpAmqpLib\Message\AMQPMessage;
5
use Psr\Log\LoggerAwareInterface;
6
use Psr\Log\LoggerAwareTrait;
7
8
/**
9
 * Class AbstractMessageProcessor
10
 *
11
 * @package NeedleProject\LaravelRabbitMq\Processor
12
 * @author  Adrian tilita <[email protected]>
13
 */
14
abstract class AbstractMessageProcessor implements MessageProcessorInterface, LoggerAwareInterface
15
{
16
    use LoggerAwareTrait;
17
18
    /**
19
     * @const string Key used on message to identify if we ack/nack via the child
20
     */
21
    const HANDLED_PROPERTY = 'handled_property';
22
23
    /**
24
     * @var int
25
     */
26
    private $messageCount = 0;
27
28
    /**
29
     * {@inheritdoc}
30
     * @param AMQPMessage $message
31
     */
32 6
    public function consume(AMQPMessage $message)
33
    {
34 6
        $this->messageCount++;
35
        try {
36 6
            $response = $this->processMessage($message);
37
            // Already ack/nack from inside the processor using
38
            // the protected methods ::ack / ::nack
39 5
            if (property_exists($message, self::HANDLED_PROPERTY)) {
40 1
                $this->logger->debug("Already handled!");
0 ignored issues
show
Bug introduced by
The method debug() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
                $this->logger->/** @scrutinizer ignore-call */ 
41
                               debug("Already handled!");

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
41 1
                return;
42
            }
43 5
            if ($response === true) {
44 3
                $this->ack($message);
45
            } else {
46 5
                $this->nack($message);
47
            }
48 1
        } catch (\Throwable $e) {
49 1
            $this->logger->error(
50
                sprintf(
51 1
                    "Could not process message, got %s from %s in %d for message: %s",
52 1
                    get_class($e) . '-' . $e->getMessage(),
53 1
                    (string)$e->getFile(),
54 1
                    (int)$e->getLine(),
55 1
                    (string)$message->getBody()
56
                )
57
            );
58 1
            $this->nack($message);
59
        }
60 6
    }
61
62
    /**
63
     * @param AMQPMessage $message
64
     */
65 3
    protected function ack(AMQPMessage $message)
66
    {
67
        try {
68 3
            $this->logger->debug(sprintf("Processed with success message %s", $message->getBody()));
69 3
            $message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);
0 ignored issues
show
Deprecated Code introduced by
The property PhpAmqpLib\Message\AMQPMessage::$delivery_info has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

69
            $message->delivery_info['channel']->basic_ack(/** @scrutinizer ignore-deprecated */ $message->delivery_info['delivery_tag']);
Loading history...
70 2
            $message->{self::HANDLED_PROPERTY} = true;
71 1
        } catch (\Throwable $e) {
72 1
            $this->logger->error(
73
                sprintf(
74 1
                    "Could not process message, got %s from %s in %d for message: %s",
75 1
                    get_class($e) . '-' . $e->getMessage(),
76 1
                    (string)$e->getFile(),
77 1
                    (int)$e->getLine(),
78 1
                    (string)$message->getBody()
79
                )
80
            );
81
        }
82 3
    }
83
84
    /**
85
     * @param AMQPMessage $message
86
     * @param bool $redeliver
87
     */
88 3
    protected function nack(AMQPMessage $message, bool $redeliver = true)
89
    {
90
        try {
91 3
            $this->logger->debug(sprintf("Did not processed with success message %s", $message->getBody()));
92 3
            $message->delivery_info['channel']->basic_nack($message->delivery_info['delivery_tag'], false, $redeliver);
0 ignored issues
show
Deprecated Code introduced by
The property PhpAmqpLib\Message\AMQPMessage::$delivery_info has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

92
            /** @scrutinizer ignore-deprecated */ $message->delivery_info['channel']->basic_nack($message->delivery_info['delivery_tag'], false, $redeliver);
Loading history...
93 2
            $message->{self::HANDLED_PROPERTY} = true;
94 1
        } catch (\Throwable $e) {
95 1
            $this->logger->debug(sprintf("Did not processed with success message %s", $message->getBody()));
96
        }
97 3
    }
98
99
    /**
100
     * @return int
101
     */
102 1
    public function getProcessedMessages(): int
103
    {
104 1
        return $this->messageCount;
105
    }
106
107
    /**
108
     * @param AMQPMessage $message
109
     * @return bool
110
     */
111
    abstract public function processMessage(AMQPMessage $message): bool;
112
}
113