diff --git a/src/Service/HealthCheck.php b/src/Service/HealthCheck.php new file mode 100644 index 0000000000000000000000000000000000000000..835648c875287997c9cd26cac118ce9b62a7d48b --- /dev/null +++ b/src/Service/HealthCheck.php @@ -0,0 +1,47 @@ +<?php + +declare(strict_types=1); + +namespace Dbp\Relay\BasePersonConnectorLdapBundle\Service; + +use Dbp\Relay\CoreBundle\HealthCheck\CheckInterface; +use Dbp\Relay\CoreBundle\HealthCheck\CheckOptions; +use Dbp\Relay\CoreBundle\HealthCheck\CheckResult; + +class HealthCheck implements CheckInterface +{ + private $ldap; + + public function __construct(LDAPApi $ldap) + { + $this->ldap = $ldap; + } + + public function getName(): string + { + return 'base-person-connector-ldap'; + } + + private function checkMethod(string $description, callable $func): CheckResult + { + $result = new CheckResult($description); + try { + $func(); + } catch (\Throwable $e) { + $result->set(CheckResult::STATUS_FAILURE, $e->getMessage(), ['exception' => $e]); + + return $result; + } + $result->set(CheckResult::STATUS_SUCCESS); + + return $result; + } + + public function check(CheckOptions $options): array + { + $results = []; + $results[] = $this->checkMethod('Check if we can connect to the LDAP server', [$this->ldap, 'checkConnection']); + + return $results; + } +} diff --git a/src/Service/LDAPApi.php b/src/Service/LDAPApi.php index 30e4bc41950df6c20604bcde727735eb8fc71140..458f8fd7b59ea1d47f8d68e4cafda820dfff27f9 100644 --- a/src/Service/LDAPApi.php +++ b/src/Service/LDAPApi.php @@ -106,6 +106,13 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface $this->providerConfig['port'] = ($encryption === 'start_tls') ? 389 : 636; } + public function checkConnection() + { + $provider = $this->getProvider(); + $builder = $this->getCachedBuilder($provider); + $builder->first(); + } + public function setDeploymentEnvironment(string $env) { $this->deploymentEnv = $env;