adrema/app/Mailman/Support/MailmanService.php

83 lines
2.8 KiB
PHP
Raw Normal View History

2022-10-18 15:04:47 +02:00
<?php
namespace App\Mailman\Support;
2022-10-20 11:11:52 +02:00
use App\Mailman\Data\MailingList;
2022-10-18 16:44:36 +02:00
use App\Mailman\Exceptions\MailmanServiceException;
2022-10-20 02:15:28 +02:00
use Illuminate\Http\Client\ConnectionException;
2022-10-18 15:21:14 +02:00
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
2022-10-18 16:44:36 +02:00
use Illuminate\Support\LazyCollection;
2022-10-18 17:55:53 +02:00
use Zoomyboy\LaravelNami\Support\Paginator;
2022-10-18 15:21:14 +02:00
2022-10-18 15:04:47 +02:00
class MailmanService
{
2022-10-18 15:21:14 +02:00
private string $baseUrl;
private string $username;
private string $password;
2022-10-18 15:04:47 +02:00
public function setCredentials(string $baseUrl, string $username, string $password): self
{
2022-10-18 15:21:14 +02:00
$this->baseUrl = $baseUrl;
$this->username = $username;
$this->password = $password;
return $this;
2022-10-18 15:04:47 +02:00
}
public function check(): bool
{
2022-10-20 02:15:28 +02:00
try {
$response = $this->http()->get('/system/versions');
2022-10-18 15:21:14 +02:00
2022-10-20 02:15:28 +02:00
return 200 === $response->status();
} catch (ConnectionException $e) {
return false;
}
2022-10-18 15:21:14 +02:00
}
2022-10-18 16:44:36 +02:00
/**
* @return LazyCollection<int, string>
*/
public function members(string $listId): LazyCollection
{
2022-10-18 17:55:53 +02:00
return app(Paginator::class)->result(
fn ($page) => $this->http()->get("/lists/{$listId}/roster/member?page={$page}&count=10"),
function ($response) use ($listId) {
2022-10-18 16:44:36 +02:00
throw_unless($response->ok(), MailmanServiceException::class, 'Fetching members for listId '.$listId.' failed.');
/** @var array<int, array{email: string}>|null */
$entries = data_get($response->json(), 'entries');
throw_if(is_null($entries), MailmanServiceException::class, 'Failed getting member list from response');
foreach ($entries as $entry) {
yield $entry['email'];
}
2022-10-18 17:55:53 +02:00
},
fn ($response) => data_get($response->json(), 'total_size')
);
2022-10-18 16:44:36 +02:00
}
2022-10-18 15:21:14 +02:00
private function http(): PendingRequest
{
return Http::withBasicAuth($this->username, $this->password)->withOptions(['base_uri' => $this->baseUrl]);
2022-10-18 15:04:47 +02:00
}
2022-10-20 11:11:52 +02:00
public function getLists(): LazyCollection
{
return app(Paginator::class)->result(
fn ($page) => $this->http()->get("/lists?page={$page}&count=10"),
function ($response) {
throw_unless($response->ok(), MailmanServiceException::class, 'Fetching lists failed.');
/** @var array<int, array{email: string}>|null */
$entries = data_get($response->json(), 'entries');
throw_if(is_null($entries), MailmanServiceException::class, 'Failed getting lists from response');
foreach ($entries as $entry) {
yield MailingList::from($entry);
}
},
fn ($response) => data_get($response->json(), 'total_size')
);
}
2022-10-18 15:04:47 +02:00
}