53 lines
1.5 KiB
PHP
53 lines
1.5 KiB
PHP
<?php
|
|
namespace Incoviba\Service;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
use Incoviba\Common\Ideal;
|
|
|
|
class External extends Ideal\Service
|
|
{
|
|
public function __construct(LoggerInterface $logger, protected Queue $queueService)
|
|
{
|
|
parent::__construct($logger);
|
|
}
|
|
|
|
protected array $externalServices;
|
|
public function register($service): self
|
|
{
|
|
if (!isset($this->externalServices)) {
|
|
$this->externalServices = [$service];
|
|
return $this;
|
|
}
|
|
if (!in_array($service, $this->externalServices)) {
|
|
$this->externalServices []= $service;
|
|
}
|
|
return $this;
|
|
}
|
|
|
|
public function check(bool $update = false): bool
|
|
{
|
|
$errors = [];
|
|
foreach ($this->externalServices as $externalService) {
|
|
if (!method_exists($externalService, 'check')) {
|
|
continue;
|
|
}
|
|
$queueData = [
|
|
'type' => 'checkExternal',
|
|
'service' => get_class($externalService),
|
|
'action' => 'check',
|
|
];
|
|
if ($update) {
|
|
$queueData['args'] = ['update' => true];
|
|
}
|
|
if (!$this->queueService->enqueue($queueData)) {
|
|
$errors []= get_class($externalService);
|
|
}
|
|
}
|
|
if (count($errors) > 0) {
|
|
$this->logger->error('Could not enqueue check of external services', ['services' => $errors]);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|