From a56ac8815295ed4d4ffeda598fa63863c57c97b1 Mon Sep 17 00:00:00 2001
From: Oliver Hader <oliver@typo3.org>
Date: Wed, 15 Mar 2023 11:04:49 +0100
Subject: [PATCH] [TASK] Convert class ApplicationType to enum

Converting the class to an enum is possible since
 * the constructor is private,
 * property $type is not exposed,
 * public functions are kept.

Resolves: #100172
Releases: main
Change-Id: I2ce736fd93f64a288300cfd7f09c5a63d18552b7
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/78129
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Benni Mack <benni@typo3.org>
Tested-by: core-ci <typo3@b13.com>
Reviewed-by: Benni Mack <benni@typo3.org>
---
 .../core/Classes/Http/ApplicationType.php     | 31 ++++++++++++++-----
 .../Tests/Unit/Http/ApplicationTypeTest.php   | 20 ++++++++++++
 2 files changed, 44 insertions(+), 7 deletions(-)

diff --git a/typo3/sysext/core/Classes/Http/ApplicationType.php b/typo3/sysext/core/Classes/Http/ApplicationType.php
index 7be94a0be23e..1fbd21063e9f 100644
--- a/typo3/sysext/core/Classes/Http/ApplicationType.php
+++ b/typo3/sysext/core/Classes/Http/ApplicationType.php
@@ -52,9 +52,12 @@ use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
  * can NOT be answered in the TYPO3 bootstrap related extension files ext_localconf.php,
  * ext_tables.php and Configuration/TCA/* files.
  */
-final class ApplicationType
+enum ApplicationType: string
 {
-    private int $type;
+    // SystemEnvironmentBuilder::REQUESTTYPE_BE
+    case BACKEND = 'backend';
+    // SystemEnvironmentBuilder::REQUESTTYPE_FE
+    case FRONTEND = 'frontend';
 
     /**
      * Create an ApplicationType object from a given PSR-7 request.
@@ -70,21 +73,35 @@ final class ApplicationType
             // This is bogus, we throw a generic RuntimeException that should not be caught.
             throw new RuntimeException('No valid attribute "applicationType" found in request object.', 1606222812);
         }
-        return new self($type);
+        if (($type & SystemEnvironmentBuilder::REQUESTTYPE_FE) === SystemEnvironmentBuilder::REQUESTTYPE_FE) {
+            return self::FRONTEND;
+        }
+        if (($type & SystemEnvironmentBuilder::REQUESTTYPE_BE) === SystemEnvironmentBuilder::REQUESTTYPE_BE) {
+            return self::BACKEND;
+        }
+        throw new \LogicException('Could not resolve application type to either frontend or backend', 1678875015);
     }
 
-    private function __construct(int $type)
+    public function abbreviate(): ?string
     {
-        $this->type = $type;
+        return self::abbreviations()[$this] ?? null;
     }
 
     public function isFrontend(): bool
     {
-        return (bool)($this->type & SystemEnvironmentBuilder::REQUESTTYPE_FE);
+        return $this === self::FRONTEND;
     }
 
     public function isBackend(): bool
     {
-        return (bool)($this->type & SystemEnvironmentBuilder::REQUESTTYPE_BE);
+        return $this === self::BACKEND;
+    }
+
+    private static function abbreviations(): \WeakMap
+    {
+        $map = new \WeakMap();
+        $map[self::FRONTEND] = 'FE';
+        $map[self::BACKEND] = 'BE';
+        return $map;
     }
 }
diff --git a/typo3/sysext/core/Tests/Unit/Http/ApplicationTypeTest.php b/typo3/sysext/core/Tests/Unit/Http/ApplicationTypeTest.php
index e921e2bbaae5..bc945e3d2293 100644
--- a/typo3/sysext/core/Tests/Unit/Http/ApplicationTypeTest.php
+++ b/typo3/sysext/core/Tests/Unit/Http/ApplicationTypeTest.php
@@ -77,4 +77,24 @@ class ApplicationTypeTest extends UnitTestCase
                 ->isBackend()
         );
     }
+
+    /**
+     * @test
+     */
+    public function isFrontendEnumResolved(): void
+    {
+        $type = ApplicationType::FRONTEND;
+        self::assertSame('frontend', $type->value);
+        self::assertSame('FE', $type->abbreviate());
+    }
+
+    /**
+     * @test
+     */
+    public function isBackendEnumResolved(): void
+    {
+        $type = ApplicationType::BACKEND;
+        self::assertSame('backend', $type->value);
+        self::assertSame('BE', $type->abbreviate());
+    }
 }
-- 
GitLab