CmsmsMessage::toXmlArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 8
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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
    private function __construct(string $body = '')
33
    {
34 18
        $this->body($body);
35 18
    }
36
37
    /**
38
     * @param string $body
39
     *
40
     * @return $this
41 18
     */
42
    public function body(string $body)
43 18
    {
44
        $this->body = trim($body);
45 18
46
        return $this;
47
    }
48
49
    /**
50
     * @param string|int $originator
51
     *
52
     * @throws InvalidMessage
53 6
     *
54
     * @return $this
55 6
     */
56 2
    public function originator($originator)
57
    {
58
        if (empty($originator) || strlen($originator) > 11) {
59 4
            throw InvalidMessage::invalidOriginator($originator);
60
        }
61 4
62
        $this->originator = (string) $originator;
63
64
        return $this;
65
    }
66
67
    /**
68
     * @param string $reference
69 4
     *
70
     * @throws InvalidMessage
71 4
     *
72 3
     * @return $this
73
     */
74
    public function reference(string $reference)
75 1
    {
76
        if (empty($reference) || strlen($reference) > 32 || !ctype_alnum($reference)) {
77 1
            throw InvalidMessage::invalidReference($reference);
78
        }
79
80
        $this->reference = $reference;
81
82
        return $this;
83
    }
84
85 3
    /**
86
     * @param int $tariff Tariff in eurocent
87 3
     *
88
     * @throws InvalidMessage
89 3
     *
90
     * @return $this
91
     */
92
    public function tariff(int $tariff)
93
    {
94
        $this->tariff = $tariff;
95 4
96
        return $this;
97 4
    }
98
99
    /**
100
     * @return int
101
     */
102
    public function getTariff(): int
103
    {
104
        return $this->tariff;
105
    }
106 1
107
    /**
108 1
     * @param int $minimum
109 1
     * @param int $maximum
110
     *
111
     * @throws InvalidMessage
112
     *
113
     * @return $this
114
     */
115
    public function multipart(int $minimum, int $maximum)
116
    {
117
        if ($maximum > 8 || $minimum >= $maximum) {
118
            throw InvalidMessage::invalidMultipart($minimum, $maximum);
119
        }
120
121 8
        $this->minimumNumberOfMessageParts = $minimum;
122
        $this->maximumNumberOfMessageParts = $maximum;
123 8
124 8
        return $this;
125 8
    }
126 8
127 8
    /**
128 8
     * @return array
129
     */
130
    public function toXmlArray(): array
131
    {
132
        return array_filter([
133
            'BODY'                        => $this->body,
134
            'FROM'                        => $this->originator,
135
            'REFERENCE'                   => $this->reference,
136 18
            'MINIMUMNUMBEROFMESSAGEPARTS' => $this->minimumNumberOfMessageParts,
137
            'MAXIMUMNUMBEROFMESSAGEPARTS' => $this->maximumNumberOfMessageParts,
138 18
        ]);
139
    }
140
141
    /**
142
     * @param string $body
143
     *
144
     * @return static
145
     */
146
    public static function create($body = ''): self
147
    {
148
        return new static($body);
149
    }
150
}
151