Skip to content

Commit 16dfbbc

Browse files
committed
Update using Guzzle instead of massive Lib
1 parent a14aa24 commit 16dfbbc

9 files changed

+68
-48
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
build
33
composer.phar
44
composer.lock
5+
/.idea

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"php": ">=5.6.4",
1616
"illuminate/notifications": "^5.3",
1717
"illuminate/support": "^5.1|^5.2|^5.3",
18-
"illuminate/queue": "^5.3@dev",
19-
"messagebird/php-rest-api": "^1.4"
18+
"illuminate/queue": "^5.3",
19+
"guzzlehttp/guzzle": "^6.2"
2020
},
2121
"require-dev": {
2222
"mockery/mockery": "^0.9.5",

src/Exceptions/CouldNotSendNotification.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,4 @@ public static function serviceRespondedWithAnError(Exception $exception)
1414
{
1515
return new static("MessageBird service responded with an error '{$exception->getCode()}: {$exception->getMessage()}'");
1616
}
17-
18-
/**
19-
* @return static
20-
*/
21-
public static function couldNotAuthenticate()
22-
{
23-
return new static('You have entered the wrong credentials.');
24-
}
25-
26-
/**
27-
* @return static
28-
*/
29-
public static function notEnoughCredits()
30-
{
31-
return new static('Not enough credits.');
32-
}
3317
}

src/MessagebirdClient.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,27 @@
33
namespace NotificationChannels\Messagebird;
44

55
use Exception;
6-
use MessageBird\Exceptions\AuthenticateException;
7-
use MessageBird\Exceptions\BalanceException;
6+
use GuzzleHttp\Client;
87
use NotificationChannels\Messagebird\Exceptions\CouldNotSendNotification;
9-
use MessageBird\Client;
108

119
class MessagebirdClient
1210
{
1311
protected $client;
12+
protected $access_key;
1413

15-
public function __construct(Client $client)
14+
/**
15+
* MessagebirdClient constructor.
16+
* @param Client $client
17+
* @param $access_key string API Key from Messagebird API
18+
*/
19+
public function __construct(Client $client, $access_key)
1620
{
1721
$this->client = $client;
22+
$this->access_key = $access_key;
1823
}
1924

2025
/**
21-
* Send the Message through MessageBird Client.
26+
* Send the Message
2227
* @param MessagebirdMessage $message
2328
* @throws CouldNotSendNotification
2429
*/
@@ -32,11 +37,12 @@ public function send(MessagebirdMessage $message)
3237
}
3338

3439
try {
35-
$this->client->messages->create($message);
36-
} catch (AuthenticateException $exception) {
37-
throw CouldNotSendNotification::couldNotAuthenticate();
38-
} catch (BalanceException $exception) {
39-
throw CouldNotSendNotification::notEnoughCredits();
40+
$this->client->request('POST', 'https://rest.messagebird.com/messages', [
41+
'body' => $message->toJson(),
42+
'headers' => [
43+
'Authorization' => 'AccessKey ' . $this->access_key
44+
],
45+
]);
4046
} catch (Exception $exception) {
4147
throw CouldNotSendNotification::serviceRespondedWithAnError($exception);
4248
}

src/MessagebirdMessage.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace NotificationChannels\Messagebird;
44

5-
use MessageBird\Objects\Message;
6-
7-
class MessagebirdMessage extends Message
5+
class MessagebirdMessage
86
{
7+
public $body;
8+
public $originator;
9+
public $recipients;
10+
911
public static function create($body = '')
1012
{
1113
return new static($body);
@@ -34,12 +36,17 @@ public function setOriginator($originator)
3436

3537
public function setRecipients($recipients)
3638
{
37-
if (is_string($recipients)) {
38-
$recipients = [$recipients];
39+
if (is_array($recipients)) {
40+
$recipients = implode(',', $recipients);
3941
}
4042

4143
$this->recipients = $recipients;
4244

4345
return $this;
4446
}
47+
48+
public function toJson()
49+
{
50+
return json_encode($this);
51+
}
4552
}

src/MessagebirdServiceProvider.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace NotificationChannels\Messagebird;
44

55
use Illuminate\Support\ServiceProvider;
6-
use MessageBird\Client;
6+
use GuzzleHttp\Client;
77
use NotificationChannels\Messagebird\Exceptions\InvalidConfiguration;
88

99
class MessagebirdServiceProvider extends ServiceProvider
@@ -22,9 +22,7 @@ public function boot()
2222
throw InvalidConfiguration::configurationNotSet();
2323
}
2424

25-
return new MessagebirdClient(new Client(
26-
$config['access_key']
27-
));
25+
return new MessagebirdClient(new Client(), $config['access_key']);
2826
});
2927
}
3028
}

tests/MessagebirdChannelTest.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace NotificationChannels\Messagebird\Test;
44

