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

CmsmsMessage   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 14
c 6
b 1
f 1
lcom 1
cbo 1
dl 0
loc 103
ccs 23
cts 23
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A body() 0 6 1
A originator() 0 10 3
A reference() 0 10 4
A tariff() 0 10 3
A toXmlArray() 0 9 1
A create() 0 4 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 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