Completed
Push — master ( 6db363...e3ebaa )
by Michel
11s
created

CmsmsMessage::originator()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

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