Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
joearcher committed Mar 25, 2015
0 parents commit 7f0ca8d
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 0 deletions.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.

40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
26 changes: 26 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
}
],
"minimum-stability": "dev"
}
7 changes: 7 additions & 0 deletions src/Facades/Poosh.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php namespace Joearcher\Pooshlaravel\Facades;

use Illuminate\Support\Facades\Facade;

class Poosh extends Facade{
protected static function getFacadeAccessor() { return 'pooshlaravel'; }
}
109 changes: 109 additions & 0 deletions src/Pooshlaravel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php namespace Joearcher\Pooshlaravel;

use \GuzzleHttp\Client as Guzz;
use \GuzzleHttp\Exception\RequestException;
use Exception;

/**
* @author Joe Archer <[email protected]>
* @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";
}
}
}

}
29 changes: 29 additions & 0 deletions src/PooshlaravelServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php namespace Joearcher\Pooshlaravel;

/**
* @author Joe Archer <[email protected]>
* @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;
});
}
}
8 changes: 8 additions & 0 deletions src/config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

return [

'secret' => env('POOSH_SECRET'),
'server_url' => env('POOSH_URL'),
'server_port' => env('POOSH_PORT', 1337)
];

0 comments on commit 7f0ca8d

Please sign in to comment.