feat-wip(ISPConfig & Dolibarr connections)

This commit is contained in:
2025-11-14 17:27:39 +01:00
parent 94a5794bab
commit de5e81fe22
10 changed files with 370 additions and 6 deletions

View File

@@ -0,0 +1,86 @@
<?php
namespace App\Console\Commands;
use App\Models\Member;
use App\Services\DolibarrService;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Http\Client\ConnectionException;
use function Deployer\timestamp;
class SyncDolibarrMembers extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:sync-dolibarr-members';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Retrieve members data from Dolibarr';
/**
* Execute the console command.
* @throws ConnectionException
*/
public function handle(): void
{
$this->info('Starting Dolibarr members import...');
// Dolibarr API call
$client = new DolibarrService;
$doliMembers = collect($client->getAllMembers());
$progressBar = $this->output->createProgressBar(count($doliMembers));
$progressBar->start();
$i = 0;
foreach ($doliMembers as $member) {
dd($member);
$newMember = Member::updateOrCreate([
'dolibarr_id' => $member->id,
], [
'status' => $member['status'], // @todo: faire concorder les statuts
'nature' => 'physical',
'member_type' => $member['type'],
'group_id' => null,
'lastname' => $member['firstname'],
'firstname' => $member['lastname'],
'email' => $member['email'],
'personal_email' => '',
'company' => '',
'website_url' => $member['url'],
'date_of_birth' => '',
'address' => '',
'zipcode' => '',
'city' => '',
'country' => '',
'phone1' => '',
'phone2' => '',
'public_membership' => '',
'created_at' => Carbon::create($member['date_creation'])->format(timestamp()),
'updated_at' => '',
]);
// On crée l'adhérent en remplissant les données en bdd avec ses coordonnées, son statut etc ...
// On récupère toutes les adhésions/cotisations pour chaque adhérent
$memberships = $client->getMemberSubscriptions($member->id);
// on traite les notes (privée, publique, lien ect) contenues dans dolibarr
$i++;
}
$progressBar->finish();
// Logs
$this->info('Import finished. ' .$i.' members have been imported.');
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace App\Services;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
class DolibarrService
{
protected string $baseUrl;
protected string $username;
protected string $password;
protected string $apiKey;
public function __construct()
{
$this->baseUrl = config('services.dolibarr.base_url');
$this->username = config('services.dolibarr.username');
$this->password = config('services.dolibarr.password');
$this->apiKey = config('services.dolibarr.api_key');
}
/**
* Create an authenticated HTTP client for Dolibarr
*/
protected function client(): PendingRequest
{
return Http::withBasicAuth($this->username, $this->password)
->withHeaders([
'Accept' => 'application/json',
'DOLAPIKEY' => $this->apiKey,
]);
}
/**
* Get all members
* @throws ConnectionException
*/
public function getAllMembers(int $limit = 400, string $sortField = 't.rowid', string $sortOrder = 'ASC'): array
{
$response = $this->client()->get($this->baseUrl . '/members', [
'sortfield' => $sortField,
'sortorder' => $sortOrder,
'limit' => $limit,
]);
return $response->json();
}
/**
* Get member subscriptions
* @throws ConnectionException
*/
public function getMemberSubscriptions(int|string $id): array
{
$response = $this->client()->get($this->baseUrl . '/members/'. $id . '/subscriptions');
return $response->json();
}
}

View File

@@ -0,0 +1,89 @@
<?php
namespace App\Services;
use Illuminate\Support\Facades\Log;
use SoapClient;
use SoapFault;
use Exception;
class ISPConfigService
{
protected ?SoapClient $client = null;
protected ?string $sessionId = null;
public function __construct()
{
}
/**
* ISPConfig Login
*/
public function connect(string $type): void
{
// Type = 'hosting' or 'mailbox'
$username = $username ?? config('services.ispconfig'.$type.'.username');
$password = $password ?? config('services.ispconfig'.$type.'.password');
try {
$this->client = new SoapClient(null, [
'location' => config('services.ispconfig' . $type . '.base_url'),
'trace' => true,
'exceptions' => true,
'stream_context' => stream_context_create([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
],
]),
]);
$this->sessionId = $this->client->login($username, $password);
} catch (SoapFault $e) {
throw new Exception("An error occurred : " . $e->getMessage());
}
}
/**
* Get all clients
*/
public function getAllClients(string $username = null, string $password = null): array
{
if (!$this->sessionId) {
$this->connect($username, $password);
}
try {
$clientIds = $this->client->client_get_all($this->sessionId);
$clients = [];
foreach ($clientIds as $id) {
$details = $this->client->client_get($this->sessionId, (int)$id);
if (!empty($details)) {
$clients[] = (array) $details;
}
}
return $clients;
} catch (SoapFault $e) {
throw new Exception("An error occurred : " . $e->getMessage());
} finally {
$this->disconnect();
}
}
/**
* Logout
*/
public function disconnect(): void
{
if ($this->client && $this->sessionId) {
try {
$this->client->logout($this->sessionId);
} catch (SoapFault $e) {
Log::info('ISP Config logout succeeded');
}
}
}
}