Skip to content

Commit 578f5cb

Browse files
committed
Going all in on the meaning of status messages for result.
Renaming result::*status* to result::*error* Per https://www.drupal.org/node/2789237 #24 If a result is given, and the result has > 0 reports, and no error status, then success. See devel form
1 parent 1b6fc96 commit 578f5cb

File tree

6 files changed

+83
-51
lines changed

6 files changed

+83
-51
lines changed

modules/sms_devel/src/Form/SmsDevelMessageForm.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Drupal\sms\Entity\SmsMessage;
1212
use Drupal\sms\Direction;
1313
use Drupal\sms\Exception\SmsException;
14+
use Drupal\sms\Message\SmsMessageResultInterface;
1415

1516
/**
1617
* Simulate a message being sent or received.
@@ -73,6 +74,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
7374
'#required' => TRUE,
7475
];
7576

77+
$gateways = [];
7678
foreach (SmsGateway::loadMultiple() as $sms_gateway) {
7779
$gateways[$sms_gateway->id()] = $sms_gateway->label();
7880
}
@@ -165,8 +167,11 @@ function submitReceive(array &$form, FormStateInterface $form_state) {
165167
$this->message->setDirection(Direction::INCOMING);
166168

167169
if ($form_state->getValue('skip_queue')) {
168-
$this->smsProvider->incoming($this->message);
169-
drupal_set_message($this->t('Message received.'));
170+
$messages = $this->smsProvider->incoming($this->message);
171+
foreach ($messages as $message) {
172+
$result = $message->getResult();
173+
$this->resultMessage($result);
174+
}
170175
}
171176
else {
172177
$this->smsProvider->queue($this->message);
@@ -188,8 +193,11 @@ function submitSend(array &$form, FormStateInterface $form_state) {
188193

189194
try {
190195
if ($form_state->getValue('skip_queue')) {
191-
$this->smsProvider->send($this->message);
192-
drupal_set_message($this->t('Message sent.'));
196+
$messages = $this->smsProvider->send($this->message);
197+
foreach ($messages as $message) {
198+
$result = $message->getResult();
199+
$this->resultMessage($result);
200+
}
193201
}
194202
else {
195203
$this->smsProvider->queue($this->message);
@@ -208,4 +216,28 @@ function submitSend(array &$form, FormStateInterface $form_state) {
208216
*/
209217
function submitForm(array &$form, FormStateInterface $form_state) {}
210218

219+
/**
220+
* Output a status message for a result object.
221+
*
222+
* @param \Drupal\sms\Message\SmsMessageResultInterface $result
223+
* An SMS result object
224+
*/
225+
protected function resultMessage(SmsMessageResultInterface $result) {
226+
if ($status_code = $result->getError()) {
227+
$status_message = $result->getErrorMessage();
228+
drupal_set_message($this->t('A problem occurred while attempting to process message: (code: @code) @message', [
229+
'@code' => $status_code,
230+
'@message' => $status_message,
231+
]), 'error');
232+
}
233+
else if ($report_count = count($result->getReports())) {
234+
drupal_set_message($this->t('Message was processed, @count delivery reports were generated.', [
235+
'@count' => $report_count,
236+
]));
237+
}
238+
else {
239+
drupal_set_message($this->t('An unknown error occurred while attempting to process message. No result or reports were generated by the gateway.'), 'error');
240+
}
241+
}
242+
211243
}

