diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..256abf4 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2015 Joe Archer + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..ee96c58 --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# Poosh-Laravel +Super simple facade to send push messages via a [Poosh](https://github.com/joearcher/poosh) server. + +### Setup +Require this package in composer.json and run `composer update` + +```javascript +"joearcher/pooshlaravel": "dev-master" +``` +After updating add the ServiceProvider the the providers array in `app/config/app.php` + +```php +'Joearcher\Pooshlaravel\PooshlaravelServiceProvider', +``` +And then you can add the facade to the Facades array + +```php +'Tomatoes' => 'Joearcher\Pooshlaravel\Facades\Poosh', +``` +Publish the config + +```console +artisan vendor:publish +``` + +This creates a `poosh.php` file in `app/config`, we recommend setting these options via your .env file + +`POOSH_SECRET` - This is the shared secret it needs to be the same as the one set on your Poosh server. +`POOSH_URL` - The full url including the protocol to your Poosh server, e.g. `http://pooshserver.blaa` +`POOSH_PORT` - The server port set on your Poosh server (Default is 1337). + + +## Usage +This facade currently provides one method `Poosh::send($event,$payload)` it requires 2 parameters, `$event` must be a `string`, this is the name of the event to fire on the client. `$payload` must be an `array` and is the payload to be sent to all clients listening for the event. + +````php + Poosh::send('message',['body' => 'This is a message'])'' +```` +## Thanks +Made possible by the awesome [Guzzle Http client](https://github.com/guzzle/guzzle) \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..76b06c8 --- /dev/null +++ b/composer.json @@ -0,0 +1,26 @@ +{ + "name": "joearcher/pooshlaravel", + "description": "Poosh push messaging facade for Laravel 5", + "license": "MIT", + "type": "API", + "keywords": [ + "Poosh", + "Push messaging" + ], + "require": { + "laravel/framework": "5.0.*", + "guzzlehttp/guzzle": "~5.2" + }, + "autoload": { + "psr-4": { + "Joearcher\\Pooshlaravel\\": "src/" + } + }, + "authors": [ + { + "name": "Joe Archer", + "email": "joe.archer@gmail.com" + } + ], + "minimum-stability": "dev" +} diff --git a/src/Facades/Poosh.php b/src/Facades/Poosh.php new file mode 100644 index 0000000..00fb394 --- /dev/null +++ b/src/Facades/Poosh.php @@ -0,0 +1,7 @@ + +* @copyright Copyright (c) 2015 +* @license http://www.opensource.org/licenses/mit-license.html MIT License +*/ + + +class Pooshlaravel{ + + /** + * Server Secret key from config + *@var string + **/ + + protected $secret; + + /** + * Base Poosh server url + *@var string + **/ + + protected $baseUrl; + + /** + *Instance of Guzzle client + *@var \GuzzleHttp\Client + **/ + + protected $client; + + /** + *Poosh server port number + *@var int + **/ + + protected $port; + + /** + *set everything up + *@return void + **/ + public function __construct() + { + $this->secret = config('poosh.secret'); + $this->port = config('poosh.server_port'); + $this->baseUrl = config('poosh.server_url'); + + if($this->secret == ""){ + $error = "You must set a secret in your config"; + throw new Exception($error); + } + + if($this->baseUrl== ""){ + $error = "You must set a server base URL in your config"; + throw new Exception($error); + } + + $this->client = new Guzz(['base_url' => $this->baseUrl]); + } + + /** + *Send payload to all clients listening for event + *@param String $event [name of the event] + *@param Array $payload [payload to be sent] + **/ + + public function send($event,$payload) + { + //check event is a string + if(!is_string($event)){ + $error = "Event must be a string"; + throw new Exception($error); + } + //check the payload is an array + if(!is_array($payload)){ + $error = "Payload must be an array"; + throw new Exception($error); + } + //encode the payload for transmission + $body = json_encode(['event' => $event,'payload' => $payload]); + + //set up the request + $request = $this->client->createRequest('POST','/send',['body' => $body]); + //set the Poosh server port + $request->setPort($this->port); + //set the headers + $request->setHeader('content-type','application/json'); + $request->setHeader('authorization',sha1($this->secret.json_encode($payload))); + + try + { + $this->client->send($request); + } + catch(RequestException $e) + { + echo $e->getRequest() . "\n"; + if ($e->hasResponse()) { + echo $e->getResponse() . "\n"; + } + } + } + +} \ No newline at end of file diff --git a/src/PooshlaravelServiceProvider.php b/src/PooshlaravelServiceProvider.php new file mode 100644 index 0000000..cc6517b --- /dev/null +++ b/src/PooshlaravelServiceProvider.php @@ -0,0 +1,29 @@ + +* @copyright Copyright (c) 2015 +* @license http://www.opensource.org/licenses/mit-license.html MIT License +*/ + +use Illuminate\Support\ServiceProvider; + +class PooshlaravelServiceProvider extends ServiceProvider { + + public function boot(){ + $this->publishes([ + __DIR__.'/config/config.php' => config_path('poosh.php'), + ]); + } + + public function register() + { + $this->app['pooshlaravel'] = $this->app->share(function ($app) + { + + $poosh = new Pooshlaravel(); + + return $poosh; + }); + } +} \ No newline at end of file diff --git a/src/config/config.php b/src/config/config.php new file mode 100644 index 0000000..9c2831f --- /dev/null +++ b/src/config/config.php @@ -0,0 +1,8 @@ + env('POOSH_SECRET'), + 'server_url' => env('POOSH_URL'), + 'server_port' => env('POOSH_PORT', 1337) +]; \ No newline at end of file