Completed
Push — master ( f48699...0b66f1 )
by Michael
29s queued 22s
created

Message::setSubject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 9.4285
ccs 5
cts 5
cp 1
crap 1
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits of supporting
4
 developers from this source code or any supporting source code which is considered
5
 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
 * This is an Immutable data object. That means any changes to the data (state)
20
 * return a new object, while the internal state of the original object is preserved.
21
 *
22
 * All data is validated for type and value, and an exception is generated when
23
 * data on any operation for a property when it is not valid.
24
 *
25
 * The Message data object is used for message and mailer services
26
 *
27
 * @category  Xoops\Core\Service\Data
28
 * @package   Xoops\Core
29
 * @author    Richard Griffith <[email protected]>
30
 * @copyright 2018 XOOPS Project (https://xoops.org)
31
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
32
 * @link      https://xoops.org
33
 */
34
class Message
35
{
36
    /** @var string $subject the subject of the message, a non-empty string */
37
    protected $subject;
38
39
    /** @var string $body the body of the message, a non-empty string */
40
    protected $body;
41
42
    /** @var int $fromId the user id sending the message, a positive integer */
43
    protected $fromId;
44
45
    /** @var int $toId the user id to receive the message, a positive integer */
46
    protected $toId;
47
48
    /* assert messages */
49
    protected const MESSAGE_BODY    = 'Body must be specified';
50
    protected const MESSAGE_FROM    = 'From id must be a valid userid';
51
    protected const MESSAGE_SUBJECT = 'Subject must be specified';
52
    protected const MESSAGE_TO      = 'To Id must be a valid userid';
53
54
    /**
55
     * Message constructor.
56
     *
57
     * If an argument is null, the corresponding value will not be set. Values can be set
58
     * later with the with*() methods, but each will result in a new object.
59
     *
60
     * @param null|string $subject the subject of the message, a non-empty string
61
     * @param null|string $body    the body of the message, a non-empty string
62
     * @param null|int    $fromId  the user id sending the message, a positive integer
63
     * @param null|int    $toId    the user id to receive the message, a positive integer
64
     *
65
     * @throws \InvalidArgumentException
66
     */
67 17
    public function __construct(?string $subject = null, ?string $body = null, ?int $fromId = null, ?int $toId = null)
68
    {
69 17
        if (null!==$subject) {
70 4
            $subject = trim($subject);
71 4
            Assert::stringNotEmpty($subject, static::MESSAGE_SUBJECT);
72 3
            $this->subject = $subject;
73
        }
74 17
        if (null!==$body) {
75 4
            $body = trim($body);
76 4
            Assert::stringNotEmpty($body, static::MESSAGE_BODY);
77 3
            $this->body = $body;
78
        }
79 17
        if (null!==$fromId) {
80 6
            Assert::greaterThan($fromId, 0, static::MESSAGE_FROM);
81 4
            $this->fromId = $fromId;
82
        }
83 17
        if (null!==$toId) {
84 4
            Assert::greaterThan($toId, 0, static::MESSAGE_TO);
85 3
            $this->toId = $toId;
86
        }
87 17
    }
88
89
    /**
90
     * Return a new object with a the specified toId
91
     *
92
     * @param int $toId userid message is to
93
     *
94
     * @return Message
95
     *
96
     * @throws \InvalidArgumentException
97
     */
98 3
    public function withToId(int $toId) : Message
99
    {
100 3
        return new static($this->subject, $this->body, $this->fromId, $toId);
101
    }
102
103
    /**
104
     * Return a new object with a the specified fromId
105
     *
106
     * @param int $fromId userid message is from
107
     *
108
     * @return Message
109
     *
110
     * @throws \InvalidArgumentException
111
     */
112 3
    public function withFromId(int $fromId) : Message
113
    {
114 3
        return new static($this->subject, $this->body, $fromId, $this->toId);
115
    }
116
117
    /**
118
     * Return a new object with a the specified subject
119
     *
120
     * @param string $subject message subject
121
     *
122
     * @return Message
123
     *
124
     * @throws \InvalidArgumentException
125
     */
126 3
    public function withSubject(string $subject) : Message
127
    {
128 3
        return new static($subject, $this->body, $this->fromId, $this->toId);
129
    }
130
131
    /**
132
     * Return a new object with a the specified body
133
     *
134
     * @param string $body message body
135
     *
136
     * @return Message
137
     *
138
     * @throws \InvalidArgumentException
139
     */
140 3
    public function withBody(string $body) : Message
141
    {
142 3
        return new static($this->subject, $body, $this->fromId, $this->toId);
143
    }
144
145
    /**
146
     * getToId
147
     *
148
     * @return int the toId
149
     *
150
     * @throws \LogicException (property was not properly set before used)
151
     */
152 5
    public function getToId() : int
153
    {
154
        try {
155 5
            Assert::greaterThan($this->toId, 0, static::MESSAGE_TO);
156 3
        } catch (\InvalidArgumentException $e) {
157 3
            throw new \LogicException($e->getMessage(), $e->getCode(), $e);
158
        }
159 3
        return $this->toId;
160
    }
161
162
    /**
163
     * getFromId
164
     *
165
     * @return int the fromId
166
     *
167
     * @throws \LogicException (property was not properly set before used)
168
     */
169 5
    public function getFromId() : int
170
    {
171
        try {
172 5
            Assert::greaterThan($this->fromId, 0, static::MESSAGE_FROM);
173 1
        } catch (\InvalidArgumentException $e) {
174 1
            throw new \LogicException($e->getMessage(), $e->getCode(), $e);
175
        }
176 4
        return $this->fromId;
177
    }
178
179
    /**
180
     * getSubject
181
     *
182
     * @return string the message subject
183
     *
184
     * @throws \LogicException (property was not properly set before used)
185
     */
186 4
    public function getSubject() : string
187
    {
188
        try {
189 4
            Assert::stringNotEmpty($this->subject, static::MESSAGE_SUBJECT);
190 1
        } catch (\InvalidArgumentException $e) {
191 1
            throw new \LogicException($e->getMessage(), $e->getCode(), $e);
192
        }
193
194 3
        return $this->subject;
195
    }
196
197
    /**
198
     * getBody
199
     *
200
     * @return string the message body
201
     *
202
     * @throws \LogicException (property was not properly set before used)
203
     */
204 4
    public function getBody() : string
205
    {
206
        try {
207 4
            Assert::stringNotEmpty($this->body, static::MESSAGE_BODY);
208 1
        } catch (\InvalidArgumentException $e) {
209 1
            throw new \LogicException($e->getMessage(), $e->getCode(), $e);
210
        }
211 3
        return $this->body;
212
    }
213
}
214