Messaging::toXML()   B
last analyzed

Complexity

Conditions 6
Paths 18

Size

Total Lines 37
Code Lines 23

Duplication

Lines 24
Ratio 64.86 %

Code Coverage

Tests 32
CRAP Score 6

Importance

Changes 0
Metric Value
dl 24
loc 37
ccs 32
cts 32
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 23
nc 18
nop 2
crap 6
1
<?php
2
namespace Bpost\BpostApiClient\Bpost\Order\Box\Option;
3
4
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException;
5
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
6
7
/**
8
 * bPost Messaging class
9
 *
10
 * @author    Tijs Verkoyen <[email protected]>
11
 * @version   3.0.0
12
 * @copyright Copyright (c), Tijs Verkoyen. All rights reserved.
13
 * @license   BSD License
14
 */
15
class Messaging extends Option
16
{
17
18
    const MESSAGING_LANGUAGE_EN = 'EN';
19
    const MESSAGING_LANGUAGE_NL = 'NL';
20
    const MESSAGING_LANGUAGE_FR = 'FR';
21
    const MESSAGING_LANGUAGE_DE = 'DE';
22
23
    const MESSAGING_TYPE_INFO_DISTRIBUTED = 'infoDistributed';
24
    const MESSAGING_TYPE_INFO_NEXT_DAY = 'infoNextDay';
25
    const MESSAGING_TYPE_INFO_REMINDER = 'infoReminder';
26
    const MESSAGING_TYPE_KEEP_ME_INFORMED = 'keepMeInformed';
27
28
    /**
29
     * @var string
30
     */
31
    private $type;
32
33
    /**
34
     * @var string
35
     */
36
    private $language;
37
38
    /**
39
     * @var string
40
     */
41
    private $emailAddress;
42
43
    /**
44
     * @var string
45
     */
46
    private $mobilePhone;
47
48
    /**
49
     * @param string $emailAddress
50
     * @throws BpostInvalidLengthException
51
     */
52 5 View Code Duplication
    public function setEmailAddress($emailAddress)
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...
53
    {
54 5
        $length = 50;
55 5
        if (mb_strlen($emailAddress) > $length) {
56 1
            throw new BpostInvalidLengthException('emailAddress', mb_strlen($emailAddress), $length);
57
        }
58
59 4
        $this->emailAddress = $emailAddress;
60 4
    }
61
62
    /**
63
     * @return string
64
     */
65 4
    public function getEmailAddress()
66
    {
67 4
        return $this->emailAddress;
68
    }
69
70
    /**
71
     * @param string $language
72
     * @throws BpostInvalidValueException
73
     */
74 7 View Code Duplication
    public function setLanguage($language)
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...
75
    {
76 7
        $language = strtoupper($language);
77
78 7
        if (!in_array($language, self::getPossibleLanguageValues())) {
79 1
            throw new BpostInvalidValueException('language', $language, self::getPossibleLanguageValues());
80
        }
81
82 7
        $this->language = $language;
83 7
    }
84
85
    /**
86
     * @return string
87
     */
88 4
    public function getLanguage()
89
    {
90 4
        return $this->language;
91
    }
92
93
    /**
94
     * @return array
95
     */
96 7
    public static function getPossibleLanguageValues()
97
    {
98
        return array(
99 7
            self::MESSAGING_LANGUAGE_EN,
100 7
            self::MESSAGING_LANGUAGE_NL,
101 7
            self::MESSAGING_LANGUAGE_FR,
102 7
            self::MESSAGING_LANGUAGE_DE,
103 7
        );
104
    }
105
106
    /**
107
     * @param string $mobilePhone
108
     * @throws BpostInvalidLengthException
109
     */
110 5 View Code Duplication
    public function setMobilePhone($mobilePhone)
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...
111
    {
112 5
        $length = 20;
113 5
        if (mb_strlen($mobilePhone) > $length) {
114 1
            throw new BpostInvalidLengthException('mobilePhone', mb_strlen($mobilePhone), $length);
115
        }
116
117 4
        $this->mobilePhone = $mobilePhone;
118 4
    }
119
120
    /**
121
     * @return string
122
     */
123 4
    public function getMobilePhone()
124
    {
125 4
        return $this->mobilePhone;
126
    }
127
128
    /**
129
     * @return array
130
     */
131 7
    public static function getPossibleTypeValues()
132
    {
133
        return array(
134 7
            self::MESSAGING_TYPE_INFO_DISTRIBUTED,
135 7
            self::MESSAGING_TYPE_INFO_NEXT_DAY,
136 7
            self::MESSAGING_TYPE_INFO_REMINDER,
137 7
            self::MESSAGING_TYPE_KEEP_ME_INFORMED,
138 7
        );
139
    }
140
141
    /**
142
     * @param string $type
143
     * @throws BpostInvalidValueException
144
     */
145 7
    public function setType($type)
146
    {
147
148 7
        if (!in_array($type, self::getPossibleTypeValues())) {
149 1
            throw new BpostInvalidValueException('type', $type, self::getPossibleTypeValues());
150
        }
151
152 7
        $this->type = $type;
153 7
    }
154
155
    /**
156
     * @return string
157
     */
158 4
    public function getType()
159
    {
160 4
        return $this->type;
161
    }
162
163
    /**
164
     * @param string $type
165
     * @param string $language
166
     * @param string|null $emailAddress
167
     * @param string|null $mobilePhone
168
     *
169
     * @throws BpostInvalidLengthException
170
     * @throws BpostInvalidValueException
171
     */
172 7
    public function __construct($type, $language, $emailAddress = null, $mobilePhone = null)
173
    {
174 7
        $this->setType($type);
175 7
        $this->setLanguage($language);
176
177 7
        if ($emailAddress !== null) {
178 4
            $this->setEmailAddress($emailAddress);
179 3
        }
180 7
        if ($mobilePhone !== null) {
181 4
            $this->setMobilePhone($mobilePhone);
182 3
        }
183 6
    }
184
185
    /**
186
     * Return the object as an array for usage in the XML
187
     *
188
     * @param  \DomDocument $document
189
     * @param  string       $prefix
190
     * @return \DomElement
191
     */
192 4
    public function toXML(\DOMDocument $document, $prefix = 'common')
193
    {
194 4
        $tagName = $this->getType();
195 4
        if ($prefix !== null) {
196 3
            $tagName = $prefix . ':' . $tagName;
197 3
        }
198
199 4
        $messaging = $document->createElement($tagName);
200 4
        $messaging->setAttribute('language', $this->getLanguage());
201
202 4 View Code Duplication
        if ($this->getEmailAddress() !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
203 3
            $tagName = 'emailAddress';
204 3
            if ($prefix !== null) {
205 2
                $tagName = $prefix . ':' . $tagName;
206 2
            }
207 3
            $messaging->appendChild(
208 3
                $document->createElement(
209 3
                    $tagName,
210 3
                    $this->getEmailAddress()
211 3
                )
212 3
            );
213 3
        }
214 4 View Code Duplication
        if ($this->getMobilePhone() !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
215 3
            $tagName = 'mobilePhone';
216 3
            if ($prefix !== null) {
217 2
                $tagName = $prefix . ':' . $tagName;
218 2
            }
219 3
            $messaging->appendChild(
220 3
                $document->createElement(
221 3
                    $tagName,
222 3
                    $this->getMobilePhone()
223 3
                )
224 3
            );
225 3
        }
226
227 4
        return $messaging;
228
    }
229
230
    /**
231
     * @param  \SimpleXMLElement $xml
232
     *
233
     * @return Messaging
234
     * @throws BpostInvalidLengthException
235
     */
236 2
    public static function createFromXML(\SimpleXMLElement $xml)
237
    {
238 2
        $messaging = new Messaging(
239 2
            $xml->getName(), (string) $xml->attributes()->language
240 2
        );
241
242 2
        $data = $xml->{$xml->getName()};
243 2
        if (isset($data->emailAddress) && $data->emailAddress != '') {
244 1
            $messaging->setEmailAddress((string) $data->emailAddress);
245 1
        }
246 2
        if (isset($data->mobilePhone) && $data->mobilePhone != '') {
247 1
            $messaging->setMobilePhone((string) $data->mobilePhone);
248 1
        }
249
250 2
        return $messaging;
251
    }
252
}
253