Label   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 83.33%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 19
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 143
rs 10
ccs 40
cts 48
cp 0.8333

11 Methods

Rating   Name   Duplication   Size   Complexity  
A addBarcode() 0 4 1
A getBarcode() 0 10 2
A setBarcodes() 0 4 1
A getBarcodes() 0 4 1
A setBytes() 0 4 1
A getBytes() 0 4 1
A setMimeType() 0 8 2
A getMimeType() 0 4 1
A getPossibleMimeTypeValues() 0 8 1
A output() 0 6 1
B createFromXML() 0 17 7
1
<?php
2
3
namespace Bpost\BpostApiClient\Bpost;
4
5
use Bpost\BpostApiClient\Bpost\Label\Barcode;
6
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
7
8
/**
9
 * bPost Label class
10
 *
11
 * @author Tijs Verkoyen <[email protected]>
12
 */
13
class Label
14
{
15
16
    const LABEL_MIME_TYPE_IMAGE_PNG = 'image/png';
17
    const LABEL_MIME_TYPE_IMAGE_PDF = 'image/pdf';
18
    const LABEL_MIME_TYPE_APPLICATION_PDF = 'application/pdf';
19
20
    /**
21
     * @var Barcode[]
22
     */
23
    private $barcodes;
24
25
    /**
26
     * @var string
27
     */
28
    private $mimeType;
29
30
    /**
31
     * @var string
32
     */
33
    private $bytes;
34
35
    /**
36
     * @param Barcode $barcode
37
     */
38 2
    public function addBarcode(Barcode $barcode)
39
    {
40 2
        $this->barcodes[] = $barcode;
41 2
    }
42
43
    /**
44
     * @return string
45
     */
46 2
    public function getBarcode()
47
    {
48 2
        if (is_array($this->getBarcodes())) {
49 2
            $barcode = current($this->getBarcodes());
50
51 2
            return $barcode->getBarcode();
52
        }
53
54
        return '';
55
    }
56
57
    /**
58
     * @param Barcode[] $barcodes
59
     */
60
    public function setBarcodes(array $barcodes)
61
    {
62
        $this->barcodes = $barcodes;
63
    }
64
65
    /**
66
     * @return Barcode[]
67
     */
68 2
    public function getBarcodes()
69
    {
70 2
        return $this->barcodes;
71
    }
72
73
    /**
74
     * @param string $bytes
75
     */
76 2
    public function setBytes($bytes)
77
    {
78 2
        $this->bytes = $bytes;
79 2
    }
80
81
    /**
82
     * @return string
83
     */
84 2
    public function getBytes()
85
    {
86 2
        return $this->bytes;
87
    }
88
89
    /**
90
     * @param string $mimeType
91
     * @throws BpostInvalidValueException
92
     */
93 3
    public function setMimeType($mimeType)
94
    {
95 3
        if (!in_array($mimeType, self::getPossibleMimeTypeValues())) {
96 1
            throw new BpostInvalidValueException('mimeType', $mimeType, self::getPossibleMimeTypeValues());
97
        }
98
99 2
        $this->mimeType = $mimeType;
100 2
    }
101
102
    /**
103
     * @return string
104
     */
105 2
    public function getMimeType()
106
    {
107 2
        return $this->mimeType;
108
    }
109
110
    /**
111
     * @return array
112
     */
113 3
    public static function getPossibleMimeTypeValues()
114
    {
115
        return array(
116 3
            self::LABEL_MIME_TYPE_IMAGE_PNG,
117 3
            self::LABEL_MIME_TYPE_IMAGE_PDF,
118 3
            self::LABEL_MIME_TYPE_APPLICATION_PDF,
119 3
        );
120
    }
121
122
    /**
123
     * Output the bytes directly to the screen
124
     */
125
    public function output()
126
    {
127
        header('Content-type: ' . $this->getMimeType());
128
        echo $this->getBytes();
129
        exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method output() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
130
    }
131
132
    /**
133
     * @param  \SimpleXMLElement $xml
134
     *
135
     * @return Label
136
     * @throws BpostInvalidValueException
137
     */
138 2
    public static function createFromXML(\SimpleXMLElement $xml)
139
    {
140 2
        $label = new Label();
141 2
        if (isset($xml->barcodeWithReference)) {
142 2
            foreach ($xml->barcodeWithReference as $barcodeWithReference) {
143 2
                $label->addBarcode(Barcode::createFromXML($barcodeWithReference));
144 2
            }
145 2
        }
146 2
        if (isset($xml->mimeType) && $xml->mimeType != '') {
147 2
            $label->setMimeType((string) $xml->mimeType);
148 2
        }
149 2
        if (isset($xml->bytes) && $xml->bytes != '') {
150 2
            $label->setBytes((string) base64_decode($xml->bytes));
151 2
        }
152
153 2
        return $label;
154
    }
155
}
156