|
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\AuthSuccessEvent; |
|
16
|
|
|
use Core23\FacebookBundle\Session\SessionManager; |
|
17
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
20
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
21
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
22
|
|
|
use Twig\Environment; |
|
23
|
|
|
|
|
24
|
|
|
final class AuthSuccessAction |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var Environment |
|
28
|
|
|
*/ |
|
29
|
|
|
private $twig; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var RouterInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $router; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var SessionManager |
|
38
|
|
|
*/ |
|
39
|
|
|
private $sessionManager; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var EventDispatcherInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
private $eventDispatcher; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* AuthSuccessAction constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param Environment $twig |
|
50
|
|
|
* @param RouterInterface $router |
|
51
|
|
|
* @param SessionManager $sessionManager |
|
52
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct( |
|
55
|
|
|
Environment $twig, |
|
56
|
|
|
RouterInterface $router, |
|
57
|
|
|
SessionManager $sessionManager, |
|
58
|
|
|
EventDispatcherInterface $eventDispatcher |
|
59
|
|
|
) { |
|
60
|
|
|
$this->twig = $twig; |
|
61
|
|
|
$this->router = $router; |
|
62
|
|
|
$this->sessionManager = $sessionManager; |
|
63
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @throws \Twig_Error_Loader |
|
68
|
|
|
* @throws \Twig_Error_Runtime |
|
69
|
|
|
* @throws \Twig_Error_Syntax |
|
70
|
|
|
* |
|
71
|
|
|
* @return Response |
|
72
|
|
|
*/ |
|
73
|
|
|
public function __invoke(): Response |
|
74
|
|
|
{ |
|
75
|
|
|
if (!$this->sessionManager->isAuthenticated()) { |
|
76
|
|
|
return $this->redirectToRoute('core23_facebook_error'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$session = $this->sessionManager->getSession(); |
|
80
|
|
|
|
|
81
|
|
|
if (null === $session) { |
|
82
|
|
|
return $this->redirectToRoute('core23_facebook_error'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$event = new AuthSuccessEvent($session); |
|
86
|
|
|
$this->eventDispatcher->dispatch(Core23FacebookEvents::AUTH_SUCCESS, $event); |
|
87
|
|
|
|
|
88
|
|
|
if ($response = $event->getResponse()) { |
|
89
|
|
|
return $response; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return new Response($this->twig->render('@Core23Facebook/Auth/success.html.twig', [ |
|
93
|
|
|
'name' => $this->sessionManager->getUsername(), |
|
94
|
|
|
])); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Returns a RedirectResponse to the given route with the given parameters. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $route The name of the route |
|
101
|
|
|
* @param array $parameters An array of parameters |
|
102
|
|
|
* @param int $status The status code to use for the Response |
|
103
|
|
|
* |
|
104
|
|
|
* @return RedirectResponse |
|
105
|
|
|
*/ |
|
106
|
|
|
private function redirectToRoute($route, array $parameters = [], $status = 302): RedirectResponse |
|
107
|
|
|
{ |
|
108
|
|
|
return new RedirectResponse($this->generateUrl($route, $parameters), $status); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Generates a URL from the given parameters. |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $route The name of the route |
|
115
|
|
|
* @param array $parameters An array of parameters |
|
116
|
|
|
* @param int $referenceType The type of reference (one of the constants in UrlGeneratorInterface) |
|
117
|
|
|
* |
|
118
|
|
|
* @return string The generated URL |
|
119
|
|
|
*/ |
|
120
|
|
|
private function generateUrl($route, array $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->router->generate($route, $parameters, $referenceType); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|