Completed
Pull Request — master (#575)
by Richard
06:57
created

Message::getFromId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 9.4285
ccs 5
cts 5
cp 1
crap 2
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
namespace Xoops\Core\Service\Data;
13
14
use Xmf\Assert;
15
16
/**
17
 * The Message data object is a minimal message from one user to another user
18
 *
19
 * Message data object used for message and mailer services
20
 *
21
 * All service providers should extend this class, and implement the relevant
22
 * contract interface
23
 *
24
 * @category  Xoops\Core\Service\Data
25
 * @package   Xoops\Core
26
 * @author    Richard Griffith <[email protected]>
27
 * @copyright 2018 XOOPS Project (https://xoops.org)
28
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
29
 * @link      https://xoops.org
30
 */
31
class Message
32
{
33
    /** @var int $fromId */
34
    protected $fromId = 0;
35
36
    /** @var int $toId */
37
    protected $toId = 0;
38
39
    /** @var string $subject */
40
    protected $subject = '';
41
42
    /** @var string $body */
43
    protected $body = '';
44
45
    /** @var string[] $assertMessages */
46
    protected $assertMessages = [
47
        'to'      => 'To Id must be a valid userid',
48
        'from'    => 'From id must be a valid userid',
49
        'subject' => 'Subject must be specified',
50
        'body'    => 'Body must be specified',
51
    ];
52
53
    /**
54
     * Message constructor.
55
     *
56
     * @param null|int    $toId    userid message is to
57
     * @param null|int    $fromId  userid message is from
58
     * @param null|string $subject message subject
59
     * @param null|string $body    message body
60
     *
61
     * @throws \InvalidArgumentException
62
     */
63 13
    public function __construct(?int $toId = null, ?int $fromId = null, ?string $subject = null, ?string $body = null)
64
    {
65 13
        if (null!==$toId) {
66 3
            $this->setToId($toId);
67
        }
68 13
        if (null!==$fromId) {
69 3
            $this->setFromId($fromId);
70
        }
71 13
        if (null!==$subject) {
72 2
            $this->setSubject($subject);
73
        }
74 13
        if (null!==$body) {
75 2
            $this->setBody($body);
76
        }
77 13
    }
78
79
    /**
80
     * Set to id
81
     *
82
     * @param int $toId userid message is to
83
     *
84
     * @return $this
85
     *
86
     * @throws \InvalidArgumentException
87
     */
88 4
    public function setToId(int $toId) : Message
89
    {
90 4
        Assert::greaterThan($toId, 0, $this->assertMessages['to']);
91 2
        $this->toId = $toId;
92 2
        return $this;
93
    }
94
95
    /**
96
     * Set from id
97
     *
98
     * @param int $fromId userid message is from
99
     *
100
     * @return $this
101
     *
102
     * @throws \InvalidArgumentException
103
     */
104 4
    public function setFromId(int $fromId) : Message
105
    {
106 4
        Assert::greaterThan($fromId, 0, $this->assertMessages['from']);
107 3
        $this->fromId = $fromId;
108 3
        return $this;
109
    }
110
111
    /**
112
     * Set subject
113
     *
114
     * @param string $subject message subject
115
     *
116
     * @return $this
117
     *
118
     * @throws \InvalidArgumentException
119
     */
120 3
    public function setSubject(string $subject) : Message
121
    {
122 3
        $subject = trim($subject);
123 3
        Assert::stringNotEmpty($subject, $this->assertMessages['subject']);
124 2
        $this->subject = $subject;
125 2
        return $this;
126
    }
127
128
    /**
129
     * Set subject
130
     *
131
     * @param string $body message body
132
     *
133
     * @return $this
134
     *
135
     * @throws \InvalidArgumentException
136
     */
137 3
    public function setBody(string $body) : Message
138
    {
139 3
        $body = trim($body);
140 3
        Assert::stringNotEmpty($body, $this->assertMessages['body']);
141 2
        $this->body = $body;
142 2
        return $this;
143
    }
144
145
    /**
146
     * getToId
147
     *
148
     * @return int the toId
149
     *
150
     * @throws \LogicException (property was not properly set)
151
     */
152 4
    public function getToId() : int
153
    {
154
        try {
155 4
            Assert::greaterThan($this->toId, 0, $this->assertMessages['to']);
156 2
        } catch (\InvalidArgumentException $e) {
157 2
            throw new \LogicException($e->getMessage(), $e->getCode(), $e);
158
        }
159 2
        return $this->toId;
160
    }
161
162
    /**
163
     * getFromId
164
     *
165
     * @return int the fromId
166
     *
167
     * @throws \LogicException (property was not properly set)
168
     */
169 4
    public function getFromId() : int
170
    {
171
        try {
172 4
            Assert::greaterThan($this->fromId, 0, $this->assertMessages['from']);
173 1
        } catch (\InvalidArgumentException $e) {
174 1
            throw new \LogicException($e->getMessage(), $e->getCode(), $e);
175
        }
176 3
        return $this->fromId;
177
    }
178
179
    /**
180
     * getSubject
181
     *
182
     * @return string the message subject
183
     *
184
     * @throws \LogicException (property was not properly set)
185
     */
186 3
    public function getSubject() : string
187
    {
188
        try {
189 3
            Assert::stringNotEmpty($this->subject, $this->assertMessages['subject']);
190 1
        } catch (\InvalidArgumentException $e) {
191 1
            throw new \LogicException($e->getMessage(), $e->getCode(), $e);
192
        }
193
194 2
        return $this->subject;
195
    }
196
197
    /**
198
     * getBody
199
     *
200
     * @return string the message body
201
     *
202
     * @throws \LogicException (property was not properly set)
203
     */
204 3
    public function getBody() : string
205
    {
206
        try {
207 3
            Assert::stringNotEmpty($this->body, $this->assertMessages['body']);
208 1
        } catch (\InvalidArgumentException $e) {
209 1
            throw new \LogicException($e->getMessage(), $e->getCode(), $e);
210
        }
211 2
        return $this->body;
212
    }
213
}
214