‪TYPO3CMS  11.5
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;
22 
27 class ‪Demand
28 {
29  protected const ‪ORDER_DESCENDING = 'desc';
30  protected const ‪ORDER_ASCENDING = 'asc';
31  protected const ‪DEFAULT_ORDER_FIELD = 'source_host';
32  protected const ‪DEFAULT_SECONDARY_ORDER_FIELD = 'source_host';
33  protected const ‪ORDER_FIELDS = ['source_host', 'source_path', 'lasthiton', 'hitcount', 'protected'];
34 
38  protected ‪$orderField;
39 
43  protected ‪$orderDirection;
44 
48  protected ‪$sourceHosts;
49 
53  protected ‪$sourcePath;
54 
58  protected ‪$target;
59 
63  protected ‪$statusCodes = [];
64 
68  protected ‪$limit = 50;
69 
73  protected ‪$page;
74 
78  protected ‪$secondaryOrderField;
79 
83  private ‪$maxHits;
84 
88  private ‪$olderThan;
89 
90  public function ‪__construct(
91  int ‪$page = 1,
92  string ‪$orderField = self::DEFAULT_ORDER_FIELD,
93  string ‪$orderDirection = self::ORDER_ASCENDING,
94  array ‪$sourceHosts = [],
95  string ‪$sourcePath = '',
96  string ‪$target = '',
97  array ‪$statusCodes = [],
98  int ‪$maxHits = 0,
99  \DateTimeInterface ‪$olderThan = null
100  ) {
101  $this->page = ‪$page;
102  if (!in_array(‪$orderField, self::ORDER_FIELDS, true)) {
104  }
105  $this->orderField = ‪$orderField;
106  if (!in_array(‪$orderDirection, [self::ORDER_DESCENDING, self::ORDER_ASCENDING], true)) {
108  }
109  $this->orderDirection = ‪$orderDirection;
110  $this->sourceHosts = ‪$sourceHosts;
111  $this->sourcePath = ‪$sourcePath;
112  $this->target = ‪$target;
113  $this->statusCodes = ‪$statusCodes;
114  $this->secondaryOrderField = $this->orderField === self::DEFAULT_ORDER_FIELD ? ‪self::DEFAULT_SECONDARY_ORDER_FIELD : '';
115  $this->maxHits = ‪$maxHits;
116  $this->olderThan = ‪$olderThan;
117  }
118 
119  public static function ‪fromRequest(ServerRequestInterface $request): self
120  {
121  ‪$page = (int)($request->getQueryParams()['page'] ?? $request->getParsedBody()['page'] ?? 1);
122  ‪$orderField = $request->getQueryParams()['orderField'] ?? $request->getParsedBody()['orderField'] ?? ‪self::DEFAULT_ORDER_FIELD;
123  ‪$orderDirection = $request->getQueryParams()['orderDirection'] ?? $request->getParsedBody()['orderDirection'] ?? ‪self::ORDER_ASCENDING;
124  $demand = $request->getQueryParams()['demand'] ?? $request->getParsedBody()['demand'] ?? [];
125  if (empty($demand)) {
126  return new self(‪$page, ‪$orderField, ‪$orderDirection);
127  }
128  $sourceHost = $demand['source_host'] ?? '';
129  ‪$sourceHosts = $sourceHost ? [$sourceHost] : [];
130  ‪$sourcePath = $demand['source_path'] ?? '';
131  $statusCode = (int)($demand['target_statuscode'] ?? 0);
132  ‪$statusCodes = $statusCode > 0 ? [$statusCode] : [];
133  ‪$target = $demand['target'] ?? '';
134  ‪$maxHits = (int)($demand['max_hits'] ?? 0);
136  }
137 
138  public static function ‪fromCommandInput(InputInterface $input): self
139  {
140  return new self(
141  1,
144  (array)$input->getOption('domain'),
145  (string)$input->getOption('path'),
146  '',
147  (array)$input->getOption('statusCode'),
148  $input->hasOption('hitCount') ? (int)$input->getOption('hitCount') : 0,
149  $input->getOption('days')
150  ? new \DateTimeImmutable($input->getOption('days') . ' days ago')
151  : new \DateTimeImmutable('90 days ago')
152  );
153  }
154 
155  public function ‪getMaxHits(): int
156  {
157  return ‪$this->maxHits;
158  }
159 
160  public function ‪hasMaxHits(): bool
161  {
162  return $this->maxHits > 0;
163  }
164 
165  public function ‪getOlderThan(): ?\DateTimeInterface
166  {
167  return ‪$this->olderThan;
168  }
169 
170  public function ‪hasOlderThan(): bool
171  {
172  return $this->olderThan instanceof \DateTimeInterface;
173  }
174 
175  public function ‪getOrderField(): string
176  {
177  return ‪$this->orderField;
178  }
179 
180  public function ‪getOrderDirection(): string
181  {
183  }
184 
185  public function ‪getDefaultOrderDirection(): string
186  {
188  }
189 
190  public function ‪getReverseOrderDirection(): string
191  {
192  return $this->orderDirection === self::ORDER_ASCENDING ? ‪self::ORDER_DESCENDING : ‪self::ORDER_ASCENDING;
193  }
194 
195  public function ‪hasSecondaryOrdering(): bool
196  {
197  return $this->secondaryOrderField !== '';
198  }
199 
200  public function ‪getSecondaryOrderField(): string
201  {
203  }
204 
205  public function ‪getFirstSourceHost(): string
206  {
207  return $this->sourceHosts[0] ?? '';
208  }
209 
210  public function ‪getSourceHosts(): ?array
211  {
212  return $this->sourceHosts === [] ? null : ‪$this->sourceHosts;
213  }
214 
215  public function ‪getSourcePath(): string
216  {
217  return ‪$this->sourcePath;
218  }
219 
220  public function ‪getTarget(): string
221  {
222  return ‪$this->target;
223  }
224 
225  public function ‪getLimit(): int
226  {
227  return ‪$this->limit;
228  }
229 
230  public function ‪getFirstStatusCode(): int
231  {
232  return $this->statusCodes[0] ?? 0;
233  }
234 
235  public function ‪getStatusCodes(): array
236  {
237  return ‪$this->statusCodes;
238  }
239 
240  public function ‪hasStatusCodes(): bool
241  {
242  return !empty($this->statusCodes);
243  }
244 
245  public function ‪hasSourceHosts(): bool
246  {
247  return !empty($this->sourceHosts);
248  }
249 
250  public function ‪hasSourcePath(): bool
251  {
252  return $this->sourcePath !== '';
253  }
254 
255  public function ‪hasTarget(): bool
256  {
257  return $this->target !== '';
258  }
259 
260  public function ‪hasConstraints(): bool
261  {
262  return $this->‪hasSourcePath()
263  || $this->‪hasSourceHosts()
264  || $this->‪hasTarget()
265  || $this->‪hasStatusCodes()
266  || $this->‪hasMaxHits();
267  }
268 
272  public function ‪getPage(): int
273  {
275  }
276 
280  public function ‪getOffset(): int
281  {
282  return ($this->page - 1) * ‪$this->limit;
283  }
284 
285  public function ‪getParameters(): array
286  {
287  $parameters = [];
288  if ($this->‪hasSourcePath()) {
289  $parameters['source_path'] = $this->‪getSourcePath();
290  }
291  if ($this->‪hasSourceHosts()) {
292  $parameters['source_host'] = $this->‪getFirstSourceHost();
293  }
294  if ($this->‪hasTarget()) {
295  $parameters['target'] = $this->‪getTarget();
296  }
297  if ($this->‪hasStatusCodes()) {
298  $parameters['target_statuscode'] = $this->‪getFirstStatusCode();
299  }
300  if ($this->‪hasMaxHits()) {
301  $parameters['max_hits'] = $this->‪getMaxHits();
302  }
303  return $parameters;
304  }
305 }
‪TYPO3\CMS\Redirects\Repository\Demand
Definition: Demand.php:28
‪TYPO3\CMS\Redirects\Repository\Demand\$maxHits
‪int $maxHits
Definition: Demand.php:73
‪TYPO3\CMS\Redirects\Repository\Demand\getOffset
‪getOffset()
Definition: Demand.php:269
‪TYPO3\CMS\Redirects\Repository\Demand\ORDER_ASCENDING
‪const ORDER_ASCENDING
Definition: Demand.php:30
‪TYPO3\CMS\Redirects\Repository\Demand\getMaxHits
‪getMaxHits()
Definition: Demand.php:144
‪TYPO3\CMS\Redirects\Repository\Demand\hasStatusCodes
‪hasStatusCodes()
Definition: Demand.php:229
‪TYPO3\CMS\Redirects\Repository\Demand\getOrderField
‪getOrderField()
Definition: Demand.php:164
‪TYPO3\CMS\Redirects\Repository\Demand\fromRequest
‪static fromRequest(ServerRequestInterface $request)
Definition: Demand.php:108
‪TYPO3\CMS\Redirects\Repository\Demand\ORDER_FIELDS
‪const ORDER_FIELDS
Definition: Demand.php:33
‪TYPO3\CMS\Redirects\Repository\Demand\getReverseOrderDirection
‪getReverseOrderDirection()
Definition: Demand.php:179
‪TYPO3\CMS\Redirects\Repository\Demand\getFirstStatusCode
‪getFirstStatusCode()
Definition: Demand.php:219
‪TYPO3\CMS\Redirects\Repository\Demand\getFirstSourceHost
‪getFirstSourceHost()
Definition: Demand.php:194
‪TYPO3\CMS\Redirects\Repository\Demand\DEFAULT_SECONDARY_ORDER_FIELD
‪const DEFAULT_SECONDARY_ORDER_FIELD
Definition: Demand.php:32
‪TYPO3\CMS\Redirects\Repository\Demand\getParameters
‪getParameters()
Definition: Demand.php:274
‪TYPO3\CMS\Redirects\Repository\Demand\ORDER_DESCENDING
‪const ORDER_DESCENDING
Definition: Demand.php:29
‪TYPO3\CMS\Redirects\Repository\Demand\$olderThan
‪DateTimeInterface null $olderThan
Definition: Demand.php:77
‪TYPO3\CMS\Redirects\Repository\Demand\$orderField
‪string $orderField
Definition: Demand.php:37
‪TYPO3\CMS\Redirects\Repository\Demand\hasMaxHits
‪hasMaxHits()
Definition: Demand.php:149
‪TYPO3\CMS\Redirects\Repository\Demand\getDefaultOrderDirection
‪getDefaultOrderDirection()
Definition: Demand.php:174
‪TYPO3\CMS\Redirects\Repository\Demand\hasSourcePath
‪hasSourcePath()
Definition: Demand.php:239
‪TYPO3\CMS\Redirects\Repository\Demand\hasSecondaryOrdering
‪hasSecondaryOrdering()
Definition: Demand.php:184
‪TYPO3\CMS\Redirects\Repository\Demand\hasConstraints
‪hasConstraints()
Definition: Demand.php:249
‪TYPO3\CMS\Redirects\Repository\Demand\getOrderDirection
‪getOrderDirection()
Definition: Demand.php:169
‪TYPO3\CMS\Redirects\Repository\Demand\$target
‪string $target
Definition: Demand.php:53
‪TYPO3\CMS\Redirects\Repository\Demand\$limit
‪int $limit
Definition: Demand.php:61
‪TYPO3\CMS\Redirects\Repository\Demand\getTarget
‪getTarget()
Definition: Demand.php:209
‪TYPO3\CMS\Redirects\Repository\Demand\getLimit
‪getLimit()
Definition: Demand.php:214
‪TYPO3\CMS\Redirects\Repository\Demand\getSourceHosts
‪getSourceHosts()
Definition: Demand.php:199
‪TYPO3\CMS\Redirects\Repository\Demand\$statusCodes
‪int[] $statusCodes
Definition: Demand.php:57
‪TYPO3\CMS\Redirects\Repository\Demand\hasOlderThan
‪hasOlderThan()
Definition: Demand.php:159
‪TYPO3\CMS\Redirects\Repository\Demand\$page
‪int $page
Definition: Demand.php:65
‪TYPO3\CMS\Redirects\Repository\Demand\fromCommandInput
‪static fromCommandInput(InputInterface $input)
Definition: Demand.php:127
‪TYPO3\CMS\Redirects\Repository
Definition: Demand.php:18
‪TYPO3\CMS\Redirects\Repository\Demand\$sourceHosts
‪string[] $sourceHosts
Definition: Demand.php:45
‪TYPO3\CMS\Redirects\Repository\Demand\$orderDirection
‪string $orderDirection
Definition: Demand.php:41
‪TYPO3\CMS\Redirects\Repository\Demand\getSecondaryOrderField
‪getSecondaryOrderField()
Definition: Demand.php:189
‪TYPO3\CMS\Redirects\Repository\Demand\getPage
‪getPage()
Definition: Demand.php:261
‪TYPO3\CMS\Redirects\Repository\Demand\getStatusCodes
‪getStatusCodes()
Definition: Demand.php:224
‪TYPO3\CMS\Redirects\Repository\Demand\hasSourceHosts
‪hasSourceHosts()
Definition: Demand.php:234
‪TYPO3\CMS\Redirects\Repository\Demand\getSourcePath
‪getSourcePath()
Definition: Demand.php:204
‪TYPO3\CMS\Redirects\Repository\Demand\hasTarget
‪hasTarget()
Definition: Demand.php:244
‪TYPO3\CMS\Redirects\Repository\Demand\DEFAULT_ORDER_FIELD
‪const DEFAULT_ORDER_FIELD
Definition: Demand.php:31
‪TYPO3\CMS\Redirects\Repository\Demand\$secondaryOrderField
‪string $secondaryOrderField
Definition: Demand.php:69
‪TYPO3\CMS\Redirects\Repository\Demand\getOlderThan
‪getOlderThan()
Definition: Demand.php:154
‪TYPO3\CMS\Redirects\Repository\Demand\$sourcePath
‪string $sourcePath
Definition: Demand.php:49
‪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)
Definition: Demand.php:79