Completed
Push — master ( 223eb1...8bdc28 )
by Michel
03:12
created

CmsmsMessage::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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
    /**
22
     * @param string $body
23
     */
24 20
    public function __construct($body = '')
25
    {
26 20
        $this->body($body);
27 20
    }
28
29
    /**
30
     * @param string $body
31
     * @return $this
32
     */
33 20
    public function body($body)
34
    {
35 20
        $this->body = trim($body);
36
37 20
        return $this;
38
    }
39
40
    /**
41
     * @param string|int $originator
42
     * @return $this
43
     * @throws InvalidMessage
44
     */
45 8
    public function originator($originator)
46
    {
47 8
        if (empty($originator) || strlen($originator) > 11) {
48 2
            throw InvalidMessage::invalidOriginator($originator);
49
        }
50
51 6
        $this->originator = (string) $originator;
52
53 6
        return $this;
54
    }
55
56
    /**
57
     * @param string $reference
58
     * @return $this
59
     * @throws InvalidMessage
60
     */
61 4
    public function reference($reference)
62
    {
63 4
        if (empty($reference) || strlen($reference) > 32 || ! ctype_alnum($reference)) {
64 3
            throw InvalidMessage::invalidReference($reference);
65
        }
66
67 1
        $this->reference = $reference;
68
69 1
        return $this;
70
    }
71
72
    /**
73
     * @param int $tariff Tariff in eurocent
74
     * @return $this
75
     * @throws InvalidMessage
76
     */
77 3
    public function tariff($tariff)
78
    {
79 3
        if (empty($tariff) || ! is_int($tariff)) {
80 2
            throw InvalidMessage::invalidTariff($tariff);
81
        }
82
83 1
        $this->tariff = $tariff;
84
85 1
        return $this;
86
    }
87
88
    /**
89
     * @return array
90
     */
91 10
    public function toXmlArray()
92
    {
93 10
        return array_filter([
94 10
            'BODY' => $this->body,
95 10
            'FROM' => $this->originator,
96 10
            'REFERENCE' => $this->reference,
97 10
            'TARIFF' => $this->tariff,
98
        ]);
99
    }
100
101
    /**
102
     * @param string $body
103
     * @return static
104
     */
105 1
    public static function create($body = '')
106
    {
107 1
        return new static($body);
108
    }
109
}
110