‪TYPO3CMS  ‪main
_ExampleReactionType.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
7 use Psr\Http\Message\ResponseFactoryInterface;
8 use Psr\Http\Message\ResponseInterface;
9 use Psr\Http\Message\ServerRequestInterface;
10 use Psr\Http\Message\StreamFactoryInterface;
14 
16 {
17  private const ‪REGISTRY_KEY = 'changed_ids';
18 
19  public function ‪__construct(
20  private readonly ‪Registry $registry,
21  private readonly ResponseFactoryInterface $responseFactory,
22  private readonly StreamFactoryInterface $streamFactory,
23  ) {}
24 
25  public static function ‪getType(): string
26  {
27  return 'example-reaction-type';
28  }
29 
30  public static function ‪getDescription(): string
31  {
32  return 'Example reaction type';
33  }
34 
35  public static function ‪getIconIdentifier(): string
36  {
37  return 'tx_examples-dummy';
38  }
39 
40  public function ‪react(
41  ServerRequestInterface $request,
42  array $payload,
43  ‪ReactionInstruction $reaction
44  ): ResponseInterface {
45  $id = $payload['id'] ?? 0;
46  if ($id <= 0) {
47  $data = [
48  'success' => false,
49  'error' => 'id not given',
50  ];
51 
52  return $this->‪jsonResponse($data, 400);
53  }
54 
55  $this->‪updateRegistryEntry($id);
56 
57  return $this->‪jsonResponse(['success' => true]);
58  }
59 
60  private function ‪updateRegistryEntry(int $id): void
61  {
62  $ids = $this->registry->get('tx_examples', self::REGISTRY_KEY) ?? [];
63  $ids[] = $id;
64  $ids = array_unique($ids);
65  $this->registry->set('tx_examples', self::REGISTRY_KEY, $ids);
66  }
67 
68  private function ‪jsonResponse(array $data, int ‪$statusCode = 201): ResponseInterface
69  {
70  return $this->responseFactory
71  ->createResponse(‪$statusCode)
72  ->withHeader('Content-Type', 'application/json')
73  ->withBody($this->streamFactory->createStream(json_encode($data, JSON_THROW_ON_ERROR)));
74  }
75 }
‪TYPO3\CMS\Reactions\Model\ReactionInstruction
Definition: ReactionInstruction.php:27
‪TYPO3\CMS\Core\Registry
Definition: Registry.php:33
‪T3docs\Examples\Reaction\ExampleReactionType\updateRegistryEntry
‪updateRegistryEntry(int $id)
Definition: _ExampleReactionType.php:60
‪T3docs\Examples\Reaction\ExampleReactionType\__construct
‪__construct(private readonly Registry $registry, private readonly ResponseFactoryInterface $responseFactory, private readonly StreamFactoryInterface $streamFactory,)
Definition: _ExampleReactionType.php:19
‪T3docs\Examples\Reaction
Definition: _ExampleReactionType.php:5
‪T3docs\Examples\Reaction\ExampleReactionType\getIconIdentifier
‪static getIconIdentifier()
Definition: _ExampleReactionType.php:35
‪T3docs\Examples\Reaction\ExampleReactionType\jsonResponse
‪jsonResponse(array $data, int $statusCode=201)
Definition: _ExampleReactionType.php:68
‪TYPO3\CMS\Redirects\Message\$statusCode
‪identifier readonly UriInterface readonly int $statusCode
Definition: RedirectWasHitMessage.php:34
‪TYPO3\CMS\Reactions\Reaction\ReactionInterface
Definition: ReactionInterface.php:25
‪T3docs\Examples\Reaction\ExampleReactionType\getType
‪static getType()
Definition: _ExampleReactionType.php:25
‪T3docs\Examples\Reaction\ExampleReactionType\getDescription
‪static getDescription()
Definition: _ExampleReactionType.php:30
‪T3docs\Examples\Reaction\ExampleReactionType\react
‪react(ServerRequestInterface $request, array $payload, ReactionInstruction $reaction)
Definition: _ExampleReactionType.php:40
‪T3docs\Examples\Reaction\ExampleReactionType
Definition: _ExampleReactionType.php:16
‪T3docs\Examples\Reaction\ExampleReactionType\REGISTRY_KEY
‪const REGISTRY_KEY
Definition: _ExampleReactionType.php:17