1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher Content Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2020 Sourcefabric z.ú. and contributors. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please see the |
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @copyright 2020 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\ContentBundle\EventListener; |
18
|
|
|
|
19
|
|
|
use SWP\Component\Storage\Repository\RepositoryInterface; |
20
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
21
|
|
|
use Symfony\Component\HttpFoundation\Response; |
22
|
|
|
use Symfony\Component\HttpKernel\Event\ExceptionEvent; |
23
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
24
|
|
|
use Symfony\Component\Routing\RouterInterface; |
25
|
|
|
|
26
|
|
|
final class ArticlePreviousRelativeUrlListener |
27
|
|
|
{ |
28
|
|
|
private $router; |
29
|
|
|
|
30
|
|
|
private $articlePreviousUrlRepository; |
31
|
|
|
|
32
|
|
|
public function __construct(RouterInterface $router, RepositoryInterface $articlePreviousUrlRepository) |
33
|
|
|
{ |
34
|
|
|
$this->router = $router; |
35
|
|
|
$this->articlePreviousUrlRepository = $articlePreviousUrlRepository; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function onKernelException(ExceptionEvent $event): void |
39
|
|
|
{ |
40
|
|
|
$exception = $event->getException(); |
|
|
|
|
41
|
|
|
$request = $event->getRequest(); |
42
|
|
|
|
43
|
|
|
if (!$exception instanceof NotFoundHttpException) { |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$articleRelativeUrl = $this->articlePreviousUrlRepository->findOneBy(['relativeUrl' => $request->getRequestUri()]); |
48
|
|
|
|
49
|
|
|
if (null !== $articleRelativeUrl) { |
50
|
|
|
$article = $articleRelativeUrl->getArticle(); |
51
|
|
|
|
52
|
|
|
$event->setResponse(new RedirectResponse( |
53
|
|
|
$this->router->generate( |
|
|
|
|
54
|
|
|
$article->getRoute()->getName(), |
55
|
|
|
[ |
56
|
|
|
'slug' => $article->getSlug(), |
57
|
|
|
] |
58
|
|
|
), |
59
|
|
|
Response::HTTP_MOVED_PERMANENTLY |
60
|
|
|
)); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.