From b94985f579d0741bd2e9a5a6e56ed67fd5cfd602 Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Thu, 3 Mar 2022 13:03:40 +0100
Subject: [PATCH] Add some basic health checks

---
 src/Resources/config/services.yaml |  4 ++
 src/Service/CheckinApi.php         | 22 ++++++++++
 src/Service/HealthCheck.php        | 64 ++++++++++++++++++++++++++++++
 3 files changed, 90 insertions(+)
 create mode 100644 src/Service/HealthCheck.php

diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml
index f556567..b7b35d3 100644
--- a/src/Resources/config/services.yaml
+++ b/src/Resources/config/services.yaml
@@ -3,6 +3,10 @@ services:
     public: false
     autowire: true
 
+  Dbp\Relay\CheckinBundle\Service\HealthCheck:
+    autowire: true
+    autoconfigure: true
+
   Dbp\Relay\CheckinBundle\DataPersister\:
     resource: '../../DataPersister'
     autowire: true
diff --git a/src/Service/CheckinApi.php b/src/Service/CheckinApi.php
index 31a6f65..4f61c01 100644
--- a/src/Service/CheckinApi.php
+++ b/src/Service/CheckinApi.php
@@ -141,6 +141,28 @@ class CheckinApi implements LoggerAwareInterface
         return new Client($client_options);
     }
 
+    /**
+     * Check if CampusQR is reachable.
+     */
+    public function checkConnection(): void
+    {
+        $client = $this->getClient();
+        $client->request('GET', $this->campusQRUrl);
+    }
+
+    /**
+     * Check if we can talk to the API.
+     */
+    public function checkApi(): void
+    {
+        $client = $this->getClient();
+        $options = [
+            'headers' => ['X-Authorization' => $this->campusQRToken],
+        ];
+        $url = $this->urls->getLocationListRequestUrl($this->campusQRUrl);
+        $client->request('GET', $url, $options);
+    }
+
     private function getLocationClient(): Client
     {
         $stack = HandlerStack::create($this->clientHandler);
diff --git a/src/Service/HealthCheck.php b/src/Service/HealthCheck.php
new file mode 100644
index 0000000..ccb5c4e
--- /dev/null
+++ b/src/Service/HealthCheck.php
@@ -0,0 +1,64 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Dbp\Relay\CheckinBundle\Service;
+
+use Dbp\Relay\CoreBundle\HealthCheck\CheckInterface;
+use Dbp\Relay\CoreBundle\HealthCheck\CheckOptions;
+use Dbp\Relay\CoreBundle\HealthCheck\CheckResult;
+
+class HealthCheck implements CheckInterface
+{
+    private $api;
+
+    public function __construct(CheckinApi $api)
+    {
+        $this->api = $api;
+    }
+
+    public function getName(): string
+    {
+        return 'checkin';
+    }
+
+    public function checkConnection(): CheckResult
+    {
+        $result = new CheckResult('Check if CampusQR is reachable');
+        try {
+            $this->api->checkConnection();
+        } catch (\Throwable $e) {
+            $result->set(CheckResult::STATUS_FAILURE, $e->getMessage());
+
+            return $result;
+        }
+
+        $result->set(CheckResult::STATUS_SUCCESS);
+
+        return $result;
+    }
+
+    public function checkApi(): CheckResult
+    {
+        $result = new CheckResult('Check if we can use the CampusQR API');
+        try {
+            $this->api->checkApi();
+        } catch (\Throwable $e) {
+            $result->set(CheckResult::STATUS_FAILURE, $e->getMessage());
+
+            return $result;
+        }
+
+        $result->set(CheckResult::STATUS_SUCCESS);
+
+        return $result;
+    }
+
+    public function check(CheckOptions $options): array
+    {
+        return [
+            $this->checkConnection(),
+            $this->checkApi(),
+        ];
+    }
+}
-- 
GitLab