Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aislandener committed Jan 8, 2022
0 parents commit 0f09791
Show file tree
Hide file tree
Showing 12 changed files with 260 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor/
/.idea/
/.vscode/
composer.lock
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":[],"times":[]}
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# mix-telematics-laravel
MiX Telematics Communication API with Laravel
44 changes: 44 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
}
],
"minimum-stability": "dev"
}
11 changes: 11 additions & 0 deletions config/mixtelematics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
return [
'clientName' => 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'),
];
33 changes: 33 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
>
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<php>
<env name="DB_CONNECTION" value="testing"/>
<env name="APP_KEY" value="base64:2fl+Ktvkfl+Fuz4Qp/A75G2RTiWVA/ZoKZvp6fiiM10="/>
</php>
</phpunit>
17 changes: 17 additions & 0 deletions src/Facades/MixTelematics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Aislandener\MixTelematicsLaravel\Facades;

use Illuminate\Support\Facades\Facade;

/**
* @method static string getToken()
* @method static array getDriversOrganisation()
*/
class MixTelematics extends Facade
{
protected static function getFacadeAccessor()
{
return 'MixTelematics';
}
}
61 changes: 61 additions & 0 deletions src/MixTelematicsLaravelService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Aislandener\MixTelematicsLaravel;

use Http;
use Jumbojett\OpenIDConnectClient;
use Jumbojett\OpenIDConnectClientException;

class MixTelematicsLaravelService
{
private $clientName;
private $clientID;
private $clientSecret;
private $idBaseUrl;
private $restBaseUrl;
private $dynamixUserName;
private $dynamixUserPassword;
private $organisationId;

private $token;

const SCOPE = "offline_access MiX.Integrate";

public function __construct()
{
$this->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();
}
}
34 changes: 34 additions & 0 deletions src/MixTelematicsServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Aislandener\MixTelematicsLaravel;

use Illuminate\Support\ServiceProvider;

class MixTelematicsServiceProvider extends ServiceProvider
{
public function register()
{
$configPath = __DIR__ . '/../config/mixtelematics.php';
$this->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');
}
}
}
26 changes: 26 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Aislandener\MixTelematicsLaravel\Tests;

use Aislandener\MixTelematicsLaravel\MixTelematicsServiceProvider;

class TextCase extends \Orchestra\Testbench\TestCase
{
public function setUp(): void
{
parent::setUp();
// additional setup
}

protected function getPackageProviders($app)
{
return [
MixTelematicsServiceProvider::class,
];
}

protected function getEnvironmentSetUp($app)
{
// perform environment setup
}
}

0 comments on commit 0f09791

Please sign in to comment.