MessageAdapter::amqpMsg()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019-2023 © Philippe M./Irønie  <[email protected]>
5
 * For the full copyright and MIT license information, view the license file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Infrastructure;
11
12
use App\Domain\InfrastructurePorts\MessageInterface;
13
use Exception;
14
use PhpAmqpLib\Message\AMQPMessage;
15
16
/**
17
 * @unused
18
 * Class MessageAdapter
19
 *
20
 * @package App\Infrastructure
21
 */
22
class MessageAdapter implements MessageInterface
23
{
24
    private array $tempStorage = [];
25
26
    // todo: dependency inject AMQPAdapter, logAdapter, etc ?
27
    public function __construct()
28
    {
29
    }
30
31
    /**
32
     * A temporary exchange system.
33
     *
34
     * @param string $queue
35
     * @param        $message
36
     *
37
     * @throws Exception
38
     */
39
    public function send(string $queue, $message): void
40
    {
41
        // keep message in memory
42
        if ('test' === $queue) {
43
            dump('queue:test', $message);
44
        }
45
46
        // insert message in text file
47
        if ('ISBN invalide' === $queue) {
48
            $corpus = new CorpusAdapter();
49
            $corpus->addNewElementToCorpus('queue_ISBN invalide', $message);
50
51
            return;
52
        }
53
54
        // send message to AMQP server
55
        if ('rabbit' === $queue) {
56
            $this->amqpMsg($queue, $message);
57
58
            return;
59
        }
60
61
        //default
62
        $this->tempStorage[$queue][] = $message;
63
    }
64
65
    /**
66
     * todo : DI
67
     * AMQP server interface (with acknowledge and durable messages).
68
     */
69
    public function amqpMsg(string $queueName, string $data): void
70
    {
71
        $channel = ServiceFactory::queueChannel($queueName);
72
        $msg = new AMQPMessage(
73
            $data, ['delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT]
74
        );
75
        // set routing_key to queueName value when using the default '' exchange
76
        $channel->basic_publish($msg, '', $queueName);
77
78
        $channel->close();
79
    }
80
}
81