5+
use GuzzleHttp\Client;
56
use Illuminate\Notifications\Notifiable;
67
use Illuminate\Notifications\Notification;
7-
use MessageBird\Client;
88
use Mockery;
99
use NotificationChannels\Messagebird\MessagebirdChannel;
1010
use NotificationChannels\Messagebird\MessagebirdClient;
@@ -17,8 +17,8 @@ public function setUp()
1717
{
1818
$this->notification = new TestNotification;
1919
$this->notifiable = new TestNotifiable;
20-
$this->messagebirdClient = Mockery::mock(new Client('test_1234567890'));
21-
$this->client = Mockery::mock(new MessagebirdClient($this->messagebirdClient));
20+
$this->guzzle = Mockery::mock(new Client());
21+
$this->client = Mockery::mock(new MessagebirdClient($this->guzzle, 'test_ek1qBbKbHoA20gZHM40RBjxzX'));
2222
$this->channel = new MessagebirdChannel($this->client);
2323
}
2424

@@ -34,6 +34,13 @@ public function it_can_be_instantiated()
3434
$this->assertInstanceOf(MessagebirdClient::class, $this->client);
3535
$this->assertInstanceOf(MessagebirdChannel::class, $this->channel);
3636
}
37+
38+
/** @test */
39+
public function test_it_shares_message()
40+
{
41+
$this->client->shouldReceive('send')->once();
42+
$this->channel->send($this->notifiable, $this->notification);
43+
}
3744
}
3845

3946
class TestNotifiable
@@ -42,14 +49,14 @@ class TestNotifiable
4249

4350
public function routeNotificationForMessagebird()
4451
{
45-
return '0031650520659';
52+
return '31650520659';
4653
}
4754
}
4855

4956
class TestNotification extends Notification
5057
{
5158
public function toMessagebird($notifiable)
5259
{
53-
return (new MessagebirdMessage('Message content'))->setOriginator('APPNAME')->setRecipients('0031650520659');
60+
return (new MessagebirdMessage('Message content'))->setOriginator('APPNAME')->setRecipients('31650520659');
5461
}
5562
}

tests/MessagebirdClientTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
namespace NotificationChannels\Messagebird\Test;
44

5-
use MessageBird\Client;
5+
use GuzzleHttp\Client;
66
use Mockery;
77
use NotificationChannels\Messagebird\MessagebirdClient;
8+
use NotificationChannels\Messagebird\MessagebirdMessage;
89
use PHPUnit_Framework_TestCase;
910

1011
class MessagebirdClientTest extends PHPUnit_Framework_TestCase
1112
{
1213
public function setUp()
1314
{
14-
$this->messagebirdClient = Mockery::mock(new Client('test_1234567890'));
15-
$this->client = Mockery::mock(new MessagebirdClient($this->messagebirdClient));
15+
$this->guzzle = Mockery::mock(new Client());
16+
$this->client = Mockery::mock(new MessagebirdClient($this->guzzle, 'test_ek1qBbKbHoA20gZHM40RBjxzX'));
17+
$this->message = (new MessagebirdMessage('Message content'))->setOriginator('APPNAME')->setRecipients('31650520659');
1618
}
1719

1820
public function tearDown()
@@ -25,5 +27,12 @@ public function tearDown()
2527
public function it_can_be_instantiated()
2628
{
2729
$this->assertInstanceOf(MessagebirdClient::class, $this->client);
30+
$this->assertInstanceOf(MessagebirdMessage::class, $this->message);
31+
}
32+
33+
/** @test */
34+
public function it_can_send_message()
35+
{
36+
$this->client->send($this->message);
2837
}
2938
}

tests/MessagebirdMessageTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,24 @@ public function it_can_set_originator()
5050
/** @test */
5151
public function it_can_set_recipients_from_array()
5252
{
53-
$message = (new MessagebirdMessage)->setRecipients([31650520659]);
53+
$message = (new MessagebirdMessage)->setRecipients([31650520659, 31599858770]);
5454

55-
$this->assertEquals([31650520659], $message->recipients);
55+
$this->assertEquals('31650520659,31599858770', $message->recipients);
56+
}
57+
58+
/** @test */
59+
public function it_can_set_recipients_from_integer()
60+
{
61+
$message = (new MessagebirdMessage)->setRecipients(31650520659);
62+
63+
$this->assertEquals(31650520659, $message->recipients);
5664
}
5765

5866
/** @test */
5967
public function it_can_set_recipients_from_string()
6068
{
6169
$message = (new MessagebirdMessage)->setRecipients('31650520659');
6270

63-
$this->assertEquals([31650520659], $message->recipients);
71+
$this->assertEquals('31650520659', $message->recipients);
6472
}
6573
}

0 commit comments

Comments
 (0)