├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── composer.lock ├── src ├── Exceptions │ ├── InvalidChannelException.php │ ├── InvalidConfigException.php │ ├── InvalidDeleteRequestException.php │ └── InvalidEventException.php ├── Pushman.php ├── PushmanBroadcaster.php └── PushmanServiceProvider.php └── tests ├── channel.txt ├── channels.txt ├── delete.txt ├── delete_array.txt ├── delete_half.txt ├── new_channel.txt └── push.txt /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | /node_modules 3 | /storage/database.sqlite 4 | /.idea 5 | /bin 6 | .phpstorm.meta.php 7 | .env 8 | test.php 9 | README.html -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | php: 3 | - 5.6 4 | - 5.5 5 | - 5.4 6 | - 7.0 7 | - hhvm 8 | 9 | matrix: 10 | allow_failures: 11 | - php: 7.0 12 | 13 | before_script: 14 | - composer self-update 15 | - composer install --prefer-source --no-interaction --dev -o 16 | 17 | script: 18 | - vendor/phpspec/phpspec/bin/phpspec run -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 George Miller 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pushman_php 2 | The Pushman PHP Library 3 | 4 | [![Latest Stable Version](https://poser.pugx.org/pushman/phplib/v/stable)](https://packagist.org/packages/pushman/phplib) [![Total Downloads](https://poser.pugx.org/pushman/phplib/downloads)](https://packagist.org/packages/pushman/phplib) [![Latest Unstable Version](https://poser.pugx.org/pushman/phplib/v/unstable)](https://packagist.org/packages/pushman/phplib) [![License](https://poser.pugx.org/pushman/phplib/license)](https://packagist.org/packages/pushman/phplib) 5 | 6 | ## Installation 7 | ```php 8 | composer require pushman/phplib 9 | ``` 10 | 11 | ## Usage 12 | ```php 13 | use Pushman\PHPLib\Pushman; 14 | $pushman = new Pushman('private-key-goes-here'); 15 | 16 | $response = $pushman->push('kittens_are_cute', 'public', ['foo' => 'asdasdasdasd']); 17 | ``` 18 | 19 | As of version 2.1.0 in Pushman, you can push to multiple channels by feeding an array into the channels variable. 20 | 21 | ```php 22 | use Pushman\PHPLib\Pushman; 23 | $pushman = new Pushman('private-key-goes-here'); 24 | 25 | $response = $pushman->push('kittens_are_cute', ['public', 'auth'], ['foo' => 'asdasdasdasd']); 26 | ``` 27 | 28 | On your own pushman instance: 29 | 30 | ```php 31 | use Pushman\PHPLib\Pushman; 32 | $pushman = new Pushman('private-key-goes-here, ['url' => 'http://pushman.yoursite.com']); 33 | 34 | $response = $pushman->push('kittens_are_cute', 'public', ['foo' => 'asdasdasdasd']); 35 | ``` 36 | 37 | `$response` will always return a JSON payload with `status` and `message` along with any other relevant information about your event. 38 | 39 | #### Extending Laravel 40 | You can also extend Laravels `event()` functionality by including the Pushman ServiceProvider in your `config/app.php` file. Add the service provider `Pushman\PHPLib\PushmanServiceProvider` in your app.php file and then in your `.env` file, add the following keys: 41 | 42 | ``` 43 | PUSHMAN_PRIVATE=60_char_private_token_here 44 | PUSHMAN_URL=http://pushman.yoursite.com 45 | ``` 46 | 47 | Later in your `config/broadcasting.php` file, add in under `connections` the Pushman settings: 48 | 49 | ```php 50 | 'pushman' => [ 51 | 'driver' => 'pushman', 52 | 'private' => env('PUSHMAN_PRIVATE'), 53 | 'url' => env('PUSHMAN_URL'), 54 | ], 55 | ``` 56 | 57 | From that point onwards, you can use `php artisan make:event {Name}` to make a Laravel Event, inside that event implement `ShouldBroadcast`, and in your `broadcastOn` function, return an array of channels you want to broadcast on. 58 | 59 | ##### Example 60 | `php artisan make:event UserCreated` - Called when a user is created. 61 | ```php 62 | $user = new User([ 63 | 'name' => 'James Duffleman', 64 | 'email' => 'george@duffleman.co.uk', 65 | 'password' => bcrypt('aPassword') 66 | ]); 67 | 68 | event(new UserCreated($user)); 69 | ``` 70 | 71 | ```php 72 | use Illuminate\Contracts\Broadcasting\ShouldBroadcast; 73 | 74 | class UserCreated extends Event implements ShouldBroadcast { 75 | 76 | public $user; 77 | 78 | public function __construct($user) 79 | { 80 | $this->user = $user; 81 | } 82 | 83 | public function broadcastOn() 84 | { 85 | return ['public']; 86 | } 87 | } 88 | ``` 89 | 90 | ### Getting Information 91 | 92 | Because Pushman can generate your public token every 60 minutes, updating your clients should be an automatic process. You can use the following code to grab the public token of any channel. 93 | 94 | ```php 95 | use Pushman\PHPLib\Pushman; 96 | $pushman = new Pushman('private-key-goes-here'); 97 | 98 | $response = $pushman->token('public'); 99 | $token = $response['token']; 100 | ``` 101 | 102 | And you can load all channel information by the `channels()` and `channel()` method. 103 | 104 | ```php 105 | use Pushman\PHPLib\Pushman; 106 | $pushman = new Pushman('private-key-goes-here'); 107 | 108 | $response = $pushman->channel('auth'); 109 | $max_connections = $response['max_connections']; 110 | 111 | $response = $pushman->channels(); 112 | foreach($response as $channel) { 113 | echo("Found channel {$channel['name']}.\n"); 114 | } 115 | ``` 116 | 117 | ## Todo for Future Versions 118 | * Add endpoint support for /api/subcribers. 119 | * Add some sort of trait/interface so you can append it to your existing user model and broadcast out easily to that specific user. -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pushman/phplib", 3 | "description": "The Pushman.dfl.mn PHP Library", 4 | "type": "library", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "George Miller", 9 | "email": "github@duffleman.co.uk" 10 | } 11 | ], 12 | "minimum-stability": "dev", 13 | "autoload": { 14 | "psr-4": { 15 | "Pushman\\PHPLib\\": "src/" 16 | } 17 | }, 18 | "require": { 19 | "guzzlehttp/guzzle": "^6.1" 20 | }, 21 | "config": { 22 | "bin-dir": "bin" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", 5 | "This file is @generated automatically" 6 | ], 7 | "hash": "4f66b959eb0604431ea85f14de4deae1", 8 | "content-hash": "2f817ce70525fc1fd15917602d86605e", 9 | "packages": [ 10 | { 11 | "name": "guzzlehttp/guzzle", 12 | "version": "dev-master", 13 | "source": { 14 | "type": "git", 15 | "url": "https://github.com/guzzle/guzzle.git", 16 | "reference": "739b9c8378c462943c42fee5353453ab4164ed8e" 17 | }, 18 | "dist": { 19 | "type": "zip", 20 | "url": "https://api.github.com/repos/guzzle/guzzle/zipball/739b9c8378c462943c42fee5353453ab4164ed8e", 21 | "reference": "739b9c8378c462943c42fee5353453ab4164ed8e", 22 | "shasum": "" 23 | }, 24 | "require": { 25 | "guzzlehttp/promises": "~1.0", 26 | "guzzlehttp/psr7": "~1.1", 27 | "php": ">=5.5.0" 28 | }, 29 | "require-dev": { 30 | "ext-curl": "*", 31 | "phpunit/phpunit": "~4.0", 32 | "psr/log": "~1.0" 33 | }, 34 | "type": "library", 35 | "extra": { 36 | "branch-alias": { 37 | "dev-master": "6.2-dev" 38 | } 39 | }, 40 | "autoload": { 41 | "files": [ 42 | "src/functions_include.php" 43 | ], 44 | "psr-4": { 45 | "GuzzleHttp\\": "src/" 46 | } 47 | }, 48 | "notification-url": "https://packagist.org/downloads/", 49 | "license": [ 50 | "MIT" 51 | ], 52 | "authors": [ 53 | { 54 | "name": "Michael Dowling", 55 | "email": "mtdowling@gmail.com", 56 | "homepage": "https://github.com/mtdowling" 57 | } 58 | ], 59 | "description": "Guzzle is a PHP HTTP client library", 60 | "homepage": "http://guzzlephp.org/", 61 | "keywords": [ 62 | "client", 63 | "curl", 64 | "framework", 65 | "http", 66 | "http client", 67 | "rest", 68 | "web service" 69 | ], 70 | "time": "2016-02-17 21:09:10" 71 | }, 72 | { 73 | "name": "guzzlehttp/promises", 74 | "version": "dev-master", 75 | "source": { 76 | "type": "git", 77 | "url": "https://github.com/guzzle/promises.git", 78 | "reference": "976e0f50981f845dcee1c25bfcac2304a7bb5583" 79 | }, 80 | "dist": { 81 | "type": "zip", 82 | "url": "https://api.github.com/repos/guzzle/promises/zipball/976e0f50981f845dcee1c25bfcac2304a7bb5583", 83 | "reference": "976e0f50981f845dcee1c25bfcac2304a7bb5583", 84 | "shasum": "" 85 | }, 86 | "require": { 87 | "php": ">=5.5.0" 88 | }, 89 | "require-dev": { 90 | "phpunit/phpunit": "~4.0" 91 | }, 92 | "type": "library", 93 | "extra": { 94 | "branch-alias": { 95 | "dev-master": "1.0-dev" 96 | } 97 | }, 98 | "autoload": { 99 | "psr-4": { 100 | "GuzzleHttp\\Promise\\": "src/" 101 | }, 102 | "files": [ 103 | "src/functions_include.php" 104 | ] 105 | }, 106 | "notification-url": "https://packagist.org/downloads/", 107 | "license": [ 108 | "MIT" 109 | ], 110 | "authors": [ 111 | { 112 | "name": "Michael Dowling", 113 | "email": "mtdowling@gmail.com", 114 | "homepage": "https://github.com/mtdowling" 115 | } 116 | ], 117 | "description": "Guzzle promises library", 118 | "keywords": [ 119 | "promise" 120 | ], 121 | "time": "2016-02-26 22:29:15" 122 | }, 123 | { 124 | "name": "guzzlehttp/psr7", 125 | "version": "1.2.3", 126 | "source": { 127 | "type": "git", 128 | "url": "https://github.com/guzzle/psr7.git", 129 | "reference": "2e89629ff057ebb49492ba08e6995d3a6a80021b" 130 | }, 131 | "dist": { 132 | "type": "zip", 133 | "url": "https://api.github.com/repos/guzzle/psr7/zipball/2e89629ff057ebb49492ba08e6995d3a6a80021b", 134 | "reference": "2e89629ff057ebb49492ba08e6995d3a6a80021b", 135 | "shasum": "" 136 | }, 137 | "require": { 138 | "php": ">=5.4.0", 139 | "psr/http-message": "~1.0" 140 | }, 141 | "provide": { 142 | "psr/http-message-implementation": "1.0" 143 | }, 144 | "require-dev": { 145 | "phpunit/phpunit": "~4.0" 146 | }, 147 | "type": "library", 148 | "extra": { 149 | "branch-alias": { 150 | "dev-master": "1.0-dev" 151 | } 152 | }, 153 | "autoload": { 154 | "psr-4": { 155 | "GuzzleHttp\\Psr7\\": "src/" 156 | }, 157 | "files": [ 158 | "src/functions_include.php" 159 | ] 160 | }, 161 | "notification-url": "https://packagist.org/downloads/", 162 | "license": [ 163 | "MIT" 164 | ], 165 | "authors": [ 166 | { 167 | "name": "Michael Dowling", 168 | "email": "mtdowling@gmail.com", 169 | "homepage": "https://github.com/mtdowling" 170 | } 171 | ], 172 | "description": "PSR-7 message implementation", 173 | "keywords": [ 174 | "http", 175 | "message", 176 | "stream", 177 | "uri" 178 | ], 179 | "time": "2016-02-18 21:54:00" 180 | }, 181 | { 182 | "name": "psr/http-message", 183 | "version": "dev-master", 184 | "source": { 185 | "type": "git", 186 | "url": "https://github.com/php-fig/http-message.git", 187 | "reference": "85d63699f0dbedb190bbd4b0d2b9dc707ea4c298" 188 | }, 189 | "dist": { 190 | "type": "zip", 191 | "url": "https://api.github.com/repos/php-fig/http-message/zipball/85d63699f0dbedb190bbd4b0d2b9dc707ea4c298", 192 | "reference": "85d63699f0dbedb190bbd4b0d2b9dc707ea4c298", 193 | "shasum": "" 194 | }, 195 | "require": { 196 | "php": ">=5.3.0" 197 | }, 198 | "type": "library", 199 | "extra": { 200 | "branch-alias": { 201 | "dev-master": "1.0.x-dev" 202 | } 203 | }, 204 | "autoload": { 205 | "psr-4": { 206 | "Psr\\Http\\Message\\": "src/" 207 | } 208 | }, 209 | "notification-url": "https://packagist.org/downloads/", 210 | "license": [ 211 | "MIT" 212 | ], 213 | "authors": [ 214 | { 215 | "name": "PHP-FIG", 216 | "homepage": "http://www.php-fig.org/" 217 | } 218 | ], 219 | "description": "Common interface for HTTP messages", 220 | "keywords": [ 221 | "http", 222 | "http-message", 223 | "psr", 224 | "psr-7", 225 | "request", 226 | "response" 227 | ], 228 | "time": "2015-05-04 20:22:00" 229 | } 230 | ], 231 | "packages-dev": [], 232 | "aliases": [], 233 | "minimum-stability": "dev", 234 | "stability-flags": [], 235 | "prefer-stable": false, 236 | "prefer-lowest": false, 237 | "platform": [], 238 | "platform-dev": [] 239 | } 240 | -------------------------------------------------------------------------------- /src/Exceptions/InvalidChannelException.php: -------------------------------------------------------------------------------- 1 | privateKey = $privateKey; 51 | $this->guzzle = $guzzle; 52 | $this->config = $config; 53 | 54 | $this->validatePrivatekey($privateKey); 55 | $this->initializeGuzzle(); 56 | $this->initializeConfig(); 57 | } 58 | 59 | /** 60 | * Validate a private key. 61 | * 62 | * @param $private 63 | * @throws \Pushman\PHPLib\Exceptions\InvalidConfigException 64 | */ 65 | private function validatePrivatekey($private) 66 | { 67 | if (strlen($private) !== 60) { 68 | throw new InvalidConfigException('This cannot possibly be a valid private key.'); 69 | } 70 | } 71 | 72 | /** 73 | * Setup guzzle if a client hasn't been given. 74 | */ 75 | private function initializeGuzzle() 76 | { 77 | if (is_null($this->guzzle)) { 78 | $this->guzzle = new Client(); 79 | } 80 | } 81 | 82 | /** 83 | * Setup the config if one hasn't been provided. 84 | * 85 | * @throws \Pushman\PHPLib\Exceptions\InvalidConfigException 86 | */ 87 | private function initializeConfig() 88 | { 89 | if (empty($this->config['url'])) { 90 | $this->config['url'] = static::$url; 91 | } 92 | 93 | $this->config['url'] = rtrim($this->config['url'], '/'); 94 | 95 | if (filter_var($this->config['url'], FILTER_VALIDATE_URL) === false) { 96 | throw new InvalidConfigException('You must provide a valid URL in the config.'); 97 | } 98 | } 99 | 100 | /** 101 | * Set the client after initialisation. Used only for testing. 102 | * 103 | * @param \GuzzleHttp\Client $client 104 | */ 105 | public function setClient(Client $client) 106 | { 107 | $this->guzzle = $client; 108 | } 109 | 110 | /** 111 | * Push an event to Pushman, the most used command. 112 | * 113 | * @param $event 114 | * @param string $channel 115 | * @param array $payload 116 | * @return string 117 | * @throws \Pushman\PHPLib\Exceptions\InvalidChannelException 118 | * @throws \Pushman\PHPLib\Exceptions\InvalidEventException 119 | */ 120 | public function push($event, $channel = 'public', array $payload = []) 121 | { 122 | $payload = $this->preparePayload($payload); 123 | $this->validateEvent($event); 124 | $channels = $this->validateChannel($channel); 125 | 126 | $url = $this->getURL(); 127 | 128 | $headers = [ 129 | 'body' => [ 130 | 'private' => $this->privateKey, 131 | 'channels' => $channels, 132 | 'event' => $event, 133 | 'payload' => $payload 134 | ] 135 | ]; 136 | 137 | $response = $this->processRequest($url, $headers); 138 | 139 | return $response; 140 | } 141 | 142 | /** 143 | * Encode a payload 144 | * 145 | * @param $payload 146 | * @return string 147 | */ 148 | private function preparePayload($payload) 149 | { 150 | $payload = json_encode($payload); 151 | 152 | return $payload; 153 | } 154 | 155 | /** 156 | * Validate an event name. 157 | * 158 | * @param $event 159 | * @throws \Pushman\PHPLib\Exceptions\InvalidEventException 160 | */ 161 | private function validateEvent($event) 162 | { 163 | if (empty($event)) { 164 | throw new InvalidEventException('You must provide an event name.'); 165 | } 166 | 167 | if (strpos($event, ' ') !== false) { 168 | throw new InvalidEventException('No spaces are allowed in event names.'); 169 | } 170 | } 171 | 172 | /** 173 | * Validate a channel or set of channels, return JSON if appropriate. 174 | * 175 | * @param array $channels 176 | * @param bool $returnAsArray 177 | * @return array|string 178 | * @throws \Pushman\PHPLib\Exceptions\InvalidChannelException 179 | */ 180 | private function validateChannel($channels = [], $returnAsArray = true) 181 | { 182 | if ($returnAsArray) { 183 | if (is_string($channels)) { 184 | $channels = [$channels]; 185 | } 186 | if (empty($channels)) { 187 | return ['public']; 188 | } 189 | foreach ($channels as $channel) { 190 | if (strpos($channel, ' ') !== false) { 191 | throw new InvalidChannelException('No spaces are allowed in channel names.'); 192 | } 193 | } 194 | 195 | return json_encode($channels); 196 | } else { 197 | if (empty($channels)) { 198 | return 'public'; 199 | } 200 | if (strpos($channels, ' ') !== false) { 201 | throw new InvalidChannelException('No spaces are allowed in channel names.'); 202 | } 203 | 204 | return $channels; 205 | } 206 | } 207 | 208 | /** 209 | * Get the URL of our Pushman instance. 210 | * Defaults to the live site Push command. 211 | * 212 | * @param null $endpoint 213 | * @return string 214 | */ 215 | private function getURL($endpoint = null) 216 | { 217 | if (is_null($endpoint)) { 218 | $endpoint = $this->getEndpoint(); 219 | } else { 220 | $endpoint = '/api/' . $endpoint; 221 | } 222 | 223 | return $this->config['url'] . $endpoint; 224 | } 225 | 226 | /** 227 | * Get the endpoint for the PUSH comment. 228 | * 229 | * @return string 230 | */ 231 | private function getEndpoint() 232 | { 233 | return '/api/push'; 234 | } 235 | 236 | /** 237 | * Process a request and return the handled response. 238 | * 239 | * @param string $url 240 | * @param array $headers 241 | * @param string $method 242 | * @return string 243 | */ 244 | private function processRequest($url, $headers, $method = 'post') 245 | { 246 | if (array_key_exists('body', $headers)) { 247 | $headers['body'] = json_encode($headers['body']); 248 | } 249 | 250 | if ($method == 'post') { 251 | $response = $this->guzzle->request('POST', $url, $headers); 252 | } elseif ($method == 'delete') { 253 | $response = $this->guzzle->request('DELETE', $url, $headers); 254 | } else { 255 | $params = $this->processGetParams($headers); 256 | $response = $this->guzzle->request('GET', $url . $params); 257 | } 258 | $response = $this->processResponse($response); 259 | 260 | return $response; 261 | } 262 | 263 | /** 264 | * If we are doing a GET request, turn it into URL params. 265 | * 266 | * @param $headers 267 | * @return string 268 | */ 269 | private function processGetParams($headers) 270 | { 271 | $paramStrings = []; 272 | foreach ($headers['body'] as $key => $value) { 273 | $paramStrings[] = $key . "=" . $value; 274 | } 275 | $paramString = "?"; 276 | $paramString .= implode("&", $paramStrings); 277 | 278 | return $paramString; 279 | } 280 | 281 | /** 282 | * Process a repsonse, get the JSON and output the decoded values. 283 | * 284 | * @param Response $response 285 | * @return Response|mixed|string 286 | */ 287 | private function processResponse(Response $response) 288 | { 289 | $response = $response->getBody()->getContents(); 290 | $response = json_decode($response, true); 291 | 292 | return $response; 293 | } 294 | 295 | /** 296 | * Get the token of a single channel. 297 | * 298 | * @param $channel 299 | * @return array 300 | */ 301 | public function token($channel) 302 | { 303 | $channel = $this->channel($channel); 304 | 305 | return ['token' => $channel['public'], 'expires' => $channel['token_expires']]; 306 | } 307 | 308 | /** 309 | * Get information on a single channel. 310 | * 311 | * @param $channel 312 | * @return string 313 | * @throws \Pushman\PHPLib\Exceptions\InvalidChannelException 314 | */ 315 | public function channel($channel) 316 | { 317 | $channel = $this->validateChannel($channel, false); 318 | 319 | $url = $this->getURL('channel'); 320 | 321 | $headers = [ 322 | 'body' => [ 323 | 'private' => $this->privateKey, 324 | 'channel' => $channel 325 | ] 326 | ]; 327 | 328 | $response = $this->processRequest($url, $headers, 'get'); 329 | 330 | return $response; 331 | } 332 | 333 | /** 334 | * Get an array of channels in the site. 335 | * 336 | */ 337 | public function channels() 338 | { 339 | $url = $this->getURL('channels'); 340 | 341 | $headers = [ 342 | 'body' => [ 343 | 'private' => $this->privateKey 344 | ] 345 | ]; 346 | 347 | $response = $this->processRequest($url, $headers, 'get'); 348 | 349 | return $response; 350 | } 351 | 352 | /** 353 | * Build a new channel or set of channels. 354 | * 355 | * @param $channel 356 | * @return string 357 | * @throws \Pushman\PHPLib\Exceptions\InvalidChannelException 358 | */ 359 | public function buildChannel($channel, $max = 3, $refreshes = 'no') 360 | { 361 | $channels = $this->validateChannel($channel); 362 | 363 | $url = $this->getURL('channel'); 364 | 365 | $headers = [ 366 | 'body' => [ 367 | 'private' => $this->privateKey, 368 | 'channel' => $channels, 369 | 'max' => $max, 370 | 'refreshes' => $refreshes 371 | ] 372 | ]; 373 | 374 | $response = $this->processRequest($url, $headers, 'post'); 375 | 376 | return $response; 377 | } 378 | 379 | /** 380 | * Destroy a channel or set of channels. 381 | * 382 | * @param $channel 383 | * @return string 384 | * @throws InvalidChannelException 385 | * @throws InvalidDeleteRequestException 386 | */ 387 | public function destroyChannel($channel) 388 | { 389 | $channels = $this->validateChannel($channel); 390 | 391 | $arrayOfChannels = json_decode($channels, true); 392 | if (in_array('public', $arrayOfChannels)) { 393 | throw new InvalidDeleteRequestException('You cannot delete the public channel.'); 394 | } 395 | 396 | $url = $this->getURL('channel'); 397 | 398 | $headers = [ 399 | 'body' => [ 400 | 'private' => $this->privateKey, 401 | 'channel' => $channels 402 | ] 403 | ]; 404 | 405 | $response = $this->processRequest($url, $headers, 'delete'); 406 | 407 | return $response; 408 | } 409 | } -------------------------------------------------------------------------------- /src/PushmanBroadcaster.php: -------------------------------------------------------------------------------- 1 | pushman = $pushman; 13 | } 14 | 15 | /** 16 | * Broadcast the given event. 17 | * 18 | * @param array $channels 19 | * @param string $event 20 | * @param array $payload 21 | * @return void 22 | */ 23 | public function broadcast(array $channels, $event, array $payload = []) 24 | { 25 | $this->pushman->push($event, $channels, $payload); 26 | } 27 | } -------------------------------------------------------------------------------- /src/PushmanServiceProvider.php: -------------------------------------------------------------------------------- 1 | extend( 17 | 'pushman', 18 | function ($app) { 19 | return new PushmanBroadcaster( 20 | app('pushman') 21 | ); 22 | } 23 | ); 24 | } 25 | 26 | /** 27 | * Register the application services. 28 | * 29 | * @return void 30 | */ 31 | public function register() 32 | { 33 | $this->app->singleton('pushman', function($app) { 34 | $private = env('PUSHMAN_PRIVATE'); 35 | $url = env('PUSHMAN_URL', 'http://live.pushman.dfl.mn'); 36 | 37 | return new Pushman($private, ['url' => $url]); 38 | }); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /tests/channel.txt: -------------------------------------------------------------------------------- 1 | {"id":1,"name":"public","public":"zEsBqcOt2atW4zmaAXn9","refreshes":"no","max_connections":3,"active_users":0,"events_fired":12,"created_at":"2015-05-18 04:19:59","token_expires":{"date":"2015-05-21 21:00:00.000000","timezone_type":3,"timezone":"UTC"}} -------------------------------------------------------------------------------- /tests/channels.txt: -------------------------------------------------------------------------------- 1 | [{"id":1,"name":"public","public":"zEsBqcOt2atW4zmaAXn9","refreshes":"no","max_connections":3,"active_users":0,"events_fired":12,"created_at":"2015-05-18 04:19:59"},{"id":13,"name":"auth","public":"91aUcAdf4oAUjOIsszUl","refreshes":"yes","max_connections":3,"active_users":0,"events_fired":2,"created_at":"2015-05-20 15:21:57"}] -------------------------------------------------------------------------------- /tests/delete.txt: -------------------------------------------------------------------------------- 1 | {"status":"success","message":"","deleted":"my_channel","failed_on":""} -------------------------------------------------------------------------------- /tests/delete_array.txt: -------------------------------------------------------------------------------- 1 | {"status":"success","message":"","deleted":"my_channel,my_channel2,my_channel3","failed_on":""} -------------------------------------------------------------------------------- /tests/delete_half.txt: -------------------------------------------------------------------------------- 1 | {"status":"success","message":"","deleted":"my_channel","failed_on":"does_not_exist"} -------------------------------------------------------------------------------- /tests/new_channel.txt: -------------------------------------------------------------------------------- 1 | [{"name":"my_channel","refreshes":"no","max_connections":3,"public":"wXLaUAAEhSfl3TMP3JCK","created_at":"2015-05-28 15:07:26","id":31}] -------------------------------------------------------------------------------- /tests/push.txt: -------------------------------------------------------------------------------- 1 | 2 | {"status":"success","message":"Event pushed successfully.","event":"test_event","channels":[{"id":1,"name":"public","public":"zEsBqcOt2atW4zmaAXn9","refreshes":"no","max_connections":3,"active_users":0,"events_fired":16,"created_at":"2015-05-18 04:19:59"}],"site":"test","timestamp":{"date":"2015-05-21 21:41:04.000000","timezone_type":3,"timezone":"UTC"},"payload":[]} --------------------------------------------------------------------------------