Skip to content

Commit

Permalink
First iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
Combind committed Mar 22, 2023
1 parent 4f76f19 commit 2bc2547
Show file tree
Hide file tree
Showing 11 changed files with 245 additions and 9 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ Optionally, you can publish the views using
php artisan vendor:publish --tag="laravel-mautic-views"
```

## Mautic Setup
The API must be enabled in Mautic.

Within Mautic, go to the Configuration page (located in the Settings menu) and under API Settings enable Mautic's API.

After saving the configuration, go to the API Credentials page (located in the Settings menu) and create a new client.

Enter the callback/redirect URI (Should be `https://your-domain.com/login/mautic/callback`). Click Apply, then copy the Client ID and Client Secret to the .env file.

This is an example of .env file:

```
MAUTIC_BASE_URL="https://your-domain.com"
MAUTIC_PUBLIC_KEY="XXXXXXXXXXXXXXXX"
MAUTIC_SECRET_KEY="XXXXXXXXXX"
MAUTIC_CALLBACK="https://your-domain.com/login/mautic/callback"
```

## Usage

```php
Expand Down
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords": [
"combindma",
"laravel",
"laravel-mautic"
"mautic"
],
"homepage": "https://github.com/combindma/laravel-mautic",
"license": "MIT",
Expand All @@ -16,9 +16,12 @@
}
],
"require": {
"php": "^8.2",
"spatie/laravel-package-tools": "^1.14.0",
"illuminate/contracts": "^10.0"
"php": "^8.1",
"graham-campbell/manager": "^5.0",
"guzzlehttp/guzzle": "^7.5",
"illuminate/contracts": "^10.0",
"mautic/api-library": "^3.1",
"spatie/laravel-package-tools": "^1.14.0"
},
"require-dev": {
"laravel/pint": "^1.0",
Expand Down
29 changes: 28 additions & 1 deletion config/mautic.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
<?php

// config for Combindma/Mautic
return [
/*
* Base URL of the Mautic instance
*/
'baseUrl' => env('MAUTIC_BASE_URL'),

/*
* Client/Consumer key from Mautic
*/
'clientKey' => env('MAUTIC_PUBLIC_KEY'),

/*
* Client/Consumer secret key from Mautic
*/
'clientSecret' => env('MAUTIC_SECRET_KEY'),

/*
* Redirect URI/Callback URI for this script
*/
'callback' => env('MAUTIC_CALLBACK'),

/*
* Enable or disable Mautic. Useful for local development when running tests.
*/
'apiEnabled' => env('MAUTIC_ENABLED', false),

/*
* Filename to use when storing mautic access token. Must be a json File
*/
'fileName' => 'mautic.json',
];
File renamed without changes.
14 changes: 14 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/*
|--------------------------------------------------------------------------
| Mautic Application Routes
|--------------------------------------------------------------------------
|
*/

use Combindma\Mautic\Http\Controllers\MauticController;
use Illuminate\Support\Facades\Route;

Route::get('login/mautic', [MauticController::class, 'login']);
Route::get('login/mautic/callback', [MauticController::class, 'callback']);
14 changes: 14 additions & 0 deletions src/Exceptions/UnableToStoreMauticDataException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Combindma\Mautic\Exceptions;

use Exception;
use Illuminate\Support\Facades\Log;

class UnableToStoreMauticDataException extends Exception
{
public function report()
{
Log::debug('Unable to store Mautic data in your local disk.');
}
}
4 changes: 2 additions & 2 deletions src/Facades/Mautic.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*/
class Mautic extends Facade
{
protected static function getFacadeAccessor()
protected static function getFacadeAccessor(): string
{
return 'laravel-mautic';
return \Combindma\Mautic\Mautic::class;
}
}
13 changes: 13 additions & 0 deletions src/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Combindma\Mautic\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
use AuthorizesRequests;
use ValidatesRequests;
}
26 changes: 26 additions & 0 deletions src/Http/Controllers/MauticController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Combindma\Mautic\Http\Controllers;

use Combindma\Mautic\Exceptions\UnableToStoreMauticDataException;
use Combindma\Mautic\Facades\Mautic;
use Illuminate\Support\Facades\Storage;

class MauticController extends Controller
{
public function login()
{
Mautic::getAccessTokenData();

return view('mautic::index');
}

public function callback()
{
$mauticData = Mautic::getAccessTokenData();
// The file could not be written to disk...
if (! Storage::disk('local')->put(config('mautic.fileName'), json_encode($mauticData))) {
throw new UnableToStoreMauticDataException('Unable to store Mautic data.');
}
}
}
113 changes: 113 additions & 0 deletions src/Mautic.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,119 @@

namespace Combindma\Mautic;

use Exception;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Mautic\Auth\ApiAuth;

class Mautic
{
private string $baseUrl;

private string $clientKey;

private string $clientSecret;

private string $callback;

private bool $apiEnabled;

private string $fileName;

private array $settings;

public function __construct()
{
$this->baseUrl = config('mautic.baseUrl');
$this->clientKey = config('mautic.clientKey');
$this->clientSecret = config('mautic.clientSecret');
$this->callback = config('mautic.callback');
$this->fileName = config('mautic.fileName');
$this->settings = [
'baseUrl' => $this->baseUrl,
'version' => 'OAuth2',
'clientKey' => $this->clientKey,
'clientSecret' => $this->clientSecret,
'callback' => $this->callback,
];
}

public function getBaseUrl(): string
{
return $this->baseUrl;
}

public function getClientKey(): string
{
return $this->clientKey;
}

public function getClientSecret(): string
{
return $this->clientSecret;
}

public function getCallback(): string
{
return $this->callback;
}

public function isApiEnabled(): bool
{
return $this->apiEnabled;
}

public function getFileName(): string
{
return $this->fileName;
}

public function setSettings(array $settings): void
{
$this->settings = array_merge($this->settings, $settings);
}

public function disable(): void
{
$this->apiEnabled = false;
}

public function enable(): void
{
$this->apiEnabled = true;
}

/*
* Initiate process for obtaining an access token; this will redirect the user to the $authorizationUrl
* and/or set the access_tokens when the user is redirected back after granting authorization
* If the access token is expired, and a refresh token is set above, then a new access token will be requested
* */
public function getAccessTokenData(): ?array
{
session_start();

$mautic = json_decode(Storage::disk('local')->get($this->getFileName()));
if ($mautic) {
$this->setSettings([
'accessToken' => $mautic->access_token,
'accessTokenExpires' => $mautic->expires,
'refreshToken' => $mautic->refresh_token,
]);
}
// Initiate the auth object
$auth = (new ApiAuth())->newAuth($this->settings);
try {
if ($auth->validateAccessToken()) {
// Obtain the access token returned; call accessTokenUpdated() to catch if the token was updated via a refresh token
// $accessTokenData will have the following keys: access_token, expires, token_type, refresh_token
if ($auth->accessTokenUpdated()) {
return $auth->getAccessTokenData();
}
}
} catch (Exception $e) {
Log::error($e);
}

return null;
}
}
12 changes: 10 additions & 2 deletions src/MauticServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ public function configurePackage(Package $package): void
{
$package
->name('laravel-mautic')
->hasConfigFile()
->hasViews();
->hasConfigFile('mautic')
->hasViews()
->hasRoute('web');
}

public function packageRegistered()
{
$this->app->singleton('laravel-mautic', function () {
return new Mautic();
});
}
}

0 comments on commit 2bc2547

Please sign in to comment.