JobExchangePublishMapper::mapConfig()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0054

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 8
cts 9
cp 0.8889
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 1
crap 2.0054
1
<?php
2
declare(strict_types=1);
3
namespace mcorten87\rabbitmq_api\mappers;
4
5
use mcorten87\rabbitmq_api\exceptions\WrongArgumentException;
6
use mcorten87\rabbitmq_api\jobs\JobBase;
7
use mcorten87\rabbitmq_api\jobs\JobExchangePublish;
8
use mcorten87\rabbitmq_api\objects\Method;
9
use mcorten87\rabbitmq_api\objects\Url;
10
use mcorten87\rabbitmq_api\services\MqManagementConfig;
11
12
class JobExchangePublishMapper extends BaseMapper
13
{
14
15 2
    protected function mapMethod() : Method
16
    {
17 2
        return new Method(Method::POST);
18
    }
19
20
    /**
21
     * @param JobBase $job
22
     * @return Url
23
     */
24 2
    protected function mapUrl(JobBase $job) : Url
25
    {
26 2
        if (!$job instanceof JobExchangePublish) {
27
            throw new WrongArgumentException($job, JobExchangePublish::class);
28
        }
29
30 2
        $url = 'exchanges';
31 2
        $url .= '/'.urlencode((string)$job->getVirtualHost());
32 2
        $url .= '/'.urlencode((string)$job->getExchangeName());
33 2
        $url .= '/publish';
34 2
        return new Url($url);
35
    }
36
37
    /**
38
     * @param JobBase $job
39
     * @return array
40
     */
41 2
    protected function mapConfig(JobBase $job) : array
42
    {
43 2
        if (!$job instanceof JobExchangePublish) {
44
            throw new WrongArgumentException($job, JobExchangePublish::class);
45
        }
46
47
        $body = [
48 2
            'payload' => (string)$job->getMessage(),
49 2
            'payload_encoding' => 'string', // TODO
50 2
            'routing_key' => (string)$job->getRoutingKey(),
51
            'properties' => [
52 2
                'delivery_mode' =>  $job->getDeliveryMode()->__toInt(),
53
                'headers' => []         // TODO
54
            ]
55
        ];
56
57 2
        return array_merge(parent::mapConfig($job), [
58 2
            'json'      =>  $body,
59
        ]);
60
    }
61
}
62