Insurance::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
namespace Bpost\BpostApiClient\Bpost\Order\Box\Option;
3
4
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
5
6
/**
7
 * bPost Insurance class
8
 *
9
 * @author    Tijs Verkoyen <[email protected]>
10
 * @version   3.0.0
11
 * @copyright Copyright (c), Tijs Verkoyen. All rights reserved.
12
 * @license   BSD License
13
 */
14
class Insurance extends Option
15
{
16
17
    const INSURANCE_TYPE_BASIC_INSURANCE = 'basicInsurance';
18
    const INSURANCE_TYPE_ADDITIONAL_INSURANCE = 'additionalInsurance';
19
20
    const INSURANCE_AMOUNT_UP_TO_2500_EUROS = 2;
21
    const INSURANCE_AMOUNT_UP_TO_5000_EUROS = 3;
22
    const INSURANCE_AMOUNT_UP_TO_7500_EUROS = 4;
23
    const INSURANCE_AMOUNT_UP_TO_10000_EUROS = 5;
24
    const INSURANCE_AMOUNT_UP_TO_12500_EUROS = 6;
25
    const INSURANCE_AMOUNT_UP_TO_15000_EUROS = 7;
26
    const INSURANCE_AMOUNT_UP_TO_17500_EUROS = 8;
27
    const INSURANCE_AMOUNT_UP_TO_20000_EUROS = 9;
28
    const INSURANCE_AMOUNT_UP_TO_22500_EUROS = 10;
29
    const INSURANCE_AMOUNT_UP_TO_25000_EUROS = 11;
30
31
    /**
32
     * @var string
33
     */
34
    private $type;
35
36
    /**
37
     * @var string
38
     */
39
    private $value;
40
41
    /**
42
     * @return array
43
     */
44 2
    public static function getPossibleTypeValues()
45
    {
46
        return array(
47 2
            self::INSURANCE_TYPE_BASIC_INSURANCE,
48 2
            self::INSURANCE_TYPE_ADDITIONAL_INSURANCE,
49 2
        );
50
    }
51
52
    /**
53
     * @param string $type
54
     * @throws BpostInvalidValueException
55
     */
56 2
    public function setType($type)
57
    {
58
59 2
        if (!in_array($type, self::getPossibleTypeValues())) {
60 1
            throw new BpostInvalidValueException('type', $type, self::getPossibleTypeValues());
61
        }
62
63 2
        $this->type = $type;
64 2
    }
65
66
    /**
67
     * @return string
68
     */
69 1
    public function getType()
70
    {
71 1
        return $this->type;
72
    }
73
74
    /**
75
     * @param string $value
76
     * @throws BpostInvalidValueException
77
     */
78 2
    public function setValue($value)
79
    {
80 2
        if (!in_array($value, self::getPossibleValueValues())) {
81 1
            throw new BpostInvalidValueException('value', $value, self::getPossibleValueValues());
82
        }
83
84 1
        $this->value = $value;
85 1
    }
86
87
    /**
88
     * @return string
89
     */
90 1
    public function getValue()
91
    {
92 1
        return $this->value;
93
    }
94
95
    /**
96
     * @return array
97
     */
98 2 View Code Duplication
    public static function getPossibleValueValues()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
    {
100
        return array(
101 2
            self::INSURANCE_AMOUNT_UP_TO_2500_EUROS,
102 2
            self::INSURANCE_AMOUNT_UP_TO_5000_EUROS,
103 2
            self::INSURANCE_AMOUNT_UP_TO_7500_EUROS,
104 2
            self::INSURANCE_AMOUNT_UP_TO_10000_EUROS,
105 2
            self::INSURANCE_AMOUNT_UP_TO_12500_EUROS,
106 2
            self::INSURANCE_AMOUNT_UP_TO_15000_EUROS,
107 2
            self::INSURANCE_AMOUNT_UP_TO_17500_EUROS,
108 2
            self::INSURANCE_AMOUNT_UP_TO_20000_EUROS,
109 2
            self::INSURANCE_AMOUNT_UP_TO_22500_EUROS,
110 2
            self::INSURANCE_AMOUNT_UP_TO_25000_EUROS,
111 2
        );
112
    }
113
114
    /**
115
     * @param string      $type
116
     * @param string|null $value
117
     *
118
     * @throws BpostInvalidValueException
119
     */
120 2
    public function __construct($type, $value = null)
121
    {
122 2
        $this->setType($type);
123 2
        if ($value !== null) {
124 2
            $this->setValue($value);
125 1
        }
126 1
    }
127
128
    /**
129
     * Return the object as an array for usage in the XML
130
     *
131
     * @param  \DomDocument $document
132
     * @param  string       $prefix
133
     * @return \DomElement
134
     */
135 1
    public function toXML(\DOMDocument $document, $prefix = 'common')
136
    {
137 1
        $tagName = 'insured';
138 1
        if ($prefix !== null) {
139 1
            $tagName = $prefix . ':' . $tagName;
140 1
        }
141 1
        $insured = $document->createElement($tagName);
142
143 1
        $tagName = $this->getType();
144 1
        if ($prefix !== null) {
145 1
            $tagName = $prefix . ':' . $tagName;
146 1
        }
147 1
        $insurance = $document->createElement($tagName);
148 1
        $insured->appendChild($insurance);
149
150 1
        if ($this->getValue() !== null) {
151 1
            $insurance->setAttribute('value', $this->getValue());
152 1
        }
153
154 1
        return $insured;
155
    }
156
}
157