Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
Inject HTTP client and URL into senders instead of using facades
Browse files Browse the repository at this point in the history
  • Loading branch information
marfurt committed Jul 22, 2016
1 parent c1fa111 commit ef98686
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 124 deletions.
18 changes: 9 additions & 9 deletions config/fcm.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

return [
'driver' => env('FCM_PROTOCOL','http'),
'log_enabled' => true,
'driver' => env('FCM_PROTOCOL', 'http'),
'log_enabled' => true,

'http' => [
'server_key' => env('FCM_SERVER_KEY','Your FCM server key'),
'sender_id' => env('FCM_SENDER_ID', 'Your sender id'),
'server_send_url' => 'https://fcm.googleapis.com/fcm/send',
'server_group_url' => 'https://android.googleapis.com/gcm/notification',
'timeout' => 30.0, // in second
]
'http' => [
'server_key' => env('FCM_SERVER_KEY', 'Your FCM server key'),
'sender_id' => env('FCM_SENDER_ID', 'Your sender id'),
'server_send_url' => 'https://fcm.googleapis.com/fcm/send',
'server_group_url' => 'https://android.googleapis.com/gcm/notification',
'timeout' => 30.0, // in second
]
];
13 changes: 6 additions & 7 deletions src/FCMManager.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
<?php namespace LaravelFCM;


use GuzzleHttp\Client;
use Illuminate\Support\Manager;

class FCMManager extends Manager {

public function getDefaultDriver()
{
return $this->app['config']['fcm.driver'];
return $this->app[ 'config' ][ 'fcm.driver' ];
}

protected function createHttpDriver()
{
$config = $this->app['config']->get('fcm.http', []);
return new Client([
'timeout' => $config['timeout'],
]);
$config = $this->app[ 'config' ]->get('fcm.http', []);

return new Client([ 'timeout' => $config[ 'timeout' ] ]);
}

}
24 changes: 13 additions & 11 deletions src/FCMServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,28 @@ public function boot()

public function register()
{
$this->app->singleton('fcm.client', function($app) {
return (new FCMManager($app))->driver();
});

$this->app->bind('fcm.group', function($app) {
return new FCMGroup();
$client = $app[ 'fcm.client' ];
$url = $app[ 'config' ]->get('fcm.http.server_group_url');

return new FCMGroup($client, $url);
});

$this->app->bind('fcm.sender', function($app) {
return new FCMSender();
});

$this->registerClient();
}
$client = $app[ 'fcm.client' ];
$url = $app[ 'config' ]->get('fcm.http.server_send_url');

public function registerClient()
{
$this->app->singleton('fcm.client', function($app) {
return (new FCMManager($app))->driver();
return new FCMSender($client, $url);
});
}

protected function provide()
{
return [ 'fcm.group', 'fcm.sender', 'fcm.client' ];
return [ 'fcm.client', 'fcm.group', 'fcm.sender' ];
}

}
45 changes: 0 additions & 45 deletions src/Sender/BaseSender.php

This file was deleted.