src/Message/SmsMessageResult.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@
1515
class SmsMessageResult implements SmsMessageResultInterface {
1616

1717
/**
18-
* The status of the message, or NULL if unknown.
18+
* The error of the message, or NULL if unknown.
1919
*
2020
* @var string|NULL
2121
*/
22-
protected $status = NULL;
22+
protected $error = NULL;
2323

2424
/**
25-
* The status message as provided by the gateway API.
25+
* The error message as provided by the gateway API.
2626
*
2727
* @var string
2828
*/
29-
protected $statusMessage = '';
29+
protected $errorMessage = '';
3030

3131
/**
3232
* The message delivery reports.
@@ -56,30 +56,30 @@ class SmsMessageResult implements SmsMessageResultInterface {
5656
/**
5757
* {@inheritdoc}
5858
*/
59-
public function getStatus() {
60-
return $this->status;
59+
public function getError() {
60+
return $this->error;
6161
}
6262

6363
/**
6464
* {@inheritdoc}
6565
*/
66-
public function setStatus($status) {
67-
$this->status = $status;
66+
public function setError($error) {
67+
$this->error = $error;
6868
return $this;
6969
}
7070

7171
/**
7272
* {@inheritdoc}
7373
*/
74-
public function getStatusMessage() {
75-
return $this->statusMessage;
74+
public function getErrorMessage() {
75+
return $this->errorMessage;
7676
}
7777

7878
/**
7979
* {@inheritdoc}
8080
*/
81-
public function setStatusMessage($message) {
82-
$this->statusMessage = $message;
81+
public function setErrorMessage($message) {
82+
$this->errorMessage = $message;
8383
return $this;
8484
}
8585

src/Message/SmsMessageResultInterface.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,47 @@
1313
interface SmsMessageResultInterface {
1414

1515
/**
16-
* Gets the status of the message.
16+
* Gets the error of the message.
1717
*
1818
* @return string|NULL
19-
* A status code from \Drupal\sms\Message\SmsMessageResultStatus, or NULL if
20-
* unknown.
19+
* A error code from \Drupal\sms\Message\SmsMessageResultError, or NULL if
20+
* there was no error.
2121
*/
22-
public function getStatus();
22+
public function getError();
2323

2424
/**
25-
* Sets the status of the message.
25+
* Sets the error of the message.
2626
*
27-
* Usually a setting a status on a result indicates something went wrong with
27+
* Usually a setting an error on a result indicates something went wrong with
2828
* the entire transaction.
2929
*
30-
* @param string $status|NULL
31-
* A status code from \Drupal\sms\Message\SmsMessageResultStatus, or NULL if
30+
* @param string $error|NULL
31+
* A error code from \Drupal\sms\Message\SmsMessageResultError, or NULL if
3232
* unknown.
3333
*
3434
* @return $this
3535
* Returns this result object for chaining.
3636
*/
37-
public function setStatus($status);
37+
public function setError($error);
3838

3939
/**
40-
* Gets the status message.
40+
* Gets the error message.
4141
*
4242
* @return string
43-
* The status message as provided by the gateway API.
43+
* The error message as provided by the gateway API.
4444
*/
45-
public function getStatusMessage();
45+
public function getErrorMessage();
4646

4747
/**
48-
* Sets the status message.
48+
* Sets the error message.
4949
*
5050
* @param string $message
51-
* The status message as provided by the gateway API.
51+
* The error message as provided by the gateway API.
5252
*
5353
* @return $this
5454
* Returns this report object for chaining.
5555
*/
56-
public function setStatusMessage($message);
56+
public function setErrorMessage($message);
5757

5858
/**
5959
* Gets the delivery report for a particular recipient.

src/Tests/SmsFrameworkMessageTestTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,16 @@ public function testOptionsRemove() {
185185
* @covers ::setResult
186186
*/
187187
public function testResults() {
188-
$status_message = $this->getRandomGenerator()->string();
188+
$error_message = $this->getRandomGenerator()->string();
189189
$result = (new SmsMessageResult())
190-
->setStatusMessage($status_message);
190+
->setErrorMessage($error_message);
191191

192192
$sms_message = $this->createSmsMessage();
193193
$sms_message->setResult($result);
194194

195195
$result_actual = $sms_message->getResult();
196196
$this->assertSame($result, $result_actual);
197-
$this->assertSame($status_message, $result_actual->getStatusMessage());
197+
$this->assertSame($error_message, $result_actual->getErrorMessage());
198198
}
199199

200200
/**

tests/src/Unit/Message/SmsFrameworkResultUnitTest.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,37 @@
1717
class SmsFrameworkResultUnitTest extends UnitTestCase {
1818

1919
/**
20-
* Tests status.
20+
* Tests error.
2121
*
22-
* @covers ::getStatus
23-
* @covers ::setStatus
22+
* @covers ::getError
23+
* @covers ::setError
2424
*/
25-
public function testStatus() {
25+
public function testError() {
2626
$result = $this->createResult();
27-
$this->assertNull($result->getStatus(), 'Default value is NULL');
27+
$this->assertNull($result->getError(), 'Default value is NULL');
2828

29-
$status = $this->getRandomGenerator()->string();
30-
$return = $result->setStatus($status);
29+
$error = $this->getRandomGenerator()->string();
30+
$return = $result->setError($error);
3131

3232
$this->assertTrue($return instanceof SmsMessageResultInterface);
33-
$this->assertEquals($status, $result->getStatus());
33+
$this->assertEquals($error, $result->getError());
3434
}
3535

3636
/**
37-
* Tests status message.
37+
* Tests error message.
3838
*
39-
* @covers ::getStatusMessage
40-
* @covers ::setStatusMessage
39+
* @covers ::getErrorMessage
40+
* @covers ::setErrorMessage
4141
*/
42-
public function testStatusMessage() {
42+
public function testErrorMessage() {
4343
$result = $this->createResult();
44-
$this->assertEquals('', $result->getStatusMessage(), 'Default value is empty string');
44+
$this->assertEquals('', $result->getErrorMessage(), 'Default value is empty string');
4545

46-
$status_message = $this->getRandomGenerator()->string();
47-
$return = $result->setStatusMessage($status_message);
46+
$error_message = $this->getRandomGenerator()->string();
47+
$return = $result->setErrorMessage($error_message);
4848

4949
$this->assertTrue($return instanceof SmsMessageResultInterface);
50-
$this->assertEquals($status_message, $result->getStatusMessage());
50+
$this->assertEquals($error_message, $result->getErrorMessage());
5151
}
5252

5353
/**

tests/src/Unit/Message/SmsMessageTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class SmsMessageTest extends UnitTestCase {
2525
/**
2626
* Create a SMS message object for testing.
2727
*
28-
* @return \Drupal\Tests\sms\Unit\Message\TestSmsMessage
28+
* @return \Drupal\sms\Message\SmsMessageInterface
2929
*/
3030
protected function createSmsMessage() {
3131
return new TestSmsMessage();

0 commit comments

Comments
 (0)