‪TYPO3CMS  ‪main
ReactionHandler.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
22 use Psr\Log\LoggerInterface;
28 
40 {
41  public function ‪__construct(
42  private readonly ‪ReactionRegistry $reactionRegistry,
43  private readonly LoggerInterface $logger,
44  private readonly ‪LanguageServiceFactory $languageServiceFactory
45  ) {}
46 
47  public function ‪handleReaction(ServerRequestInterface $request, ?‪ReactionInstruction $reactionInstruction, ‪ReactionUserAuthentication $user): ResponseInterface
48  {
49  if ($reactionInstruction === null) {
50  $this->logger->warning('No reaction given', [
51  'request' => $request,
52  ]);
53  throw new ‪ReactionNotFoundException('No reaction given', 1669757255);
54  }
55  $reaction = $this->reactionRegistry->getReactionByType($reactionInstruction->‪getType());
56  if ($reaction === null) {
57  throw new ‪ReactionNotFoundException('No reaction found for given reaction type', 1662458842);
58  }
59 
60  // Prepare the user and language object before calling the reaction execution process
61  ‪$GLOBALS['LANG'] = $this->languageServiceFactory->createFromUserPreferences($user);
62  ‪$GLOBALS['BE_USER'] = $user;
63 
64  $payload = $this->‪getPayload($request);
65  $response = $reaction->react($request, $payload, $reactionInstruction);
66  $this->logger->info('Reaction was handled successfully', [
67  'request' => $request,
68  ]);
69  return $this->‪buildReactionResponse($response);
70  }
71 
72  protected function ‪getPayload(ServerRequestInterface $request): array
73  {
74  $body = (string)$request->getBody();
75 
76  try {
77  $payload = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
78  return is_array($payload) ? $payload : [];
79  } catch (\JsonException $e) {
80  // do nothing
81  return [];
82  }
83  }
84 
85  protected function ‪buildReactionResponse(ResponseInterface $response): ResponseInterface
86  {
87  return $response
88  ->withHeader('X-TYPO3-Reaction-Success', $response->getStatusCode() >= 200 && $response->getStatusCode() < 300 ? 'true' : 'false');
89  }
90 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Reactions\Http\ReactionHandler\__construct
‪__construct(private readonly ReactionRegistry $reactionRegistry, private readonly LoggerInterface $logger, private readonly LanguageServiceFactory $languageServiceFactory)
Definition: ReactionHandler.php:41
‪TYPO3\CMS\Reactions\Model\ReactionInstruction
Definition: ReactionInstruction.php:27
‪TYPO3\CMS\Reactions\Http\ReactionHandler\buildReactionResponse
‪buildReactionResponse(ResponseInterface $response)
Definition: ReactionHandler.php:85
‪TYPO3\CMS\Reactions\Http\ReactionHandler
Definition: ReactionHandler.php:40
‪TYPO3\CMS\Reactions\Http\ReactionHandler\getPayload
‪getPayload(ServerRequestInterface $request)
Definition: ReactionHandler.php:72
‪TYPO3\CMS\Reactions\Model\ReactionInstruction\getType
‪getType()
Definition: ReactionInstruction.php:42
‪TYPO3\CMS\Reactions\Http
‪TYPO3\CMS\Reactions\Authentication\ReactionUserAuthentication
Definition: ReactionUserAuthentication.php:31
‪TYPO3\CMS\Reactions\Exception\ReactionNotFoundException
Definition: ReactionNotFoundException.php:20
‪TYPO3\CMS\Reactions\ReactionRegistry
Definition: ReactionRegistry.php:28
‪TYPO3\CMS\Reactions\Http\ReactionHandler\handleReaction
‪handleReaction(ServerRequestInterface $request, ?ReactionInstruction $reactionInstruction, ReactionUserAuthentication $user)
Definition: ReactionHandler.php:47
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25