11 changes: 1 addition & 10 deletions src/Sender/FCMGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* @package LaravelFCM\Sender
*/
class FCMGroup extends BaseSender {
class FCMGroup extends HTTPSender {

const CREATE = "create";
const ADD = "add";
Expand Down Expand Up @@ -96,13 +96,4 @@ public function isValidResponse(GuzzleResponse $response)
return $response->getReasonPhrase() != 'OK' || $response->getStatusCode() != 200;
}

/**
* get the url
*
* @return string
*/
protected function getUrl()
{
return $this->config['server_group_url'];
}
}
15 changes: 1 addition & 14 deletions src/Sender/FCMSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* @package LaravelFCM\Sender
*/
class FCMSender extends BaseSender {
class FCMSender extends HTTPSender {

const MAX_TOKEN_PER_REQUEST = 1000;

Expand All @@ -37,7 +37,6 @@ public function sendTo($to, Options $options = null, PayloadNotification $notifi
{
$response = null;


if (is_array($to) && !empty($to)) {

$partialTokens = array_chunk($to, self::MAX_TOKEN_PER_REQUEST, false);
Expand Down Expand Up @@ -96,25 +95,13 @@ public function sendToGroup($notificationKey, Options $options = null, PayloadNo
*/
public function sendToTopic(Topics $topics, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null)
{

$request = new Request(null, $options, $notification, $data, $topics);

$responseGuzzle = $this->post($request);

return new TopicResponse($responseGuzzle, $topics);
}


/**
* get the url
*
* @return string
*/
protected function getUrl()
{
return $this->config[ 'server_send_url' ];
}

/**
* @internal
*
Expand Down
38 changes: 38 additions & 0 deletions src/Sender/HTTPSender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php namespace LaravelFCM\Sender;

use GuzzleHttp\ClientInterface;

/**
* Class BaseSender
*
* @package LaravelFCM\Sender
*/
abstract class HTTPSender {

/**
* The client used to send messages.
*
* @var GuzzleHttp\ClientInterface
*/
protected $client;

/**
* The URL entry point.
*
* @var string
*/
protected $url;

/**
* Initializes a new sender object.
*
* @param GuzzleHttp\ClientInterface $client
* @param string $url
*/
public function __construct(ClientInterface $client, $url)
{
$this->client = $client;
$this->url = $url;
}

}
15 changes: 3 additions & 12 deletions tests/DownstreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ public function it_send_a_notification_to_a_device()

$client = Mockery::mock(Client::class);
$client->shouldReceive('post')->once()->andReturn($response);
$this->app->singleton('fcm.client', function($app) use($client) {
return $client;
});

$tokens = 'uniqueToken';

$fcm = new FCMSender();
$fcm = new FCMSender($client, 'http://test.test');
$fcm->sendTo($tokens);
}

Expand All @@ -56,16 +53,13 @@ public function it_send_a_notification_to_more_than_1000_devices()

$client = Mockery::mock(Client::class);
$client->shouldReceive('post')->times(10)->andReturn($response);
$this->app->singleton('fcm.client', function($app) use($client) {
return $client;
});

$tokens = [];
for ($i=0 ; $i<10000 ; $i++) {
$tokens[$i] = 'token_'.$i;
}

$fcm = new FCMSender();
$fcm = new FCMSender($client, 'http://test.test');
$fcm->sendTo($tokens);
}

Expand All @@ -91,11 +85,8 @@ public function an_empty_array_of_tokens_thrown_an_exception()

$client = Mockery::mock(Client::class);
$client->shouldReceive('post')->once()->andReturn($response);
$this->app->singleton('fcm.client', function($app) use($client) {
return $client;
});

$fcm = new FCMSender();
$fcm = new FCMSender($client, 'http://test.test');
$this->setExpectedException(\LaravelFCM\Response\Exceptions\InvalidRequestException::class);
$fcm->sendTo([]);
}
Expand Down
6 changes: 5 additions & 1 deletion tests/FCMTestCase.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php

use Illuminate\Foundation\Testing\TestCase;

abstract class FCMTestCase extends Illuminate\Foundation\Testing\TestCase {
abstract class FCMTestCase extends TestCase {

public function createApplication()
{
$app = require __DIR__.'/../vendor/laravel/laravel/bootstrap/app.php';

$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
$app->register(LaravelFCM\FCMServiceProvider::class);

$app['config']['fcm.driver'] = 'http';
$app['config']['fcm.http.timeout'] = 20;
$app['config']['fcm.http.server_send_url'] = 'http://test.test';
Expand All @@ -16,4 +19,5 @@ public function createApplication()

return $app;
}

}
3 changes: 0 additions & 3 deletions tests/GroupResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class GroupResponseTest extends FCMTestCase {
*/
public function it_construct_a_response_with_successes()
{

$notificationKey = "notificationKey";

$response = new \GuzzleHttp\Psr7\Response(200, [], '{
Expand All @@ -29,7 +28,6 @@ public function it_construct_a_response_with_successes()
*/
public function it_construct_a_response_with_failures()
{

$notificationKey = "notificationKey";

$response = new \GuzzleHttp\Psr7\Response(200, [], '{
Expand All @@ -55,7 +53,6 @@ public function it_construct_a_response_with_failures()
*/
public function it_construct_a_response_with_partials_failures()
{

$notificationKey = "notificationKey";

$response = new \GuzzleHttp\Psr7\Response(200, [], '{
Expand Down
14 changes: 2 additions & 12 deletions tests/TopicsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public function it_throw_an_exception_if_no_topic_is_provided()

$this->setExpectedException(NoTopicProvidedException::class);
$topics->build();

}

/**
Expand All @@ -32,7 +31,6 @@ public function it_has_only_one_topic()
$topics->topic('myTopic');

$this->assertEquals($target, $topics->build());

}

/**
Expand Down Expand Up @@ -115,12 +113,8 @@ public function it_send_a_notification_to_a_topic()

$client = Mockery::mock(Client::class);
$client->shouldReceive('post')->once()->andReturn($response);
$this->app->singleton('fcm.client', function($app) use($client) {
return $client;
});


$fcm = new FCMSender();
$fcm = new FCMSender($client, 'http://test.test');

$topics = new Topics();
$topics->topic('test');
Expand All @@ -141,12 +135,8 @@ public function it_send_a_notification_to_a_topic_and_return_error()

$client = Mockery::mock(Client::class);
$client->shouldReceive('post')->once()->andReturn($response);
$this->app->singleton('fcm.client', function($app) use($client) {
return $client;
});


$fcm = new FCMSender();
$fcm = new FCMSender($client, 'http://test.test');

$topics = new Topics();
$topics->topic('test');
Expand Down

0 comments on commit ef98686

Please sign in to comment.