From 0f09791ac80c27096aa238a57cc8cf57ea6eaaf0 Mon Sep 17 00:00:00 2001 From: Aislan Dener Souza Vicentini Date: Sat, 8 Jan 2022 13:46:32 -0300 Subject: [PATCH] First Commit --- .gitignore | 4 ++ .phpunit.result.cache | 1 + CHANGELOG.md | 6 +++ LICENSE | 21 ++++++++++ README.md | 2 + composer.json | 44 ++++++++++++++++++++ config/mixtelematics.php | 11 +++++ phpunit.xml | 33 +++++++++++++++ src/Facades/MixTelematics.php | 17 ++++++++ src/MixTelematicsLaravelService.php | 61 ++++++++++++++++++++++++++++ src/MixTelematicsServiceProvider.php | 34 ++++++++++++++++ tests/TestCase.php | 26 ++++++++++++ 12 files changed, 260 insertions(+) create mode 100644 .gitignore create mode 100644 .phpunit.result.cache create mode 100644 CHANGELOG.md create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 config/mixtelematics.php create mode 100644 phpunit.xml create mode 100644 src/Facades/MixTelematics.php create mode 100644 src/MixTelematicsLaravelService.php create mode 100644 src/MixTelematicsServiceProvider.php create mode 100644 tests/TestCase.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a75b33b --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/vendor/ +/.idea/ +/.vscode/ +composer.lock \ No newline at end of file diff --git a/.phpunit.result.cache b/.phpunit.result.cache new file mode 100644 index 0000000..2de2a6d --- /dev/null +++ b/.phpunit.result.cache @@ -0,0 +1 @@ +{"version":1,"defects":[],"times":[]} \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..92c813a --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,6 @@ +## 0.1.0 + +* Create file config +* Get the token via API +* Searching for drivers (no class implemented) +* Laravel packages auto-package \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d189f79 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Aislan Dener Souza Vicentini + +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..13b9fe6 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# mix-telematics-laravel +MiX Telematics Communication API with Laravel diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..0ba5cda --- /dev/null +++ b/composer.json @@ -0,0 +1,44 @@ +{ + "name": "aislandener/mix-telematics-laravel", + "description": "MiX Telematics Communication API with Laravel", + "type": "library", + "version": "0.1.0", + "require": { + "php": "^8.0", + "jumbojett/openid-connect-php": "^0.9.3", + "guzzlehttp/guzzle": "^7.4", + "illuminate/support": "^6|^7|^8|^9" + }, + "require-dev": { + "orchestra/testbench": "^6.0", + "phpunit/phpunit": "9.5.11" + }, + "license": "MIT", + "autoload": { + "psr-4": { + "Aislandener\\MixTelematicsLaravel\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Aislandener\\MixTelematicsLaravel\\Tests\\": "tests/" + } + }, + "extra": { + "laravel": { + "providers": [ + "Aislandener\\MixTelematicsLaravel\\MixTelematicsServiceProvider" + ], + "aliases":{ + "MixTelematics": "Aislandener\\MixTelematicsLaravel\\Facades\\MixTelematics" + } + } + }, + "authors": [ + { + "name": "Aislan Dener Souza Vicentini", + "email": "aislandenersouzavicentini@gmail.com" + } + ], + "minimum-stability": "dev" +} diff --git a/config/mixtelematics.php b/config/mixtelematics.php new file mode 100644 index 0000000..4076bb5 --- /dev/null +++ b/config/mixtelematics.php @@ -0,0 +1,11 @@ + env('MIXTELEMETRICTS_CLIENT_NAME'), + 'clientID' => env('MIXTELEMETRICTS_CLIENT_ID'), + 'clientSecret' => env('MIXTELEMETRICTS_CLIENT_SECRECT'), + 'IDBaseUrl' => env('MIXTELEMETRICTS_ID_SERVER','https://identity.us.mixtelematics.com')."/core", + 'RestBaseUrl' => env('MIXTELEMETRICTS_REST_SERVER','https://integrate.us.mixtelematics.com'), + 'dynamixUserName' => env('MIXTELEMETRICTS_USERNAME'), + 'dynamixUserPassword' => env('MIXTELEMETRICTS_PASSWORD'), + 'organisationId' => env('MIXTELEMETRICTS_ORGANIZATION_ID'), +]; diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..947a8bd --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,33 @@ + + + + + src/ + + + + + ./tests/Unit + + + ./tests/Feature + + + + + + + \ No newline at end of file diff --git a/src/Facades/MixTelematics.php b/src/Facades/MixTelematics.php new file mode 100644 index 0000000..bd5e140 --- /dev/null +++ b/src/Facades/MixTelematics.php @@ -0,0 +1,17 @@ +clientName = config('mixtelematics.clientName'); + $this->clientID = config('mixtelematics.clientID'); + $this->clientSecret = config('mixtelematics.clientSecret'); + $this->idBaseUrl = config('mixtelematics.IDBaseUrl'); + $this->restBaseUrl = config('mixtelematics.RestBaseUrl'); + $this->dynamixUserName = config('mixtelematics.dynamixUserName'); + $this->dynamixUserPassword = config('mixtelematics.dynamixUserPassword'); + $this->organisationId = config('mixtelematics.organisationId'); + + $this->token = $this->getToken(); + } + + public function getToken() + { + $oidc = new OpenIDConnectClient( + provider_url: $this->idBaseUrl,client_id: $this->clientID, client_secret: $this->clientSecret + ); + + $oidc->providerConfigParam([ + 'token_endpoint' => "{$this->idBaseUrl}/connect/token" + ]); + $oidc->addScope([self::SCOPE]); + $oidc->setClientName($this->clientName); + $oidc->addAuthParam(['username' => $this->dynamixUserName, 'password' => $this->dynamixUserPassword]); + + try { + return $oidc->requestResourceOwnerToken(true); + } catch (OpenIDConnectClientException $e) { + return null; + } + } + + public function getDriversOrganisation(){ + return Http::withToken($this->token)->get($this->restBaseUrl . "/api/drivers/organisation/{$this->organisationId}")->json(); + } +} diff --git a/src/MixTelematicsServiceProvider.php b/src/MixTelematicsServiceProvider.php new file mode 100644 index 0000000..03dfdce --- /dev/null +++ b/src/MixTelematicsServiceProvider.php @@ -0,0 +1,34 @@ +mergeConfigFrom($configPath, 'mixtelematics'); + + $this->app->singleton('MixTelematics', function (){ + return new MixTelematicsLaravelService(); + }); + } + + public function boot() + { + if($this->app->runningInConsole()) { + $configPath = __DIR__ . '/../config/mixtelematics.php'; + if (function_exists('config_path')) { + $publishPath = config_path('mixtelematics.php'); + } else { + $publishPath = base_path('config/mixtelematics.php'); + } + + $this->publishes([ + $configPath => $publishPath + ], 'mix-config'); + } + } +} \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..a512a10 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,26 @@ +