From 83e3a783b0687ebdedfad697e866cecada92d9b9 Mon Sep 17 00:00:00 2001 From: Ali Borhani Date: Sat, 4 Apr 2015 03:59:50 +0430 Subject: [PATCH] first release --- .gitignore | 5 + .travis.yml | 13 ++ CONTRIBUTING.md | 3 + LICENSE | 21 ++ README.md | 184 +++++++++++++++++ composer.json | 40 ++++ phpunit.xml | 18 ++ src/Nikapps/OrtcLaravel/OrtcLaravelFacade.php | 17 ++ .../OrtcLaravel/OrtcLaravelFactory.php | 186 ++++++++++++++++++ .../OrtcLaravelServiceProvider.php | 53 +++++ src/config/.gitkeep | 0 src/config/config.php | 55 ++++++ tests/.gitkeep | 0 13 files changed, 595 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 CONTRIBUTING.md create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 phpunit.xml create mode 100644 src/Nikapps/OrtcLaravel/OrtcLaravelFacade.php create mode 100644 src/Nikapps/OrtcLaravel/OrtcLaravelFactory.php create mode 100644 src/Nikapps/OrtcLaravel/OrtcLaravelServiceProvider.php create mode 100644 src/config/.gitkeep create mode 100644 src/config/config.php create mode 100644 tests/.gitkeep diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fc9a507 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/vendor +composer.phar +composer.lock +.DS_Store +/.idea \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f60bbe0 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,13 @@ +language: php + +php: + - 5.4 + - 5.5 + - 5.6 + - hhvm + +before_script: + - travis_retry composer self-update + - travis_retry composer install --prefer-source --no-interaction --dev + +script: phpunit diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..68dcb91 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,3 @@ +# Contribution Guidelines + +Please submit all issues and pull requests to the [nikapps/ortc-laravel](http://github.com/nikapps/ortc-laravel) repository! diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ee13c7e --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2015 NikApps Team. + * + * 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: + * + * 1- The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * 2- 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. + */ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f23339b --- /dev/null +++ b/README.md @@ -0,0 +1,184 @@ +# ORTC-Laravel (ORTC client for Laravel) + +An Easy-To-Use ORTC API Client package for Laravel Framework (Laravel 4.2.x) + + + +*This package is based on [nikapps/ortc-php](https://github.com/nikapps/ortc-php).* + +## Installation + +Simply run command: + +``` +composer require nikapps/ortc-laravel +``` + +Or you can add this [package](https://packagist.org/packages/nikapps/ortc-laravel) dependency to your Laravel's composer.json : + +~~~json +{ + "require": { + "nikapps/ortc-laravel": "1.*" + } + +} +~~~ + +Then update composer: + +``` +composer update +``` + +- + +Add this package provider in your providers array `[app/config/app.php]`: + +~~~php +'Nikapps\OrtcLaravel\OrtcLaravelServiceProvider', +~~~ + +Next you need to publish configuration file. Run this command: + +``` +php artisan config:publish nikapps/ortc-laravel +``` + + +## Configuration + +#### Get Application Key & Private Key +First of all, you should register on realtime.co and get your api keys. + +* Login/Register at https://accounts.realtime.co + +* Create new Subscription + +* You can see your `Application Key` and `Private Key` + +* If you want to use authentication, you should enable it in your panel. + +#### Update config file + +Edit `app/config/packages/nikapps/ortc-laravel/config.php`: + +~~~php +'credentials' => [ + + /* + * your application key + */ + 'application_key' => 'YOUR_APPLICATION_KEY', + /* + * your private key + */ + 'private_key' => 'YOUR_PRIVATE_KEY', + +], +~~~ + +#### Done! + +## Usage + +#### Get Balancer URL (Manually) + +This package automatically get balancer url (best available server), but if you want fetch a new balancer url manually: + +~~~php +$balancerUrl = Ortc::getBalancerUrl(); + +echo 'Balancer Url: ' . $balancerUrl->getUrl(); +~~~ + +#### Authentication +In order to authenticate a user: + +~~~php +$channels = [ + 'channel_one' => 'w', + 'channel_two' => 'r' +]; + +Ortc::authenticate( + $authToken, //authentication token + $channels, + $ttl, //(optional) default: 3600 + $isPrivate //(optional) default: false +); +~~~ + +#### Send Message (Push) +In order to push a message to a channel: + +~~~php +Ortc::send( + $channel, //channel name + $authToken, //authentication token + $message //message (string) +); +~~~ + +*If you using UTF-8 messages, it's better to use `base64_encode()`.* + +## Exceptions +See [nikapps/ortc-php - Exceptions](https://github.com/nikapps/ortc-php#exceptions) + + +## Dependencies + +* [nikapps/ortc-php (1.x)](https://packagist.org/packages/nikapps/ortc-php) + + +## Ortc Documentations +This package is based on ORTC REST API. You can download REST service documentation from this url: + +``` +http://ortc.xrtml.org/documentation/rest/2.1.0/RestServices.pdf +``` + +## TODO + +* add UnitTest (codeception or phpunit) +* subscribe channel(s) by Ratchet/Nodejs +* support mobile push notification (ios & android) +* support presence channels +* create package for Laravel 5 +* Anything else?! + +## Contribute + +Wanna contribute? simply fork this project and make a pull request! + + +## License +This project released under the [MIT License](http://opensource.org/licenses/mit-license.php). + +``` +/* + * Copyright (C) 2015 NikApps Team. + * + * 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: + * + * 1- The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * 2- 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. + */ +``` + +## Donation + +[![Donate via Paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=G3WRCRDXJD6A8) diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..9da01ac --- /dev/null +++ b/composer.json @@ -0,0 +1,40 @@ +{ + "name": "nikapps/ortc-laravel", + "description": "An API wrapper for ORTC (Real-Time framework from realtime.co) based on Laravel framework", + "keywords": [ + "laravel", + "RealTime", + "realtime.co", + "ORTC", + "API", + "Wrapper", + "XRTML", + "pub-sub", + "push", + "publish", + "real-time" + ], + "license": "MIT", + "type": "project", + "authors": [ + { + "name": "Ali Borhani", + "email": "aliborhani1@gmail.com" + }, + { + "name": "Hossein Moradgholi", + "email": "h.moradgholi@icloud.com" + } + ], + "require": { + "php": ">=5.4.0", + "illuminate/support": "4.2.*", + "nikapps/ortc-php": "1.*" + }, + "autoload": { + "psr-0": { + "Nikapps\\OrtcLaravel\\": "src/" + } + }, + "minimum-stability": "stable" +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..3347b75 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,18 @@ + + + + + ./tests/ + + + diff --git a/src/Nikapps/OrtcLaravel/OrtcLaravelFacade.php b/src/Nikapps/OrtcLaravel/OrtcLaravelFacade.php new file mode 100644 index 0000000..6eb2447 --- /dev/null +++ b/src/Nikapps/OrtcLaravel/OrtcLaravelFacade.php @@ -0,0 +1,17 @@ +config = $config; + + $this->createOrtcConfig(); + } + + /** + * @return OrtcConfig + */ + public function getOrtcConfig() + { + return $this->ortcConfig; + } + + /** + * create OrtcConfig from laravel config + */ + protected function createOrtcConfig() + { + $ortcConfig = new OrtcConfig(); + + $ortcConfig->setApplicationKey( + $this->config->get('ortc-laravel::credentials.application_key') + ); + $ortcConfig->setPrivateKey( + $this->config->get('ortc-laravel::credentials.private_key') + ); + $ortcConfig->setBalancerUrl( + $this->config->get('ortc-laravel::api.balancer_url') + ); + $ortcConfig->setAuthenticationPath( + $this->config->get('ortc-laravel::api.authentication.path') + ); + $ortcConfig->setSendPath( + $this->config->get('ortc-laravel::api.send_message.path') + ); + $ortcConfig->setMaxChunkSize( + $this->config->get('ortc-laravel::api.send_message.max_chunk_size') + ); + $ortcConfig->setBatchPoolSize( + $this->config->get('ortc-laravel::api.send_message.batch_pool_size') + ); + $ortcConfig->setPreMessageString( + $this->config->get('ortc-laravel::api.send_message.pre_message_string') + ); + $ortcConfig->setVerifySsl( + $this->config->get('ortc-laravel::api.verify_ssl') + ); + + $this->ortcConfig = $ortcConfig; + } + + /** + * authenticate user + * + * @param string|AuthRequest $authTokenOrAuthRequest + * @param array $channels + * @param int $ttl = 3600 + * @param bool $isPrivate = false + * @throws \Nikapps\OrtcPhp\Exceptions\NetworkErrorException + * @throws \Nikapps\OrtcPhp\Exceptions\UnauthorizedException + * @return \Nikapps\OrtcPhp\Models\Responses\AuthResponse + */ + public function authenticate( + $authTokenOrAuthRequest, + $channels = [], + $ttl = 3600, + $isPrivate = false + ) { + if (!($authTokenOrAuthRequest instanceof AuthRequest)) { + $authToken = $authTokenOrAuthRequest; + + $channelObjects = []; + + //create channel objects + foreach ($channels as $channelName => $permission) { + $channel = new Channel(); + $channel->setName($channelName); + $channel->setPermission($permission); + + $channelObjects[] = $channel; + } + + $authRequest = new AuthRequest(); + $authRequest->setAuthToken($authToken); + $authRequest->setExpireTime($ttl); + $authRequest->setPrivate($isPrivate); + $authRequest->setChannels($channelObjects); + } else { + $authRequest = $authTokenOrAuthRequest; + } + + $ortc = new Ortc($this->ortcConfig); + + return $ortc->authenticate($authRequest); + } + + /** + * get new balancer url + * + * @throws \Nikapps\OrtcPhp\Exceptions\NetworkErrorException + * @throws \Nikapps\OrtcPhp\Exceptions\UnauthorizedException + * @throws \Nikapps\OrtcPhp\Exceptions\InvalidBalancerUrlException + * @return \Nikapps\OrtcPhp\Models\Responses\BalancerUrlResponse + */ + public function getBalancerUrl() + { + $ortc = new Ortc($this->ortcConfig); + + return $ortc->getBalancerUrl(); + } + + /** + * send (push) message to a channel + * + * @param string|SendMessageRequest $channelOrSendMessageRequest + * @param string $authToken + * @param string $message + * @throws \Nikapps\OrtcPhp\Exceptions\BatchRequestException + * @return \Nikapps\OrtcPhp\Models\Responses\SendMessageResponse + */ + public function send( + $channelOrSendMessageRequest, + $authToken = '', + $message = '' + ) { + if (!($channelOrSendMessageRequest instanceof SendMessageRequest)) { + $channelName = $channelOrSendMessageRequest; + + $sendMessageRequest = new SendMessageRequest(); + $sendMessageRequest->setAuthToken($authToken); + $sendMessageRequest->setChannelName($channelName); + $sendMessageRequest->setMessage($message); + } else { + $sendMessageRequest = $channelOrSendMessageRequest; + } + + $ortc = new Ortc($this->ortcConfig); + + return $ortc->sendMessage($sendMessageRequest); + } + + /** + * same as send() but pusher-way! + * + * @param string $channel + * @param string $authToken + * @param string $message + * @throws \Nikapps\OrtcPhp\Exceptions\BatchRequestException + * @return \Nikapps\OrtcPhp\Models\Responses\SendMessageResponse + */ + public function trigger($channel, $authToken, $message) + { + return $this->send($channel, $authToken, $message); + } +} diff --git a/src/Nikapps/OrtcLaravel/OrtcLaravelServiceProvider.php b/src/Nikapps/OrtcLaravel/OrtcLaravelServiceProvider.php new file mode 100644 index 0000000..f12c864 --- /dev/null +++ b/src/Nikapps/OrtcLaravel/OrtcLaravelServiceProvider.php @@ -0,0 +1,53 @@ +package('nikapps/ortc-laravel'); + + AliasLoader::getInstance()->alias( + 'Ortc', + 'Nikapps\OrtcLaravel\OrtcLaravelFacade' + ); + } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $this->app->bind('Ortc', function ($app) { + $config = $app['config']; + return new OrtcLaravelFactory($config); + }); + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return array(); + } +} diff --git a/src/config/.gitkeep b/src/config/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/config/config.php b/src/config/config.php new file mode 100644 index 0000000..ebadf08 --- /dev/null +++ b/src/config/config.php @@ -0,0 +1,55 @@ + [ + + /* + * your application key + */ + 'application_key' => 'your-applicatoin-key', + /* + * your private key + */ + 'private_key' => 'your-client-secret', + + ], + /* + |-------------------------------------------------------------------------- + | Real-time REST API Options + |-------------------------------------------------------------------------- + | you can change default options of api. + | + */ + 'api' => [ + /* + * send message + */ + 'send_message' => [ + 'path' => '/send', //api path + 'max_chunk_size' => 700, //maximum size of each message in bytes + 'batch_pool_size' => 5, //pool size for concurrent requests + 'pre_message_string' => '{RANDOM}_{PART}-{TOTAL_PARTS}_' //pre message string format + ], + /* + * authentication + */ + 'authentication' => [ + 'path' => '/authenticate' //api path + ], + /* + * url to fetch balancer url + */ + 'balancer_url' => 'https://ortc-developers.realtime.co/server/2.1?appkey={APP_KEY}', + /* + * verify ssl/tls certificate + */ + 'verify_ssl' => true + ] +]; diff --git a/tests/.gitkeep b/tests/.gitkeep new file mode 100644 index 0000000..e69de29