Files
provider_imap/lib/Service/Remote/RemoteService.php
2026-05-08 00:16:43 -04:00

62 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderImap\Service\Remote;
use KTXC\Server;
use KTXC\Logger\PlainFileLogger;
use KTXM\ProviderImap\Client\Client;
use KTXM\ProviderImap\Providers\Service;
/**
* Static factory for IMAP remote service objects.
*
* - freshClient() → builds a gricob Client from service config
* - mailService() → constructs a RemoteMailService from the client
*/
class RemoteService
{
/**
* Build and bootstrap a fully-configured IMAP client from a Service.
*/
public static function freshClient(Service $service): Client
{
$location = $service->getLocation();
$identity = $service->getIdentity();
// Build a file logger when debug mode is enabled, otherwise pass null
$logger = null;
if ($service->getDebug()) {
$logDir = Server::getInstance()?->logDir() ?? __DIR__ . '/../../../../../var/log';
$logger = new PlainFileLogger($logDir . '/imap', $service->identifier());
}
$config = $location->toConnectionConfig(
$identity?->getIdentity(),
$identity?->getSecret(),
);
$client = new Client(logger: $logger);
$client->connect($config);
return $client;
}
/**
* Build a RemoteMailService from a Service and a pre-authenticated client.
*
* The provider identifier and service ID are taken directly from the Service
* object so the caller does not have to repeat them.
*/
public static function mailService(Service $service, Client $client): RemoteMailService
{
return new RemoteMailService($client, $service->provider(), $service->identifier());
}
}