From 3b73c67250822d58e6aebcceb7733df60c6454fa Mon Sep 17 00:00:00 2001
From: Yassine Doghri <yassine@doghri.fr>
Date: Wed, 29 Jun 2022 16:19:22 +0000
Subject: [PATCH] refactor(rest-api): move rest api's enabled flag to the
 RestApi config

---
 .env.example                                        |  6 +++---
 modules/Api/Rest/V1/Config/{Api.php => RestApi.php} | 10 ++++++++--
 modules/Api/Rest/V1/Config/Routes.php               |  2 +-
 modules/Api/Rest/V1/Filters/ApiFilter.php           |  2 +-
 phpunit.xml.dist                                    |  2 +-
 tests/modules/Api/Rest/V1/PodcastTest.php           |  2 +-
 6 files changed, 15 insertions(+), 9 deletions(-)
 rename modules/Api/Rest/V1/Config/{Api.php => RestApi.php} (71%)

diff --git a/.env.example b/.env.example
index 2844aa83b1..e9536360d1 100644
--- a/.env.example
+++ b/.env.example
@@ -42,7 +42,7 @@ cache.handler="file"
 # cache.redis.port=6379
 # cache.redis.database=0
 
-#REST API configuration
 #--------------------------------------------------------------------
-# 0/1 Disabled/Enabled
-REST_API_ENABLED=1
\ No newline at end of file
+# REST API configuration
+#--------------------------------------------------------------------
+# restapi.enabled=true
diff --git a/modules/Api/Rest/V1/Config/Api.php b/modules/Api/Rest/V1/Config/RestApi.php
similarity index 71%
rename from modules/Api/Rest/V1/Config/Api.php
rename to modules/Api/Rest/V1/Config/RestApi.php
index 849732e05c..d32a6cb9ed 100644
--- a/modules/Api/Rest/V1/Config/Api.php
+++ b/modules/Api/Rest/V1/Config/RestApi.php
@@ -1,14 +1,20 @@
 <?php
 
-
 declare(strict_types=1);
 
 namespace Modules\Api\Rest\V1\Config;
 
 use CodeIgniter\Config\BaseConfig;
 
-class Api extends BaseConfig
+class RestApi extends BaseConfig
 {
+    /**
+     * Flag to enable or disable the Rest API.
+     *
+     * Disabled by default.
+     */
+    public bool $enabled = false;
+
     /**
      * --------------------------------------------------------------------------
      * Rest API gateway
diff --git a/modules/Api/Rest/V1/Config/Routes.php b/modules/Api/Rest/V1/Config/Routes.php
index f40467e03f..e1c8804f2b 100644
--- a/modules/Api/Rest/V1/Config/Routes.php
+++ b/modules/Api/Rest/V1/Config/Routes.php
@@ -7,7 +7,7 @@ namespace Modules\Api\Rest\V1\Config;
 $routes = service('routes');
 
 $routes->group(
-    config('Api')
+    config('RestApi')
         ->gateway . 'podcasts',
     [
         'namespace' => 'Modules\Api\Rest\V1\Controllers',
diff --git a/modules/Api/Rest/V1/Filters/ApiFilter.php b/modules/Api/Rest/V1/Filters/ApiFilter.php
index efd23f3533..d6e6b32afc 100644
--- a/modules/Api/Rest/V1/Filters/ApiFilter.php
+++ b/modules/Api/Rest/V1/Filters/ApiFilter.php
@@ -13,7 +13,7 @@ class ApiFilter implements FilterInterface
 {
     public function before(RequestInterface $request, $arguments = null): void
     {
-        if (! getenv('REST_API_ENABLED')) {
+        if (! config('RestApi')->enabled) {
             throw PageNotFoundException::forPageNotFound();
         }
     }
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index 7395ebae2c..dcd8692300 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -51,6 +51,6 @@
 		<env name="database.tests.password" value="castopod"/>
 		<env name="database.tests.DBDriver" value="MySQLi"/>
 		<env name="database.tests.DBPrefix" value="tests_"/>
-		<env name="REST_API_ENABLED" value="1"/>
+		<env name="restapi.enabled" value="true"/>
 	</php>
 </phpunit>
diff --git a/tests/modules/Api/Rest/V1/PodcastTest.php b/tests/modules/Api/Rest/V1/PodcastTest.php
index 56a2f9c4ff..ba6bc019b8 100644
--- a/tests/modules/Api/Rest/V1/PodcastTest.php
+++ b/tests/modules/Api/Rest/V1/PodcastTest.php
@@ -55,7 +55,7 @@ class PodcastTest extends CIUnitTestCase
         $this->podcast = FakeSinglePodcastApiSeeder::podcast();
         $this->podcast['created_at'] = [];
         $this->podcast['updated_at'] = [];
-        $this->podcastApiUrl = config('Api')
+        $this->podcastApiUrl = config('RestApi')
             ->gateway;
     }
 
-- 
GitLab