Completed
Push — master ( 78c033...223eb1 )
by Michel
12s
created

CmsmsMessage::tariff()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

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