From c6aa1311adef8bb4815261f1d5a85c2272d1d86c Mon Sep 17 00:00:00 2001 From: Pavel Dohnal Date: Wed, 29 Jan 2020 09:25:25 +0100 Subject: [PATCH] Refactor [MAILPOET-2639] --- lib/Form/DisplayFormInWPContent.php | 37 +++++++++++++++++++---------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/lib/Form/DisplayFormInWPContent.php b/lib/Form/DisplayFormInWPContent.php index 8b811bfef8..7baf9fbf4c 100644 --- a/lib/Form/DisplayFormInWPContent.php +++ b/lib/Form/DisplayFormInWPContent.php @@ -15,32 +15,45 @@ class DisplayFormInWPContent { /** @var FormsRepository */ private $formsRepository; - /** @var bool */ - private $appendedForm = false; - public function __construct(WPFunctions $wp, FormsRepository $formsRepository) { $this->wp = $wp; $this->formsRepository = $formsRepository; } public function display(string $content): string { - $this->appendedForm = false; + if(!$this->shouldDisplay()) return $content; + + $forms = $this->getForms(); + if (count($forms) === 0) { + $this->wp->setTransient(DisplayFormInWPContent::NO_FORM_TRANSIENT_KEY, true); + return $content; + } + $result = $content; - if (!$this->wp->isSingle()) return $result; - if ($this->wp->getTransient(DisplayFormInWPContent::NO_FORM_TRANSIENT_KEY)) return $result; - $forms = $this->formsRepository->findAll(); foreach ($forms as $form) { $result .= $this->getContentBellow($form); } - if (!$this->appendedForm) { - $this->wp->setTransient(DisplayFormInWPContent::NO_FORM_TRANSIENT_KEY, true); - } + return $result; } + private function shouldDisplay():bool { + if (!$this->wp->isSingle()) return false; + if ($this->wp->getTransient(DisplayFormInWPContent::NO_FORM_TRANSIENT_KEY)) return false; + return true; + } + + /** + * @return FormEntity[] + */ + private function getForms():array { + $forms = $this->formsRepository->findAll(); + return array_filter($forms, function($form) { + return $this->shouldDisplayFormBellowContent($form); + }); + } + private function getContentBellow(FormEntity $form): string { - if (!$this->shouldDisplayFormBellowContent($form)) return ''; - $this->appendedForm = true; return Renderer::render([ 'body' => $form->getBody(), 'styles' => $form->getStyles(),