‪TYPO3CMS  ‪main
Demand.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\ServerRequestInterface;
21 use Symfony\Component\Console\Input\InputInterface;
23 
28 class ‪Demand
29 {
30  protected const ‪ORDER_DESCENDING = 'desc';
31  protected const ‪ORDER_ASCENDING = 'asc';
32  protected const ‪DEFAULT_ORDER_FIELD = 'source_host';
33  protected const ‪DEFAULT_SECONDARY_ORDER_FIELD = 'source_host';
34  protected const ‪ORDER_FIELDS = ['source_host', 'source_path', 'lasthiton', 'hitcount', 'protected'];
35 
36  protected string ‪$orderField;
37  protected string ‪$orderDirection;
38 
42  protected array ‪$sourceHosts;
43  protected string ‪$sourcePath;
44  protected string ‪$target;
45 
49  protected array ‪$statusCodes = [];
50  protected int ‪$limit = 50;
51  protected int ‪$page;
52  protected string ‪$secondaryOrderField;
53  private int ‪$maxHits;
54  private ?\DateTimeInterface ‪$olderThan;
55  protected ?int ‪$creationType = -1;
56  protected ?int ‪$protected = -1;
58 
59  public function ‪__construct(
60  int ‪$page = 1,
61  string ‪$orderField = self::DEFAULT_ORDER_FIELD,
62  string ‪$orderDirection = self::ORDER_ASCENDING,
63  array ‪$sourceHosts = [],
64  string ‪$sourcePath = '',
65  string ‪$target = '',
66  array ‪$statusCodes = [],
67  int ‪$maxHits = 0,
68  \DateTimeInterface ‪$olderThan = null,
69  ?int ‪$creationType = -1,
70  ?int ‪$protected = -1,
72  ) {
73  $this->page = ‪$page;
74  if (!in_array(‪$orderField, self::ORDER_FIELDS, true)) {
76  }
77  $this->orderField = ‪$orderField;
78  if (!in_array(‪$orderDirection, [self::ORDER_DESCENDING, self::ORDER_ASCENDING], true)) {
80  }
81  $this->orderDirection = ‪$orderDirection;
82  $this->sourceHosts = ‪$sourceHosts;
83  $this->sourcePath = ‪$sourcePath;
84  $this->target = ‪$target;
85  $this->statusCodes = ‪$statusCodes;
86  $this->secondaryOrderField = $this->orderField === self::DEFAULT_ORDER_FIELD ? ‪self::DEFAULT_SECONDARY_ORDER_FIELD : '';
87  $this->maxHits = ‪$maxHits;
88  $this->olderThan = ‪$olderThan;
89  $this->creationType = ‪$creationType;
90  $this->protected = ‪$protected;
91  $this->integrityStatus = ‪$integrityStatus;
92  }
93 
94  public static function ‪fromRequest(ServerRequestInterface $request): self
95  {
96  ‪$page = (int)($request->getQueryParams()['page'] ?? $request->getParsedBody()['page'] ?? 1);
97  ‪$orderField = $request->getQueryParams()['orderField'] ?? $request->getParsedBody()['orderField'] ?? ‪self::DEFAULT_ORDER_FIELD;
98  ‪$orderDirection = $request->getQueryParams()['orderDirection'] ?? $request->getParsedBody()['orderDirection'] ?? ‪self::ORDER_ASCENDING;
99  $demand = $request->getQueryParams()['demand'] ?? $request->getParsedBody()['demand'] ?? [];
100  if (empty($demand)) {
101  return new self(‪$page, ‪$orderField, ‪$orderDirection);
102  }
103  $sourceHost = $demand['source_host'] ?? '';
104  ‪$sourceHosts = $sourceHost ? [$sourceHost] : [];
105  ‪$sourcePath = $demand['source_path'] ?? '';
106  ‪$statusCode = (int)($demand['target_statuscode'] ?? 0);
108  ‪$target = $demand['target'] ?? '';
109  ‪$maxHits = (int)($demand['max_hits'] ?? 0);
110  ‪$creationType = isset($demand['creation_type']) ? ((int)$demand['creation_type']) : -1;
111  ‪$protected = isset($demand['protected']) ? ((int)$demand['protected']) : -1;
112  ‪$integrityStatus = isset($demand['integrity_status']) ? ((string)$demand['integrity_status']) : ‪RedirectConflict::NO_CONFLICT;
114  }
115 
116  public static function ‪fromCommandInput(InputInterface $input): self
117  {
118  return new self(
119  1,
122  (array)$input->getOption('domain'),
123  (string)$input->getOption('path'),
124  '',
125  (array)$input->getOption('statusCode'),
126  $input->hasOption('hitCount') ? (int)$input->getOption('hitCount') : 0,
127  $input->getOption('days')
128  ? new \DateTimeImmutable($input->getOption('days') . ' days ago')
129  : new \DateTimeImmutable(
130  '90 days ago'
131  ),
132  $input->hasOption('creationType') ? (int)($input->getOption('creationType')) : null,
133  $input->hasOption('protected') ? (int)($input->getOption('protected')) : null,
134  $input->hasOption('integrityStatus') ? (string)($input->getOption('integrityStatus')) : ‪RedirectConflict::NO_CONFLICT
135  );
136  }
137 
138  public function ‪getMaxHits(): int
139  {
140  return ‪$this->maxHits;
141  }
142 
143  public function ‪hasMaxHits(): bool
144  {
145  return $this->maxHits > 0;
146  }
147 
148  public function ‪getOlderThan(): ?\DateTimeInterface
149  {
150  return ‪$this->olderThan;
151  }
152 
153  public function ‪hasOlderThan(): bool
154  {
155  return $this->olderThan instanceof \DateTimeInterface;
156  }
157 
158  public function ‪getOrderField(): string
159  {
160  return ‪$this->orderField;
161  }
162 
163  public function ‪getOrderDirection(): string
164  {
166  }
167 
168  public function ‪getDefaultOrderDirection(): string
169  {
171  }
172 
173  public function ‪getReverseOrderDirection(): string
174  {
175  return $this->orderDirection === self::ORDER_ASCENDING ? ‪self::ORDER_DESCENDING : ‪self::ORDER_ASCENDING;
176  }
177 
178  public function ‪hasSecondaryOrdering(): bool
179  {
180  return $this->secondaryOrderField !== '';
181  }
182 
183  public function ‪getSecondaryOrderField(): string
184  {
186  }
187 
188  public function ‪getFirstSourceHost(): string
189  {
190  return $this->sourceHosts[0] ?? '';
191  }
192 
193  public function ‪getSourceHosts(): ?array
194  {
195  return $this->sourceHosts === [] ? null : ‪$this->sourceHosts;
196  }
197 
198  public function ‪getSourcePath(): string
199  {
200  return ‪$this->sourcePath;
201  }
202 
203  public function ‪getTarget(): string
204  {
205  return ‪$this->target;
206  }
207 
208  public function ‪getLimit(): int
209  {
210  return ‪$this->limit;
211  }
212 
213  public function ‪getCreationType(): ?int
214  {
215  return ‪$this->creationType;
216  }
217 
218  public function ‪getProtected(): ?int
219  {
220  return ‪$this->protected;
221  }
222 
223  public function ‪getIntegrityStatus(): ?string
224  {
226  }
227 
228  public function ‪getFirstStatusCode(): int
229  {
230  return $this->statusCodes[0] ?? 0;
231  }
232 
233  public function ‪getStatusCodes(): array
234  {
235  return ‪$this->statusCodes;
236  }
237 
238  public function ‪hasStatusCodes(): bool
239  {
240  return !empty($this->statusCodes);
241  }
242 
243  public function ‪hasSourceHosts(): bool
244  {
245  return !empty($this->sourceHosts);
246  }
247 
248  public function ‪hasSourcePath(): bool
249  {
250  return $this->sourcePath !== '';
251  }
252 
253  public function ‪hasTarget(): bool
254  {
255  return $this->target !== '';
256  }
257 
258  public function ‪hasCreationType(): bool
259  {
260  return $this->creationType !== null && $this->creationType !== -1;
261  }
262 
263  public function ‪hasProtected(): bool
264  {
265  return $this->protected !== null && $this->protected !== -1;
266  }
267 
268  public function ‪hasIntegrityStatus(): bool
269  {
270  return $this->integrityStatus !== null && $this->integrityStatus !== '';
271  }
272 
273  public function ‪hasConstraints(): bool
274  {
275  return $this->‪hasSourcePath()
276  || $this->‪hasSourceHosts()
277  || $this->‪hasTarget()
278  || $this->‪hasStatusCodes()
279  || $this->‪hasMaxHits()
280  || $this->‪hasCreationType()
281  || $this->‪hasProtected()
282  || $this->‪hasIntegrityStatus();
283  }
284 
288  public function ‪getPage(): int
289  {
290  return ‪$this->page;
291  }
292 
296  public function ‪getOffset(): int
297  {
298  return ($this->page - 1) * ‪$this->limit;
299  }
300 
301  public function ‪getParameters(): array
302  {
303  $parameters = [];
304  if ($this->‪hasSourcePath()) {
305  $parameters['source_path'] = $this->‪getSourcePath();
306  }
307  if ($this->‪hasSourceHosts()) {
308  $parameters['source_host'] = $this->‪getFirstSourceHost();
309  }
310  if ($this->‪hasTarget()) {
311  $parameters['target'] = $this->‪getTarget();
312  }
313  if ($this->‪hasStatusCodes()) {
314  $parameters['target_statuscode'] = $this->‪getFirstStatusCode();
315  }
316  if ($this->‪hasMaxHits()) {
317  $parameters['max_hits'] = $this->‪getMaxHits();
318  }
319  if ($this->‪hasCreationType()) {
320  $parameters['creation_type'] = $this->‪getCreationType();
321  }
322  if ($this->‪hasProtected()) {
323  $parameters['protected'] = $this->‪getProtected();
324  }
325  if ($this->‪hasIntegrityStatus()) {
326  $parameters['integrity_status'] = $this->‪getIntegrityStatus();
327  }
328  return $parameters;
329  }
330 }
‪TYPO3\CMS\Redirects\Repository\Demand
Definition: Demand.php:29
‪TYPO3\CMS\Redirects\Repository\Demand\$maxHits
‪int $maxHits
Definition: Demand.php:53
‪TYPO3\CMS\Redirects\Repository\Demand\getOffset
‪getOffset()
Definition: Demand.php:296
‪TYPO3\CMS\Redirects\Repository\Demand\ORDER_ASCENDING
‪const ORDER_ASCENDING
Definition: Demand.php:31
‪TYPO3\CMS\Redirects\Repository\Demand\getMaxHits
‪getMaxHits()
Definition: Demand.php:138
‪TYPO3\CMS\Redirects\Repository\Demand\hasStatusCodes
‪hasStatusCodes()
Definition: Demand.php:238
‪TYPO3\CMS\Redirects\Repository\Demand\getOrderField
‪getOrderField()
Definition: Demand.php:158
‪TYPO3\CMS\Redirects\Utility\RedirectConflict
Definition: RedirectConflict.php:24
‪TYPO3\CMS\Redirects\Repository\Demand\fromRequest
‪static fromRequest(ServerRequestInterface $request)
Definition: Demand.php:94
‪TYPO3\CMS\Redirects\Repository\Demand\$statusCodes
‪array $statusCodes
Definition: Demand.php:49
‪TYPO3\CMS\Redirects\Repository\Demand\ORDER_FIELDS
‪const ORDER_FIELDS
Definition: Demand.php:34
‪TYPO3\CMS\Redirects\Repository\Demand\getReverseOrderDirection
‪getReverseOrderDirection()
Definition: Demand.php:173
‪TYPO3\CMS\Redirects\Repository\Demand\getFirstStatusCode
‪getFirstStatusCode()
Definition: Demand.php:228
‪TYPO3\CMS\Redirects\Repository\Demand\getFirstSourceHost
‪getFirstSourceHost()
Definition: Demand.php:188
‪TYPO3\CMS\Redirects\Repository\Demand\getIntegrityStatus
‪getIntegrityStatus()
Definition: Demand.php:223
‪TYPO3\CMS\Redirects\Repository\Demand\$sourceHosts
‪array $sourceHosts
Definition: Demand.php:42
‪TYPO3\CMS\Redirects\Repository\Demand\DEFAULT_SECONDARY_ORDER_FIELD
‪const DEFAULT_SECONDARY_ORDER_FIELD
Definition: Demand.php:33
‪TYPO3\CMS\Redirects\Repository\Demand\getParameters
‪getParameters()
Definition: Demand.php:301
‪TYPO3\CMS\Redirects\Repository\Demand\getProtected
‪getProtected()
Definition: Demand.php:218
‪TYPO3\CMS\Redirects\Repository\Demand\ORDER_DESCENDING
‪const ORDER_DESCENDING
Definition: Demand.php:30
‪TYPO3\CMS\Redirects\Repository\Demand\getCreationType
‪getCreationType()
Definition: Demand.php:213
‪TYPO3\CMS\Redirects\Message\$statusCode
‪identifier readonly UriInterface readonly int $statusCode
Definition: RedirectWasHitMessage.php:34
‪TYPO3\CMS\Redirects\Repository\Demand\$orderField
‪string $orderField
Definition: Demand.php:36
‪TYPO3\CMS\Redirects\Repository\Demand\hasMaxHits
‪hasMaxHits()
Definition: Demand.php:143
‪TYPO3\CMS\Redirects\Repository\Demand\getDefaultOrderDirection
‪getDefaultOrderDirection()
Definition: Demand.php:168
‪TYPO3\CMS\Redirects\Repository\Demand\hasSourcePath
‪hasSourcePath()
Definition: Demand.php:248
‪TYPO3\CMS\Redirects\Repository\Demand\hasSecondaryOrdering
‪hasSecondaryOrdering()
Definition: Demand.php:178
‪TYPO3\CMS\Redirects\Repository\Demand\hasProtected
‪hasProtected()
Definition: Demand.php:263
‪TYPO3\CMS\Redirects\Repository\Demand\hasConstraints
‪hasConstraints()
Definition: Demand.php:273
‪TYPO3\CMS\Redirects\Repository\Demand\$integrityStatus
‪string $integrityStatus
Definition: Demand.php:57
‪TYPO3\CMS\Redirects\Repository\Demand\getOrderDirection
‪getOrderDirection()
Definition: Demand.php:163
‪TYPO3\CMS\Redirects\Repository\Demand\$target
‪string $target
Definition: Demand.php:44
‪TYPO3\CMS\Redirects\Repository\Demand\$limit
‪int $limit
Definition: Demand.php:50
‪TYPO3\CMS\Redirects\Repository\Demand\__construct
‪__construct(int $page=1, string $orderField=self::DEFAULT_ORDER_FIELD, string $orderDirection=self::ORDER_ASCENDING, array $sourceHosts=[], string $sourcePath='', string $target='', array $statusCodes=[], int $maxHits=0, \DateTimeInterface $olderThan=null, ?int $creationType=-1, ?int $protected=-1, ?string $integrityStatus=RedirectConflict::NO_CONFLICT)
Definition: Demand.php:59
‪TYPO3\CMS\Redirects\Repository\Demand\getTarget
‪getTarget()
Definition: Demand.php:203
‪TYPO3\CMS\Redirects\Repository\Demand\getLimit
‪getLimit()
Definition: Demand.php:208
‪TYPO3\CMS\Redirects\Repository\Demand\getSourceHosts
‪getSourceHosts()
Definition: Demand.php:193
‪TYPO3\CMS\Redirects\Repository\Demand\hasOlderThan
‪hasOlderThan()
Definition: Demand.php:153
‪TYPO3\CMS\Redirects\Repository\Demand\$page
‪int $page
Definition: Demand.php:51
‪TYPO3\CMS\Redirects\Repository\Demand\$protected
‪int $protected
Definition: Demand.php:56
‪TYPO3\CMS\Redirects\Repository\Demand\fromCommandInput
‪static fromCommandInput(InputInterface $input)
Definition: Demand.php:116
‪TYPO3\CMS\Redirects\Repository\Demand\$creationType
‪int $creationType
Definition: Demand.php:55
‪TYPO3\CMS\Redirects\Repository
Definition: Demand.php:18
‪TYPO3\CMS\Redirects\Utility\RedirectConflict\NO_CONFLICT
‪const NO_CONFLICT
Definition: RedirectConflict.php:25
‪TYPO3\CMS\Redirects\Repository\Demand\$orderDirection
‪string $orderDirection
Definition: Demand.php:37
‪TYPO3\CMS\Redirects\Repository\Demand\getSecondaryOrderField
‪getSecondaryOrderField()
Definition: Demand.php:183
‪TYPO3\CMS\Redirects\Repository\Demand\getPage
‪getPage()
Definition: Demand.php:288
‪TYPO3\CMS\Redirects\Repository\Demand\getStatusCodes
‪getStatusCodes()
Definition: Demand.php:233
‪TYPO3\CMS\Redirects\Repository\Demand\hasSourceHosts
‪hasSourceHosts()
Definition: Demand.php:243
‪TYPO3\CMS\Redirects\Repository\Demand\getSourcePath
‪getSourcePath()
Definition: Demand.php:198
‪TYPO3\CMS\Redirects\Repository\Demand\hasTarget
‪hasTarget()
Definition: Demand.php:253
‪TYPO3\CMS\Redirects\Repository\Demand\$olderThan
‪DateTimeInterface $olderThan
Definition: Demand.php:54
‪TYPO3\CMS\Redirects\Repository\Demand\hasIntegrityStatus
‪hasIntegrityStatus()
Definition: Demand.php:268
‪TYPO3\CMS\Redirects\Repository\Demand\DEFAULT_ORDER_FIELD
‪const DEFAULT_ORDER_FIELD
Definition: Demand.php:32
‪TYPO3\CMS\Redirects\Repository\Demand\$secondaryOrderField
‪string $secondaryOrderField
Definition: Demand.php:52
‪TYPO3\CMS\Redirects\Repository\Demand\getOlderThan
‪getOlderThan()
Definition: Demand.php:148
‪TYPO3\CMS\Redirects\Repository\Demand\$sourcePath
‪string $sourcePath
Definition: Demand.php:43
‪TYPO3\CMS\Redirects\Repository\Demand\hasCreationType
‪hasCreationType()
Definition: Demand.php:258