Files
roxane/app/Services/Dolibarr/DolibarrService.php

73 lines
1.9 KiB
PHP

<?php
namespace App\Services\Dolibarr;
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();
}
public function setMemberStatus(int|string $id, string $status): bool
{
$response = $this->client()->put(
$this->baseUrl . '/members/' . $id,
['status' => $status]
);
return $response->successful();
}
}