AbstractMessageProcessor   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 42
dl 0
loc 98
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
    public function consume(AMQPMessage $message)
33
    {
34
        $this->messageCount++;
35
        try {
36
            $response = $this->processMessage($message);
37
            // Already ack/nack from inside the processor using
38
            // the protected methods ::ack / ::nack
39
            if (property_exists($message, self::HANDLED_PROPERTY)) {
40
                $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
                return;
42
            }
43
            if ($response === true) {
44
                $this->ack($message);
45
            } else {
46
                $this->nack($message);
47
            }
48
        } catch (\Throwable $e) {
49
            $this->logger->error(
50
                sprintf(
51
                    "Could not process message, got %s from %s in %d for message: %s",
52
                    get_class($e) . '-' . $e->getMessage(),
53
                    (string)$e->getFile(),
54
                    (int)$e->getLine(),
55
                    (string)$message->getBody()
56
                )
57
            );
58
            $this->nack($message);
59
        }
60
    }
61
62
    /**
63
     * @param AMQPMessage $message
64
     */
65
    protected function ack(AMQPMessage $message)
66
    {
67
        try {
68
            $this->logger->debug(sprintf("Processed with success message %s", $message->getBody()));
69
            $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: Will be removed in version 4.0, use one of getters to get delivery info. ( Ignorable by Annotation )

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

69
            /** @scrutinizer ignore-deprecated */ $message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
70
            $message->{self::HANDLED_PROPERTY} = true;
71
        } catch (\Throwable $e) {
72
            $this->logger->error(
73
                sprintf(
74
                    "Could not process message, got %s from %s in %d for message: %s",
75
                    get_class($e) . '-' . $e->getMessage(),
76
                    (string)$e->getFile(),
77
                    (int)$e->getLine(),
78
                    (string)$message->getBody()
79
                )
80
            );
81
        }
82
    }
83
84
    /**
85
     * @param AMQPMessage $message
86
     * @param bool $redeliver
87
     */
88
    protected function nack(AMQPMessage $message, bool $redeliver = true)
89
    {
90
        try {
91
            $this->logger->debug(sprintf("Did not processed with success message %s", $message->getBody()));
92
            $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: Will be removed in version 4.0, use one of getters to get delivery info. ( Ignorable by Annotation )

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

92
            $message->delivery_info['channel']->basic_nack(/** @scrutinizer ignore-deprecated */ $message->delivery_info['delivery_tag'], false, $redeliver);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

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