From 33f385f7830a695dd8a07402724add597bef03fe Mon Sep 17 00:00:00 2001 From: Sylvain Tissot Date: Mon, 8 Aug 2022 16:04:59 +0200 Subject: [PATCH] Refactor to use Symfony dependency injection + use Cookiejar #32 --- .../Controller/BackendMessageController.php | 21 +++++++++---------- Classes/Controller/MessageSentController.php | 1 - Classes/PagePath/PagePath.php | 11 ++++------ Classes/TypeConverter/BodyConverter.php | 17 +++++++-------- Configuration/Services.yaml | 5 +++++ 5 files changed, 27 insertions(+), 28 deletions(-) create mode 100644 Configuration/Services.yaml diff --git a/Classes/Controller/BackendMessageController.php b/Classes/Controller/BackendMessageController.php index c6ec816..a339587 100644 --- a/Classes/Controller/BackendMessageController.php +++ b/Classes/Controller/BackendMessageController.php @@ -27,25 +27,24 @@ */ class BackendMessageController extends ActionController { + protected BodyConverter $typeConverter; + + public function __construct(BodyConverter $bodyConverter) { + $this->typeConverter = $bodyConverter; + } public function initializeAction(): void { // Configure property mapping to retrieve the file object. if ($this->arguments->hasArgument('body')) { - - /** @var BodyConverter $typeConverter */ - $typeConverter = GeneralUtility::makeInstance(BodyConverter::class); - $propertyMappingConfiguration = $this->arguments->getArgument('body')->getPropertyMappingConfiguration(); - $propertyMappingConfiguration->setTypeConverter($typeConverter); + $propertyMappingConfiguration->setTypeConverter($this->typeConverter); } } - /** - * @param int $pageId - */ - public function composeAction(array $matches = [], $pageId = 0): void + public function composeAction(array $matches = []): void { + $pageId = $this->request->hasArgument('pageId') ? $this->request->getArgument('pageId') : 0; $recipientDataType = ConfigurationUtility::getInstance()->get('recipient_data_type'); // Instantiate the Matcher object according different rules. @@ -72,11 +71,11 @@ public function composeAction(array $matches = [], $pageId = 0): void } /** - * @param bool $parseMarkdown * @Validate("Fab\Messenger\Domain\Validator\NotEmptyValidator", param="subject") */ - public function enqueueAction(string $subject, string $body, string $sender, array $matches = [], $parseMarkdown = false): void + public function enqueueAction(string $subject, string $body, string $sender, array $matches = []): void { + $parseMarkdown = $this->request->hasArgument('parseMarkdown') ? (bool) $this->request->getArgument('parseMarkdown') : false; $recipientDataType = ConfigurationUtility::getInstance()->get('recipient_data_type'); // Instantiate the Matcher object according different rules. diff --git a/Classes/Controller/MessageSentController.php b/Classes/Controller/MessageSentController.php index 9cb0693..f459a2b 100644 --- a/Classes/Controller/MessageSentController.php +++ b/Classes/Controller/MessageSentController.php @@ -66,7 +66,6 @@ public function sendAgainAction(array $matches = []): string /** @var Message $message */ $message = GeneralUtility::makeInstance(Message::class); - $isSent = $message->setBody($contentObject['body']) ->setSubject($contentObject['subject']) ->setSender($this->normalizeEmails($contentObject['sender'])) diff --git a/Classes/PagePath/PagePath.php b/Classes/PagePath/PagePath.php index bba73e9..ce5d9ab 100644 --- a/Classes/PagePath/PagePath.php +++ b/Classes/PagePath/PagePath.php @@ -51,13 +51,10 @@ public static function getUrl($pageId, $parameters): string $requestFactory = GeneralUtility::makeInstance(RequestFactory::class); // Send TYPO3 cookies as this may affect path generation - $additionalOptions = [ - 'headers' => [ - 'Cookie' => 'fe_typo_user=' . $_COOKIE['fe_typo_user'], - ], - 'cookies' => true, - ]; - $response = $requestFactory->request($url, 'GET', $additionalOptions); + $jar = \GuzzleHttp\Cookie\CookieJar::fromArray([ + 'fe_typo_user' => $_COOKIE['fe_typo_user'], + ], $_SERVER['HTTP_HOST']); + $response = $requestFactory->request($url, 'GET', ['cookies' => $jar]); $result = $response->getBody()->getContents(); $urlParts = parse_url($result); diff --git a/Classes/TypeConverter/BodyConverter.php b/Classes/TypeConverter/BodyConverter.php index d7cae74..4978e2b 100644 --- a/Classes/TypeConverter/BodyConverter.php +++ b/Classes/TypeConverter/BodyConverter.php @@ -37,11 +37,13 @@ class BodyConverter extends AbstractTypeConverter protected $priority = 1; /** - * @var RequestFactoryInterface + * @var RequestFactory */ protected $requestFactory; - public function __construct(RequestFactoryInterface $requestFactory) { + public function __construct( + RequestFactoryInterface $requestFactory + ) { $this->requestFactory = $requestFactory; } @@ -62,14 +64,11 @@ public function convertFrom($source, $targetType, array $convertedChildPropertie $baseUrl = PagePath::getSiteBaseUrl($source); // Send TYPO3 cookies as this may affect path generation - $additionalOptions = [ - 'headers' => [ - 'Cookie' => 'fe_typo_user=' . $_COOKIE['fe_typo_user'], - ], - 'cookies' => true, - ]; + $jar = \GuzzleHttp\Cookie\CookieJar::fromArray([ + 'fe_typo_user' => $_COOKIE['fe_typo_user'], + ], $_SERVER['HTTP_HOST']); $url = $baseUrl . 'index.php?id=' . $source; - $response = $this->requestFactory->request($url, 'GET', $additionalOptions); + $response = $this->requestFactory->request($url, 'GET', ['cookies' => $jar]); if ($response->getStatusCode() === 200) { $content = $response->getBody()->getContents(); $body = preg_match("/]*>(.*?)<\/body>/is", $content, $matches); diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml new file mode 100644 index 0000000..c579836 --- /dev/null +++ b/Configuration/Services.yaml @@ -0,0 +1,5 @@ +services: + _defaults: + autowire: true + autoconfigure: true + public: true