1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\FacebookBundle\Action; |
13
|
|
|
|
14
|
|
|
use Core23\FacebookBundle\Core23FacebookEvents; |
15
|
|
|
use Core23\FacebookBundle\Event\AuthFailedEvent; |
16
|
|
|
use Core23\FacebookBundle\Session\SessionManagerInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
19
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
20
|
|
|
use Symfony\Component\Routing\RouterInterface; |
21
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
22
|
|
|
use Twig\Environment; |
23
|
|
|
use Twig\Error\LoaderError; |
24
|
|
|
use Twig\Error\RuntimeError; |
25
|
|
|
use Twig\Error\SyntaxError; |
26
|
|
|
|
27
|
|
|
final class AuthErrorAction |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var Environment |
31
|
|
|
*/ |
32
|
|
|
private $twig; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var RouterInterface |
36
|
|
|
*/ |
37
|
|
|
private $router; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var SessionManagerInterface |
41
|
|
|
*/ |
42
|
|
|
private $sessionManager; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var EventDispatcherInterface |
46
|
|
|
*/ |
47
|
|
|
private $eventDispatcher; |
48
|
|
|
|
49
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
50
|
|
|
Environment $twig, |
51
|
|
|
RouterInterface $router, |
52
|
|
|
SessionManagerInterface $sessionManager, |
53
|
|
|
EventDispatcherInterface $eventDispatcher |
54
|
|
|
) { |
55
|
|
|
$this->twig = $twig; |
56
|
|
|
$this->router = $router; |
57
|
|
|
$this->sessionManager = $sessionManager; |
58
|
|
|
$this->eventDispatcher = $eventDispatcher; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @throws LoaderError |
63
|
|
|
* @throws RuntimeError |
64
|
|
|
* @throws SyntaxError |
65
|
|
|
*/ |
66
|
|
|
public function __invoke(): Response |
67
|
|
|
{ |
68
|
|
|
if ($this->sessionManager->isAuthenticated()) { |
69
|
|
|
return new RedirectResponse($this->generateUrl('core23_facebook_success')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->sessionManager->clear(); |
73
|
|
|
|
74
|
|
|
$event = new AuthFailedEvent(); |
75
|
|
|
$this->eventDispatcher->dispatch($event, Core23FacebookEvents::AUTH_ERROR); |
76
|
|
|
|
77
|
|
|
if (null !== $response = $event->getResponse()) { |
78
|
|
|
return $response; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return new Response($this->twig->render('@Core23Facebook/Auth/error.html.twig')); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Generates a URL from the given parameters. |
86
|
|
|
* |
87
|
|
|
* @param string $route The name of the route |
88
|
|
|
* @param array $parameters An array of parameters |
89
|
|
|
* @param int $referenceType The type of reference (one of the constants in UrlGeneratorInterface) |
90
|
|
|
* |
91
|
|
|
* @return string The generated URL |
92
|
|
|
*/ |
93
|
|
|
private function generateUrl(string $route, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string |
94
|
|
|
{ |
95
|
|
|
return $this->router->generate($route, $parameters, $referenceType); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.