Completed
Push — master ( 8bdc28...591856 )
by Michel
08:10
created

CmsmsMessage::multipart()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 2
cts 2
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 6
nc 2
nop 2
crap 5
1
<?php
2
3
namespace NotificationChannels\Cmsms;
4
5
use NotificationChannels\Cmsms\Exceptions\InvalidMessage;
6
7
class CmsmsMessage
8
{
9
    /** @var string */
10
    protected $body;
11
12
    /** @var string */
13
    protected $originator;
14
15
    /** @var string */
16
    protected $reference;
17
18
    /** @var int */
19
    protected $tariff;
20
21
    /** @var int */
22
    protected $minimumNumberOfMessageParts;
23
24 20
    /** @var int */
25
    protected $maximumNumberOfMessageParts;
26 20
27 20
    /**
28
     * @param string $body
29
     */
30
    public function __construct($body = '')
31
    {
32
        $this->body($body);
33 20
    }
34
35 20
    /**
36
     * @param string $body
37 20
     * @return $this
38
     */
39
    public function body($body)
40
    {
41
        $this->body = trim($body);
42
43
        return $this;
44
    }
45 8
46
    /**
47 8
     * @param string|int $originator
48 2
     * @return $this
49
     * @throws InvalidMessage
50
     */
51 6
    public function originator($originator)
52
    {
53 6
        if (empty($originator) || strlen($originator) > 11) {
54
            throw InvalidMessage::invalidOriginator($originator);
55
        }
56
57
        $this->originator = (string) $originator;
58
59
        return $this;
60
    }
61 4
62
    /**
63 4
     * @param string $reference
64 3
     * @return $this
65
     * @throws InvalidMessage
66
     */
67 1
    public function reference($reference)
68
    {
69 1
        if (empty($reference) || strlen($reference) > 32 || ! ctype_alnum($reference)) {
70
            throw InvalidMessage::invalidReference($reference);
71
        }
72
73
        $this->reference = $reference;
74
75
        return $this;
76
    }
77 3
78
    /**
79 3
     * @param int $tariff Tariff in eurocent
80 2
     * @return $this
81
     * @throws InvalidMessage
82
     */
83 1
    public function tariff($tariff)
84
    {
85 1
        if (empty($tariff) || ! is_int($tariff)) {
86
            throw InvalidMessage::invalidTariff($tariff);
87
        }
88
89
        $this->tariff = $tariff;
90
91 10
        return $this;
92
    }
93 10
94 10
    /**
95 10
     * @param int $minimum
96 10
     * @param int $maximum
97 10
     * @return $this
98
     * @throws InvalidMessage
99
     */
100
    public function multipart($minimum, $maximum)
101
    {
102
        if (! is_int($minimum) || ! is_int($maximum) || $maximum > 8 || $minimum >= $maximum) {
103
            throw InvalidMessage::invalidMessageParts($minimum, $maximum);
104
        }
105 1
106
        $this->minimumNumberOfMessageParts = $minimum;
107 1
        $this->maximumNumberOfMessageParts = $maximum;
108
109
        return $this;
110
    }
111
112
    /**
113
     * @return array
114
     */
115
    public function toXmlArray()
116
    {
117
        return array_filter([
118
            'BODY' => $this->body,
119
            'FROM' => $this->originator,
120
            'REFERENCE' => $this->reference,
121
            'TARIFF' => $this->tariff,
122
            'MINIMUMNUMBEROFMESSAGEPARTS' => $this->minimumNumberOfMessageParts,
123
            'MAXIMUMNUMBEROFMESSAGEPARTS' => $this->maximumNumberOfMessageParts,
124
        ]);
125
    }
126
127
    /**
128
     * @param string $body
129
     * @return static
130
     */
131
    public static function create($body = '')
132
    {
133
        return new static($body);
134
    }
135
}
136