PhpAmqpLibAMQPConsumer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B execute() 0 29 4
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Kreta\SharedKernel\Infrastructure\Messaging\AMQP\PhpAmqpLib;
16
17
use Kreta\SharedKernel\Domain\Event\AsyncEventSubscriber;
18
use Kreta\SharedKernel\Domain\Model\Exception;
19
use Kreta\SharedKernel\Event\AsyncEvent;
20
use Kreta\SharedKernel\Messaging\AMQP\AMQPConsumer;
21
use PhpAmqpLib\Message\AMQPMessage;
22
23
class PhpAmqpLibAMQPConsumer implements AMQPConsumer
24
{
25
    const MSG_ACK = 1;
26
    const MSG_REJECT = -1;
27
28
    private $eventSubscriber;
29
    private $messageName;
30
31
    public function __construct(AsyncEventSubscriber $eventSubscriber, string $messageName)
32
    {
33
        $this->eventSubscriber = $eventSubscriber;
34
        $this->messageName = $messageName;
35
    }
36
37
    public function execute($message) : int
38
    {
39
        if (!$message instanceof AMQPMessage) {
40
            return self::MSG_REJECT;
41
        }
42
        $body = json_decode($message->body);
43
        if ($this->messageName !== $body->name) {
44
            return self::MSG_REJECT;
45
        }
46
47
        try {
48
            $this->eventSubscriber->handle(
49
                new AsyncEvent(
50
                    $this->messageName,
51
                    new \DateTimeImmutable($body->occurredOn),
52
                    json_decode(
53
                        json_encode(
54
                            $body->values
55
                        ),
56
                        true
57
                    )
58
                )
59
            );
60
61
            return self::MSG_ACK;
62
        } catch (Exception $exception) {
63
            return self::MSG_REJECT;
64
        }
65
    }
66
}
67