From f96d8c2f50870e7aa677546c68567669d9c67be4 Mon Sep 17 00:00:00 2001 From: Sebastian Castro <sebastian.castro@protonmail.com> Date: Tue, 26 Jan 2021 10:41:44 +0100 Subject: [PATCH] config: handle null config and do not initialize in constructor --- src/Controller/APIController.php | 2 ++ src/Controller/DirectoryController.php | 4 ++-- src/Services/ConfigurationService.php | 19 ++++++++++--------- .../ElementSynchronizationService.php | 17 ++++++++++++----- src/Services/GoGoCartoJsService.php | 3 ++- src/Services/WebhookService.php | 17 +++++++++++------ 6 files changed, 39 insertions(+), 23 deletions(-) diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 5c9b42023..48c20283e 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -224,6 +224,8 @@ class APIController extends GoGoController public function getManifestAction(Request $request, DocumentManager $dm) { $config = $dm->get('Configuration')->findConfiguration(); + if (!$config) return new Response(json_encode(['error' => "No configuration found"])); + $img = $config->getFavicon() ? $config->getFavicon() : $config->getLogo(); $imageData = null; diff --git a/src/Controller/DirectoryController.php b/src/Controller/DirectoryController.php index 0a669b4c8..104174ee3 100755 --- a/src/Controller/DirectoryController.php +++ b/src/Controller/DirectoryController.php @@ -18,9 +18,9 @@ class DirectoryController extends GoGoController public function appShell(Request $request, DocumentManager $dm) { $config = $dm->get('Configuration')->findConfiguration(); - + $params = ['gogoConfigUrl' => $this->generateUrl('gogo_api_gogocartojs_configuration')]; - if( $config->getHideHeaderInPwa() ) $params['hideHeader'] = true; + $params['hideHeader'] = $config->getHideHeaderInPwa() ?? false; return $this->render('directory/directory.html.twig', $params); } diff --git a/src/Services/ConfigurationService.php b/src/Services/ConfigurationService.php index 081abb5ea..d05a6cf36 100755 --- a/src/Services/ConfigurationService.php +++ b/src/Services/ConfigurationService.php @@ -18,7 +18,6 @@ class ConfigurationService { $this->dm = $dm; $this->securityContext = $securityContext; - $this->config = $this->dm->get('Configuration')->findConfiguration(); } public function isUserAllowed($featureName, $request = null) @@ -37,19 +36,21 @@ class ConfigurationService public function getConfig() { - return $this->config; + return $this->dm->get('Configuration')->findConfiguration(); } public function getFeatureConfig($featureName) { + if (!$this->getConfig()) return null; + switch ($featureName) { - case 'report': $feature = $this->config->getReportFeature(); break; - case 'add': $feature = $this->config->getAddFeature(); break; - case 'edit': $feature = $this->config->getEditFeature(); break; - case 'directModeration': $feature = $this->config->getDirectModerationFeature(); break; - case 'delete': $feature = $this->config->getDeleteFeature(); break; - case 'vote': $feature = $this->config->getCollaborativeModerationFeature(); break; - case 'pending': $feature = $this->config->getPendingFeature(); break; + case 'report': $feature = $this->getConfig()->getReportFeature(); break; + case 'add': $feature = $this->getConfig()->getAddFeature(); break; + case 'edit': $feature = $this->getConfig()->getEditFeature(); break; + case 'directModeration': $feature = $this->getConfig()->getDirectModerationFeature(); break; + case 'delete': $feature = $this->getConfig()->getDeleteFeature(); break; + case 'vote': $feature = $this->getConfig()->getCollaborativeModerationFeature(); break; + case 'pending': $feature = $this->getConfig()->getPendingFeature(); break; } return $feature; diff --git a/src/Services/ElementSynchronizationService.php b/src/Services/ElementSynchronizationService.php index 141617cce..22eb39a65 100644 --- a/src/Services/ElementSynchronizationService.php +++ b/src/Services/ElementSynchronizationService.php @@ -14,11 +14,18 @@ use App\Document\GoGoLogLevel; class ElementSynchronizationService { + protected $config; + public function __construct(DocumentManager $dm, UrlService $urlService) { $this->dm = $dm; $this->urlService = $urlService; - $this->config = $this->dm->get('Configuration')->findConfiguration(); + } + + public function getConfig() + { + if (!$this->config) $this->config = $this->dm->get('Configuration')->findConfiguration(); + return $this->config; } /* @@ -32,12 +39,12 @@ class ElementSynchronizationService $promise = new Promise(function () use (&$promise, &$contribution, &$preparedData) { try { // Init OSM API handler - $configOsm = $this->config->getOsm(); + $configOsm = $this->getConfig()->getOsm(); $osm = new Services_OpenStreetMap([ 'server' => $this->getOsmServer(), 'user' => $configOsm->getOsmUsername(), 'password' => $configOsm->getOsmPassword(), - 'User-Agent' => $this->config->getAppName(), + 'User-Agent' => $this->getConfig()->getAppName(), 'verbose' => true ]); @@ -123,7 +130,7 @@ class ElementSynchronizationService $changeset->setTag('host', $url); $changeset->setTag('gogocarto:user', $contribution->getUserDisplayName()); $changeset->setTag('created_by:library', 'GoGoCarto'); - $changeset->setTag('created_by', $this->config->getAppName()); + $changeset->setTag('created_by', $this->getConfig()->getAppName()); $changeset->begin($this->getOsmComment($preparedData)); // Add edited feature to changeset @@ -219,7 +226,7 @@ class ElementSynchronizationService * Get OSM server URL, cleaned */ private function getOsmServer() { - $url = $this->config->getOsm()->getOsmHost(); + $url = $this->getConfig()->getOsm()->getOsmHost(); if(isset($url)) { if(!str_starts_with($url, "http://") && !str_starts_with($url, "https://")) { $url = "https://" + $url; diff --git a/src/Services/GoGoCartoJsService.php b/src/Services/GoGoCartoJsService.php index a1e5ea1d1..68034b24e 100644 --- a/src/Services/GoGoCartoJsService.php +++ b/src/Services/GoGoCartoJsService.php @@ -29,7 +29,8 @@ class GoGoCartoJsService $taxonomyJson = $taxonomyRep->findTaxonomyJson(); $config = $this->dm->get('Configuration')->findConfiguration(); - + if (!$config) return []; + $user = $this->securityContext->getToken() ? $this->securityContext->getToken()->getUser() : null; $roles = is_object($user) ? $user->getRoles() : []; diff --git a/src/Services/WebhookService.php b/src/Services/WebhookService.php index 8cb39d166..922008402 100755 --- a/src/Services/WebhookService.php +++ b/src/Services/WebhookService.php @@ -21,7 +21,7 @@ use Psr\Log\LoggerInterface; class WebhookService { protected $dm; - + protected $config; protected $router; public function __construct(DocumentManager $dm, RouterInterface $router, @@ -34,11 +34,16 @@ class WebhookService $this->router = $router; $this->urlService = $urlService; $this->securityContext = $securityContext; - $this->config = $this->dm->get('Configuration')->findConfiguration(); $this->synchService = $synchService; $this->logger = $commandsLogger; } + public function getConfig() + { + if (!$this->config) $this->config = $this->dm->get('Configuration')->findConfiguration(); + return $this->config; + } + public function processPosts($limit = 5) { $contributions = $this->dm->createQueryBuilder(UserInteractionContribution::class) @@ -139,7 +144,7 @@ class WebhookService private function getNotificationText($result) { - $element = $this->config->getElementDisplayName(); + $element = $this->getConfig()->getElementDisplayName(); switch ($result['action']) { case 'add': return "**AJOUT** {$element} **{$result['data']['name']}** ajouté par {$result['user']}\n[Lien vers la fiche]({$result['link']})"; @@ -157,7 +162,7 @@ class WebhookService private function getBatchNotificationText($result) { - $elements = $this->config->getElementDisplayNamePlural(); + $elements = $this->getConfig()->getElementDisplayNamePlural(); $title = $this->transTitle[$result['action']]; $text = $this->transText[$result['action']]; $count = count($result['data']['ids']); @@ -168,7 +173,7 @@ class WebhookService private function getBotIcon() { /** @var ConfImage $img */ - $img = $this->config->getFavicon() ? $this->config->getFavicon() : $this->config->getLogo(); + $img = $this->getConfig()->getFavicon() ? $this->getConfig()->getFavicon() : $this->getConfig()->getLogo(); return $img ? $img->getImageUrl() : $this->urlService->getAssetUrl('/img/default-icon.png'); } @@ -181,7 +186,7 @@ class WebhookService case WebhookFormat::Mattermost: return [ - 'username' => $this->config->getAppName(), + 'username' => $this->getConfig()->getAppName(), 'icon_url' => $this->getBotIcon(), 'text' => $data['text'], ]; -- GitLab