‪TYPO3CMS  ‪main
ReactionRepository.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;
22 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
29 
36 {
37  public function ‪findAll(): array
38  {
39  return $this->‪map($this->‪getQueryBuilder()
40  ->executeQuery()
41  ->fetchAllAssociative());
42  }
43 
44  public function ‪countAll(): int
45  {
46  return (int)$this->‪getQueryBuilder(false)
47  ->count('*')
48  ->executeQuery()
49  ->fetchOne();
50  }
51 
52  public function ‪getReactionRecords(?‪ReactionDemand $demand = null): array
53  {
54  return $demand !== null ? $this->‪findByDemand($demand) : $this->‪findAll();
55  }
56 
61  {
62  $queryBuilder = $this->‪getQueryBuilder();
63  $queryBuilder
64  ->getRestrictions()
65  ->add(GeneralUtility::makeInstance(HiddenRestriction::class))
66  ->add(GeneralUtility::makeInstance(StartTimeRestriction::class))
67  ->add(GeneralUtility::makeInstance(EndTimeRestriction::class));
68  $result = $queryBuilder
69  ->where(
70  $queryBuilder->expr()->eq('identifier', $queryBuilder->createNamedParameter(‪$identifier))
71  )
72  ->executeQuery()
73  ->fetchAssociative();
74  if (!empty($result)) {
75  return $this->‪mapSingleRow($result);
76  }
77  return null;
78  }
79 
80  public function ‪findByDemand(‪ReactionDemand $demand): array
81  {
82  return $this->‪map($this->‪getQueryBuilderForDemand($demand)
83  ->setMaxResults($demand->‪getLimit())
84  ->setFirstResult($demand->‪getOffset())
85  ->executeQuery()
86  ->fetchAllAssociative());
87  }
88 
89  protected function ‪getQueryBuilderForDemand(‪ReactionDemand $demand): QueryBuilder
90  {
91  $queryBuilder = $this->‪getQueryBuilder(false);
92  $queryBuilder->orderBy(
93  $demand->‪getOrderField(),
94  $demand->‪getOrderDirection()
95  );
96  // Ensure deterministic ordering.
97  if ($demand->‪getOrderField() !== 'uid') {
98  $queryBuilder->addOrderBy('uid', 'asc');
99  }
100 
101  $constraints = [];
102  if ($demand->‪hasName()) {
103  $escapedLikeString = '%' . $queryBuilder->escapeLikeWildcards($demand->‪getName()) . '%';
104  $constraints[] = $queryBuilder->expr()->like(
105  'name',
106  $queryBuilder->createNamedParameter($escapedLikeString)
107  );
108  }
109  if ($demand->‪hasReactionType()) {
110  $constraints[] = $queryBuilder->expr()->eq(
111  'reaction_type',
112  $queryBuilder->createNamedParameter($demand->‪getReactionType())
113  );
114  }
115 
116  if (!empty($constraints)) {
117  $queryBuilder->where(...$constraints);
118  }
119  return $queryBuilder;
120  }
121 
122  protected function ‪map(array $rows): array
123  {
124  $items = [];
125  foreach ($rows as $row) {
126  $items[] = $this->‪mapSingleRow($row);
127  }
128  return $items;
129  }
130 
131  protected function ‪mapSingleRow(array $row): ‪ReactionInstruction
132  {
133  $row = BackendUtility::convertDatabaseRowValuesToPhp('sys_reaction', $row);
134  return new ‪ReactionInstruction($row);
135  }
136 
137  protected function ‪getQueryBuilder(bool $addDefaultOrderByClause = true): QueryBuilder
138  {
139  // @todo ConnectionPool could be injected
140  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
141  ->getQueryBuilderForTable('sys_reaction');
142  $queryBuilder->getRestrictions()
143  ->removeAll()
144  ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
145  $queryBuilder->select('*')->from('sys_reaction');
146  if ($addDefaultOrderByClause) {
147  $queryBuilder
148  ->orderBy('name', 'asc')
149  // Ensure deterministic ordering.
150  ->addOrderBy('uid', 'asc');
151  }
152  return $queryBuilder;
153  }
154 }
‪TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction
Definition: HiddenRestriction.php:27
‪TYPO3\CMS\Reactions\Model\ReactionInstruction
Definition: ReactionInstruction.php:27
‪TYPO3\CMS\Reactions\Repository\ReactionRepository\mapSingleRow
‪mapSingleRow(array $row)
Definition: ReactionRepository.php:131
‪TYPO3\CMS\Core\Database\Query\Restriction\EndTimeRestriction
Definition: EndTimeRestriction.php:27
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\hasName
‪hasName()
Definition: ReactionDemand.php:92
‪TYPO3\CMS\Core\Database\Query\Restriction\StartTimeRestriction
Definition: StartTimeRestriction.php:27
‪TYPO3\CMS\Reactions\Repository\ReactionRepository\getQueryBuilder
‪getQueryBuilder(bool $addDefaultOrderByClause=true)
Definition: ReactionRepository.php:137
‪TYPO3\CMS\Reactions\Repository\ReactionRepository
Definition: ReactionRepository.php:36
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\getOrderField
‪getOrderField()
Definition: ReactionDemand.php:67
‪TYPO3\CMS\Reactions\Repository\ReactionRepository\countAll
‪countAll()
Definition: ReactionRepository.php:44
‪TYPO3\CMS\Reactions\Repository\ReactionDemand
Definition: ReactionDemand.php:28
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\getReactionType
‪getReactionType()
Definition: ReactionDemand.php:97
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\getOrderDirection
‪getOrderDirection()
Definition: ReactionDemand.php:72
‪TYPO3\CMS\Reactions\Repository\ReactionRepository\map
‪map(array $rows)
Definition: ReactionRepository.php:122
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\getOffset
‪getOffset()
Definition: ReactionDemand.php:123
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\getName
‪getName()
Definition: ReactionDemand.php:87
‪TYPO3\CMS\Reactions\Repository\ReactionRepository\findByDemand
‪findByDemand(ReactionDemand $demand)
Definition: ReactionRepository.php:80
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\hasReactionType
‪hasReactionType()
Definition: ReactionDemand.php:102
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\getLimit
‪getLimit()
Definition: ReactionDemand.php:118
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:28
‪TYPO3\CMS\Reactions\Repository\ReactionRepository\getReactionRecordByIdentifier
‪getReactionRecordByIdentifier(string $identifier)
Definition: ReactionRepository.php:60
‪TYPO3\CMS\Reactions\Repository\ReactionRepository\findAll
‪findAll()
Definition: ReactionRepository.php:37
‪TYPO3\CMS\Reactions\Repository\ReactionRepository\getQueryBuilderForDemand
‪getQueryBuilderForDemand(ReactionDemand $demand)
Definition: ReactionRepository.php:89
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Reactions\Repository
Definition: ReactionDemand.php:18
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37
‪TYPO3\CMS\Reactions\Repository\ReactionRepository\getReactionRecords
‪getReactionRecords(?ReactionDemand $demand=null)
Definition: ReactionRepository.php:52