Skip to content

Commit 0a219af

Browse files
Merge pull request #2 from mpociot/code-review
Code review
2 parents 362616b + e7d39e6 commit 0a219af

File tree

10 files changed

+127
-84
lines changed

10 files changed

+127
-84
lines changed

README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
# CMSMS notifications channel for Laravel 5.3
22

3+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/laravel-notification-channels/cmsms.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/cmsms)
4+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
5+
[![Build Status](https://img.shields.io/travis/laravel-notification-channels/cmsms/master.svg?style=flat-square)](https://travis-ci.org/laravel-notification-channels/cmsms)
6+
[![StyleCI](https://styleci.io/repos/xxxx/shield)](https://styleci.io/repos/xxxx)
7+
[![SensioLabsInsight](https://img.shields.io/sensiolabs/i/xxxxx.svg?style=flat-square)](https://insight.sensiolabs.com/projects/xxxx)
8+
[![Quality Score](https://img.shields.io/scrutinizer/g/laravel-notification-channels/cmsms.svg?style=flat-square)](https://scrutinizer-ci.com/g/laravel-notification-channels/cmsms)
9+
[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/laravel-notification-channels/cmsms/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/laravel-notification-channels/cmsms/?branch=master)
10+
[![Total Downloads](https://img.shields.io/packagist/dt/laravel-notification-channels/cmsms.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/cmsms)
11+
312
This package makes it easy to send [CMSMS messages](https://dashboard.onlinesmsgateway.com/docs) with Laravel 5.3.
413

514
## Contents
615

716
- [Requirements](#requirements)
817
- [Installation](#installation)
9-
- [Setting up your CMSMS account](#setting-up-your-CMSMS-account)
18+
- [Setting up your CMSMS account](#setting-up-your-cmsms-account)
1019
- [Usage](#usage)
20+
- [Available message methods](#available-message-methods)
1121
- [Changelog](#changelog)
1222
- [Testing](#testing)
1323
- [Security](#security)
@@ -52,7 +62,7 @@ Add your CMSMS Product Token and default originator (name or number of sender) t
5262
...
5363
```
5464

55-
Notice: The originator can contain a maximum of 11 alfanumeric characters.
65+
Notice: The originator can contain a maximum of 11 alphanumeric characters.
5666

5767
## Usage
5868

@@ -72,13 +82,29 @@ class VpsServerOrdered extends Notification
7282

7383
public function toCmsms($notifiable)
7484
{
75-
return (new CmsmsMessage("Your {$notifiable->service} was ordered!"));
85+
return CmsmsMessage::create("Your {$notifiable->service} was ordered!");
7686
}
7787
}
7888
```
7989

90+
91+
In order to let your Notification know which phone numer you are targeting, add the `routeNotificationForCmsms` method to your Notifiable model.
92+
8093
**Important note**: CMCMS requires the recipients phone number to be in international format. For instance: 0031612345678
8194

95+
```php
96+
public function routeNotificationForCmsms()
97+
{
98+
return '0031612345678';
99+
}
100+
```
101+
102+
### Available message methods
103+
104+
- `body('')`: Accepts a string value for the message body.
105+
- `originator('')`: Accepts a string value between 1 and 11 characters, used as the message sender name.
106+
- `reference('')`: Accepts a string value for your message reference. This information will be returned in a status report so you can match the message and it's status. Restrictions: 1 - 32 alphanumeric characters and reference will not work for demo accounts.
107+
82108
## Changelog
83109

84110
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

composer.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
},
2020
"require-dev": {
2121
"mockery/mockery": "^0.9.5",
22-
"phpunit/phpunit": "4.*"
22+
"phpunit/phpunit": "4.*",
23+
"orchestra/testbench": "^3.3"
2324
},
2425
"autoload": {
2526
"psr-4": {
@@ -36,7 +37,5 @@
3637
},
3738
"config": {
3839
"sort-packages": true
39-
},
40-
"minimum-stability": "dev",
41-
"prefer-stable": true
40+
}
4241
}

src/CmsmsChannel.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66

77
class CmsmsChannel
88
{
9-
/**
10-
* @var \NotificationChannels\Cmsms\CmsmsClient
11-
*/
9+
/** @var CmsmsClient */
1210
protected $client;
1311

1412
/**
@@ -20,19 +18,25 @@ public function __construct(CmsmsClient $client)
2018
}
2119

2220
/**
21+
* Send the given notification.
22+
*
2323
* @param mixed $notifiable
2424
* @param \Illuminate\Notifications\Notification $notification
2525
*
2626
* @throws \NotificationChannels\Cmsms\Exceptions\CouldNotSendNotification
2727
*/
2828
public function send($notifiable, Notification $notification)
2929
{
30+
if (! $recipient = $notifiable->routeNotificationFor('Cmsms')) {
31+
return;
32+
}
33+
3034
$message = $notification->toCmsms($notifiable);
3135

3236
if (is_string($message)) {
3337
$message = CmsmsMessage::create($message);
3438
}
3539

36-
$this->client->send($message);
40+
$this->client->send($message, $recipient);
3741
}
3842
}

src/CmsmsClient.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,19 @@
22

33
namespace NotificationChannels\Cmsms;
44

5-
use Exception;
65
use GuzzleHttp\Client as GuzzleClient;
6+
use Illuminate\Support\Arr;
77
use NotificationChannels\Cmsms\Exceptions\CouldNotSendNotification;
88
use SimpleXMLElement;
99

1010
class CmsmsClient
1111
{
1212
const GATEWAY_URL = 'https://sgw01.cm.nl/gateway.ashx';
1313

14-
/**
15-
* @var GuzzleClient
16-
*/
14+
/** @var GuzzleClient */
1715
protected $client;
1816

19-
/**
20-
* @var string
21-
*/
17+
/** @var string */
2218
protected $productToken;
2319

2420
/**
@@ -33,18 +29,17 @@ public function __construct(GuzzleClient $client, $productToken)
3329

3430
/**
3531
* @param CmsmsMessage $message
32+
* @param string $recipient
3633
* @throws CouldNotSendNotification
3734
*/
38-
public function send(CmsmsMessage $message)
35+
public function send(CmsmsMessage $message, $recipient)
3936
{
40-
if (empty($message->originator)) {
41-
$message->setOriginator(config('services.cmsms.originator'));
37+
if (is_null(Arr::get($message->toXmlArray(), 'FROM'))) {
38+
$message->originator(config('services.cmsms.originator'));
4239
}
4340

44-
$messageXml = $this->buildMessageXml($message);
45-
4641
$response = $this->client->request('POST', static::GATEWAY_URL, [
47-
'body' => $messageXml,
42+
'body' => $this->buildMessageXml($message, $recipient),
4843
'headers' => [
4944
'Content-Type' => 'application/xml',
5045
],
@@ -60,19 +55,21 @@ public function send(CmsmsMessage $message)
6055

6156
/**
6257
* @param CmsmsMessage $message
58+
* @param string $recipient
6359
* @return string
6460
*/
65-
protected function buildMessageXml(CmsmsMessage $message)
61+
protected function buildMessageXml(CmsmsMessage $message, $recipient)
6662
{
6763
$xml = new SimpleXMLElement('<MESSAGES/>');
6864

69-
$authentication = $xml->addChild('AUTHENTICATION');
70-
$authentication->addChild('PRODUCTTOKEN', $this->productToken);
65+
$xml->addChild('AUTHENTICATION')
66+
->addChild('PRODUCTTOKEN', $this->productToken);
7167

7268
$msg = $xml->addChild('MSG');
7369
foreach ($message->toXmlArray() as $name => $value) {
7470
$msg->addChild($name, $value);
7571
}
72+
$msg->addChild('TO', $recipient);
7673

7774
return $xml->asXML();
7875
}

src/CmsmsMessage.php

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,28 @@
66

77
class CmsmsMessage
88
{
9-
/**
10-
* @var string
11-
*/
12-
public $body;
9+
/** @var string */
10+
protected $body;
1311

14-
/**
15-
* @var string
16-
*/
17-
public $originator;
12+
/** @var string */
13+
protected $originator;
1814

19-
/**
20-
* @var string
21-
*/
22-
public $recipient;
23-
24-
/**
25-
* @var string
26-
*/
27-
public $reference;
15+
/** @var string */
16+
protected $reference;
2817

2918
/**
3019
* @param string $body
3120
*/
3221
public function __construct($body = '')
3322
{
34-
if (!empty($body)) {
35-
$this->setBody($body);
36-
}
23+
$this->body($body);
3724
}
3825

3926
/**
4027
* @param string $body
4128
* @return $this
4229
*/
43-
public function setBody($body)
30+
public function body($body)
4431
{
4532
$this->body = trim($body);
4633

@@ -50,21 +37,15 @@ public function setBody($body)
5037
/**
5138
* @param string|int $originator
5239
* @return $this
40+
* @throws InvalidMessage
5341
*/
54-
public function setOriginator($originator)
42+
public function originator($originator)
5543
{
56-
$this->originator = (string) $originator;
57-
58-
return $this;
59-
}
44+
if (empty($originator) || strlen($originator) > 11) {
45+
throw InvalidMessage::invalidOriginator($originator);
46+
}
6047

61-
/**
62-
* @param string|int $recipient
63-
* @return $this
64-
*/
65-
public function setRecipient($recipient)
66-
{
67-
$this->recipient = (string) $recipient;
48+
$this->originator = (string) $originator;
6849

6950
return $this;
7051
}
@@ -74,7 +55,7 @@ public function setRecipient($recipient)
7455
* @return $this
7556
* @throws InvalidMessage
7657
*/
77-
public function setReference($reference)
58+
public function reference($reference)
7859
{
7960
if (empty($reference) || strlen($reference) > 32 || !ctype_alnum($reference)) {
8061
throw InvalidMessage::invalidReference($reference);
@@ -93,7 +74,6 @@ public function toXmlArray()
9374
return array_filter([
9475
'BODY' => $this->body,
9576
'FROM' => $this->originator,
96-
'TO' => $this->recipient,
9777
'REFERENCE' => $this->reference,
9878
]);
9979
}

src/Exceptions/CouldNotSendNotification.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class CouldNotSendNotification extends Exception
1010
* @param string $error
1111
* @return static
1212
*/
13-
public static function serviceRespondedWithAnError(string $error)
13+
public static function serviceRespondedWithAnError($error)
1414
{
1515
return new static("CMSMS service responded with an error: {$error}'");
1616
}

src/Exceptions/InvalidMessage.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,13 @@ public static function invalidReference($reference)
1414
{
1515
return new static("The reference on the CMSMS message may only contain 1 - 32 alphanumeric characters. Was given '{$reference}'");
1616
}
17+
18+
/**
19+
* @param string $originator
20+
* @return static
21+
*/
22+
public static function invalidOriginator($originator)
23+
{
24+
return new static("The originator on the CMSMS message may only contain 1 - 11 characters. Was given '{$originator}'");
25+
}
1726
}

tests/CmsmsChannelTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ class TestNotification extends Notification
5757
{
5858
public function toCmsms($notifiable)
5959
{
60-
return (new CmsmsMessage('Message content'))->setOriginator('APPNAME')->setRecipient('0031612345678');
60+
return (new CmsmsMessage('Message content'))->originator('APPNAME');
6161
}
6262
}

tests/CmsmsClientTest.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88
use NotificationChannels\Cmsms\CmsmsClient;
99
use NotificationChannels\Cmsms\CmsmsMessage;
1010
use NotificationChannels\Cmsms\Exceptions\CouldNotSendNotification;
11+
use Orchestra\Testbench\TestCase;
1112
use PHPUnit_Framework_TestCase;
1213

13-
class CmsmsClientTest extends PHPUnit_Framework_TestCase
14+
class CmsmsClientTest extends TestCase
1415
{
1516
public function setUp()
1617
{
18+
parent::setUp();
19+
$this->app['config']['services.cmsms.originator'] = 'My App';
1720
$this->guzzle = Mockery::mock(new Client());
1821
$this->client = Mockery::mock(new CmsmsClient($this->guzzle, '00000FFF-0000-F0F0-F0f0-FFFFFFFFFFFF'));
19-
$this->message = (new CmsmsMessage('Message content'))->setOriginator('APPNAME')->setRecipient('0031612345678');
22+
$this->message = (new CmsmsMessage('Message content'))->originator('APPNAME');
2023
}
2124

2225
public function tearDown()
@@ -38,9 +41,25 @@ public function it_can_send_message()
3841
$this->guzzle
3942
->shouldReceive('request')
4043
->once()
41-
->andReturn(new Response(200, [], 'error'));
44+
->andReturn(new Response(200, [], ''));
45+
46+
$this->client->send($this->message, '00301234');
47+
}
48+
49+
/** @test */
50+
public function it_sets_a_default_originator_if_none_is_set()
51+
{
52+
$message = Mockery::mock(new CmsmsMessage('Message body'));
53+
$message->shouldReceive('originator')
54+
->once()
55+
->with($this->app['config']['services.cmsms.originator']);
56+
57+
$this->guzzle
58+
->shouldReceive('request')
59+
->once()
60+
->andReturn(new Response(200, [], ''));
4261

43-
$this->client->send($this->message);
62+
$this->client->send($message, '00301234');
4463
}
4564

4665
/** @test */
@@ -53,6 +72,6 @@ public function it_throws_exception_on_error_response()
5372
->once()
5473
->andReturn(new Response(200, [], 'error'));
5574

56-
$this->client->send($this->message);
75+
$this->client->send($this->message, '00301234');
5776
}
5877
}

0 commit comments

Comments
 (0)