‪TYPO3CMS  ‪main
WebhookRepository.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 TYPO3\CMS\Backend\Utility\BackendUtility;
23 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
28 
35 {
36  protected string ‪$cacheIdentifierPrefix = 'webhooks_';
37  protected bool ‪$applyDefaultRestrictions = false;
38 
39  public function ‪__construct(
40  protected readonly ‪ConnectionPool $connectionPool,
41  protected readonly ‪FrontendInterface $runtimeCache,
42  ) {}
43 
47  public function ‪findAll(): array
48  {
49  $cacheIdentifier = $this->cacheIdentifierPrefix . 'all';
50  if (!$this->runtimeCache->has($cacheIdentifier)) {
51  $data = $this->‪map(
52  $this->‪getQueryBuilder()->executeQuery()->fetchAllAssociative()
53  );
54  $this->runtimeCache->set($cacheIdentifier, $data);
55  } else {
56  $data = $this->runtimeCache->get($cacheIdentifier);
57  }
58  return $data;
59  }
60 
61  public function ‪countAll(): int
62  {
63  return (int)$this->‪getQueryBuilder(false)
64  ->count('*')
65  ->executeQuery()
66  ->fetchOne();
67  }
68 
69  public function ‪getWebhookRecords(?‪WebhookDemand $demand = null): array
70  {
71  return $demand !== null ? $this->‪findByDemand($demand) : $this->‪findAll();
72  }
73 
77  public function ‪findByDemand(‪WebhookDemand $demand): array
78  {
79  return $this->‪map($this->‪getQueryBuilderForDemand($demand)
80  ->setMaxResults($demand->‪getLimit())
81  ->setFirstResult($demand->‪getOffset())
82  ->executeQuery()
83  ->fetchAllAssociative());
84  }
85 
87  {
88  $this->applyDefaultRestrictions = ‪$applyDefaultRestrictions;
89  return $this;
90  }
91 
95  protected function ‪getConfiguredWebhooks(): array
96  {
97  ‪$webhooks = [];
98  foreach ($this->‪findAll() as $webhook) {
99  ‪$webhooks[$webhook->getIdentifier()] = $webhook;
100  }
101  return ‪$webhooks;
102  }
103 
107  public function getConfiguredWebhooksByType(string $type): array
108  {
110  return array_filter(‪$webhooks, static fn($webhook) => $webhook->getWebhookType()?->getServiceName() === $type);
111  }
112 
113  protected function ‪getQueryBuilderForDemand(‪WebhookDemand $demand): QueryBuilder
114  {
115  $queryBuilder = $this->‪getQueryBuilder(false);
116  $queryBuilder->orderBy(
117  $demand->‪getOrderField(),
118  $demand->‪getOrderDirection()
119  );
120  // Ensure deterministic ordering.
121  if ($demand->‪getOrderField() !== 'uid') {
122  $queryBuilder->addOrderBy('uid', 'asc');
123  }
124 
125  $constraints = [];
126  if ($demand->‪hasName()) {
127  $escapedLikeString = '%' . $queryBuilder->escapeLikeWildcards($demand->‪getName()) . '%';
128  $constraints[] = $queryBuilder->expr()->or(
129  $queryBuilder->expr()->like(
130  'name',
131  $queryBuilder->createNamedParameter($escapedLikeString)
132  ),
133  $queryBuilder->expr()->like(
134  'description',
135  $queryBuilder->createNamedParameter($escapedLikeString)
136  )
137  );
138  }
139  if ($demand->‪hasWebhookType()) {
140  $constraints[] = $queryBuilder->expr()->eq(
141  'webhook_type',
142  $queryBuilder->createNamedParameter($demand->‪getWebhookType())
143  );
144  }
145 
146  if (!empty($constraints)) {
147  $queryBuilder->where(...$constraints);
148  }
149  return $queryBuilder;
150  }
151 
152  protected function ‪map(array $rows): array
153  {
154  $items = [];
155  foreach ($rows as $row) {
156  $items[] = $this->‪mapSingleRow($row);
157  }
158  return $items;
159  }
160 
161  protected function ‪mapSingleRow(array $row): ‪WebhookInstruction
162  {
163  $row = BackendUtility::convertDatabaseRowValuesToPhp('sys_webhook', $row);
165  }
166 
167  protected function ‪getQueryBuilder(bool $addDefaultOrderByClause = true): QueryBuilder
168  {
169  $queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_webhook');
170  if (!$this->applyDefaultRestrictions) {
171  $queryBuilder->getRestrictions()
172  ->removeAll()
173  ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
174  }
175  $queryBuilder->select('*')->from('sys_webhook');
176  if ($addDefaultOrderByClause) {
177  $queryBuilder
178  ->orderBy('name', 'asc')
179  // Ensure deterministic ordering.
180  ->addOrderBy('uid', 'asc');
181  }
182  return $queryBuilder;
183  }
184 }
‪TYPO3\CMS\Webhooks\Repository\WebhookRepository\$webhooks
‪return $webhooks
Definition: WebhookRepository.php:101
‪TYPO3\CMS\Webhooks\Repository\WebhookDemand\getOffset
‪getOffset()
Definition: WebhookDemand.php:122
‪TYPO3\CMS\Webhooks\Repository\WebhookDemand\hasWebhookType
‪hasWebhookType()
Definition: WebhookDemand.php:102
‪TYPO3\CMS\Webhooks\Repository\WebhookRepository\getConfiguredWebhooks
‪array< string, getConfiguredWebhooksByType(string $type):array { $webhooks=$this-> getConfiguredWebhooks()
‪TYPO3\CMS\Webhooks\Repository\WebhookRepository\getQueryBuilder
‪getQueryBuilder(bool $addDefaultOrderByClause=true)
Definition: WebhookRepository.php:167
‪TYPO3\CMS\Webhooks\Repository\WebhookDemand\getWebhookType
‪getWebhookType()
Definition: WebhookDemand.php:97
‪TYPO3\CMS\Webhooks\Repository\WebhookDemand
Definition: WebhookDemand.php:28
‪TYPO3\CMS\Webhooks\Repository\WebhookRepository\__construct
‪__construct(protected readonly ConnectionPool $connectionPool, protected readonly FrontendInterface $runtimeCache,)
Definition: WebhookRepository.php:39
‪TYPO3\CMS\Webhooks\Repository\WebhookDemand\getName
‪getName()
Definition: WebhookDemand.php:87
‪TYPO3\CMS\Webhooks\Repository
Definition: WebhookDemand.php:18
‪TYPO3\CMS\Webhooks\Repository\WebhookDemand\getOrderField
‪getOrderField()
Definition: WebhookDemand.php:67
‪TYPO3\CMS\Webhooks\Repository\WebhookRepository\getQueryBuilderForDemand
‪getQueryBuilderForDemand(WebhookDemand $demand)
Definition: WebhookRepository.php:113
‪TYPO3\CMS\Webhooks\Repository\WebhookDemand\getOrderDirection
‪getOrderDirection()
Definition: WebhookDemand.php:72
‪TYPO3\CMS\Webhooks\Repository\WebhookRepository\$cacheIdentifierPrefix
‪string $cacheIdentifierPrefix
Definition: WebhookRepository.php:36
‪TYPO3\CMS\Webhooks\Factory\WebhookInstructionFactory\createFromRow
‪static createFromRow(array $row)
Definition: WebhookInstructionFactory.php:69
‪TYPO3\CMS\Webhooks\Repository\WebhookRepository\setApplyDefaultRestrictions
‪setApplyDefaultRestrictions(bool $applyDefaultRestrictions)
Definition: WebhookRepository.php:86
‪TYPO3\CMS\Webhooks\Repository\WebhookDemand\hasName
‪hasName()
Definition: WebhookDemand.php:92
‪TYPO3\CMS\Webhooks\Repository\WebhookDemand\getLimit
‪getLimit()
Definition: WebhookDemand.php:117
‪TYPO3\CMS\Webhooks\Repository\WebhookRepository\findAll
‪WebhookInstruction[] findAll()
Definition: WebhookRepository.php:47
‪TYPO3\CMS\Webhooks\Repository\WebhookRepository\countAll
‪countAll()
Definition: WebhookRepository.php:61
‪TYPO3\CMS\Webhooks\Repository\WebhookRepository\map
‪map(array $rows)
Definition: WebhookRepository.php:152
‪TYPO3\CMS\Webhooks\Model\WebhookInstruction
Definition: WebhookInstruction.php:28
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Webhooks\Repository\WebhookRepository\findByDemand
‪WebhookInstruction[] findByDemand(WebhookDemand $demand)
Definition: WebhookRepository.php:77
‪TYPO3\CMS\Webhooks\Repository\WebhookRepository\$applyDefaultRestrictions
‪bool $applyDefaultRestrictions
Definition: WebhookRepository.php:37
‪TYPO3\CMS\Webhooks\Repository\WebhookRepository\mapSingleRow
‪mapSingleRow(array $row)
Definition: WebhookRepository.php:161
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:28
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Webhooks\Repository\WebhookRepository\getWebhookRecords
‪getWebhookRecords(?WebhookDemand $demand=null)
Definition: WebhookRepository.php:69
‪TYPO3\CMS\Webhooks\Repository\WebhookRepository
Definition: WebhookRepository.php:35
‪TYPO3\CMS\Webhooks\Factory\WebhookInstructionFactory
Definition: WebhookInstructionFactory.php:31