CreateLabelInBulkForOrdersBuilder   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
eloc 22
c 2
b 0
f 0
dl 0
loc 60
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getMethod() 0 3 1
A isExpectXml() 0 3 1
A getXml() 0 16 3
A __construct() 0 7 1
A getHeaders() 0 7 2
A getUrl() 0 12 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Bpost\BpostApiClient\Bpost\HttpRequestBuilder;
5
6
use Bpost\BpostApiClient\Common\ApiVersions;
7
use Bpost\BpostApiClient\Common\ValidatedValue\LabelFormat;
8
use DOMDocument;
9
use DOMException;
10
11
class CreateLabelInBulkForOrdersBuilder implements HttpRequestBuilderInterface
12
{
13
    public function __construct(
14
        private readonly array $references,
15
        private readonly LabelFormat $labelFormat,
16
        private readonly bool $asPdf,
17
        private readonly bool $withReturnLabels,
18
        private readonly bool $forcePrinting,
19
    ) {}
20
21
    public function getXml(): string
22
    {
23
        $document = new DOMDocument('1.0', 'UTF-8');
24
        $document->preserveWhiteSpace = false;
25
        $document->formatOutput = true;
26
27
        $batchLabels = $document->createElement('batchLabels');
28
        $batchLabels->setAttribute('xmlns', 'http://schema.post.be/shm/deepintegration/v3/');
29
30
        foreach ($this->references as $reference) {
31
            $batchLabels->appendChild($document->createElement('order', (string) $reference));
32
        }
33
34
        $document->appendChild($batchLabels);
35
36
        return $document->saveXML() ?: '';
37
    }
38
39
    public function getHeaders(): array
40
    {
41
        $media = $this->asPdf ? 'pdf' : 'image';
42
43
        return [
44
            'Accept: application/vnd.bpost.shm-label-' . $media . '-' . ApiVersions::V3_4 . '+XML',
45
            'Content-Type: application/vnd.bpost.shm-labelRequest-' . ApiVersions::V3 . '+XML',
46
        ];
47
    }
48
49
    public function getUrl(): string
50
    {
51
        $url = '/labels/' . $this->labelFormat->getValue();
52
53
        if ($this->withReturnLabels) {
54
            $url .= '/withReturnLabels';
55
        }
56
        if ($this->forcePrinting) {
57
            $url .= '?forcePrinting=true';
58
        }
59
60
        return $url;
61
    }
62
63
    public function isExpectXml(): bool
64
    {
65
        return true;
66
    }
67
68
    public function getMethod(): string
69
    {
70
        return self::METHOD_POST;
71
    }
72
}