Completed
Pull Request — master (#183)
by Beñat
10:30
created

Consumer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B execute() 0 26 3
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 PhpAmqpLib\Message\AMQPMessage;
21
22
class Consumer
23
{
24
    const MSG_ACK = 1;
25
    const MSG_REJECT = -1;
26
27
    private $eventSubscriber;
28
    private $messageName;
29
30
    public function __construct(AsyncEventSubscriber $eventSubscriber, string $messageName)
31
    {
32
        $this->eventSubscriber = $eventSubscriber;
33
        $this->messageName = $messageName;
34
    }
35
36
    public function execute(AMQPMessage $message)
37
    {
38
        $body = json_decode($message->body);
39
        if ($this->messageName !== $body->name) {
40
            return false;
41
        }
42
43
        try {
44
            $this->eventSubscriber->handle(
45
                new AsyncEvent(
46
                    $this->messageName,
47
                    new \DateTimeImmutable($body->occurredOn),
48
                    json_decode(
49
                        json_encode(
50
                            $body->values
51
                        ),
52
                        true
53
                    )
54
                )
55
            );
56
57
            return self::MSG_ACK;
58
        } catch (Exception $exception) {
59
            return self::MSG_REJECT;
60
        }
61
    }
62
}
63