Completed
Push — master ( debbc0...855ad3 )
by Tilita
02:28
created

AbstractMessageProcessor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 53
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
processMessage() 0 1 ?
A consume() 0 25 3
A getProcessedMessages() 0 4 1
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
     * @var int
20
     */
21
    private $messageCount = 0;
22
23
    /**
24
     * {@inheritdoc}
25
     * @param AMQPMessage $message
26
     */
27 4
    public function consume(AMQPMessage $message)
28
    {
29 4
        $this->messageCount++;
30
        try {
31 4
            $response = $this->processMessage($message);
32 3
            if ($response === true) {
33 2
                $this->logger->debug(sprintf("Processed with success message %s", $message->getBody()));
34 2
                $message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);
35
            } else {
36 1
                $this->logger->debug(sprintf("Did not processed with success message %s", $message->getBody()));
37 3
                $message->delivery_info['channel']->basic_nack($message->delivery_info['delivery_tag'], false, true);
38
            }
39 1
        } catch (\Exception $e) {
40 1
            $this->logger->error(
41
                sprintf(
42 1
                    "Could not process message, got %s from %s in %d for message: %s",
43 1
                    get_class($e) . '-' . $e->getMessage(),
44 1
                    (string)$e->getFile(),
45 1
                    (int)$e->getLine(),
46 1
                    (string)$message->getBody()
47
                )
48
            );
49 1
            $message->delivery_info['channel']->basic_nack($message->delivery_info['delivery_tag'], false, true);
50
        }
51 4
    }
52
53
    /**
54
     * @return int
55
     */
56 1
    public function getProcessedMessages(): int
57
    {
58 1
        return $this->messageCount;
59
    }
60
61
    /**
62
     * @param AMQPMessage $message
63
     * @return bool
64
     */
65
    abstract public function processMessage(AMQPMessage $message): bool;
0 ignored issues
show
Coding Style introduced by
function processMessage() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
66
}
67