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\Connection\FacebookConnection; |
15
|
|
|
use Core23\FacebookBundle\Session\Session; |
16
|
|
|
use Core23\FacebookBundle\Session\SessionInterface; |
17
|
|
|
use Core23\FacebookBundle\Session\SessionManager; |
18
|
|
|
use Core23\FacebookBundle\Session\SessionManagerInterface; |
19
|
|
|
use Facebook\Exceptions\FacebookSDKException; |
20
|
|
|
use Facebook\Facebook; |
21
|
|
|
use Psr\Log\LoggerAwareInterface; |
22
|
|
|
use Psr\Log\LoggerAwareTrait; |
23
|
|
|
use Psr\Log\NullLogger; |
24
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
25
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
26
|
|
|
use Symfony\Component\Routing\RouterInterface; |
27
|
|
|
|
28
|
|
|
final class CheckAuthAction implements LoggerAwareInterface |
29
|
|
|
{ |
30
|
|
|
use LoggerAwareTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var RouterInterface |
34
|
|
|
*/ |
35
|
|
|
private $router; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Facebook |
39
|
|
|
*/ |
40
|
|
|
private $facebookConnection; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var SessionManagerInterface |
44
|
|
|
*/ |
45
|
|
|
private $sessionManager; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param RouterInterface $router |
49
|
|
|
* @param Facebook $facebookConnection |
50
|
|
|
* @param SessionManagerInterface $sessionManager |
51
|
|
|
*/ |
52
|
|
|
public function __construct( |
53
|
|
|
RouterInterface $router, |
54
|
|
|
Facebook $facebookConnection, |
55
|
|
|
SessionManagerInterface $sessionManager |
56
|
|
|
) { |
57
|
|
|
$this->router = $router; |
58
|
|
|
$this->facebookConnection = $facebookConnection; |
59
|
|
|
$this->sessionManager = $sessionManager; |
60
|
|
|
$this->logger = new NullLogger(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return RedirectResponse |
65
|
|
|
*/ |
66
|
|
|
public function __invoke(): RedirectResponse |
67
|
|
|
{ |
68
|
|
|
$session = $this->getSession(); |
69
|
|
|
|
70
|
|
|
if (null !== $session) { |
71
|
|
|
$this->sessionManager->store($session); |
72
|
|
|
|
73
|
|
|
return new RedirectResponse($this->generateUrl('core23_facebook_success')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return new RedirectResponse($this->generateUrl('core23_facebook_error')); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Generates a URL from the given parameters. |
81
|
|
|
* |
82
|
|
|
* @param string $route The name of the route |
83
|
|
|
* @param array $parameters An array of parameters |
84
|
|
|
* @param int $referenceType The type of reference (one of the constants in UrlGeneratorInterface) |
85
|
|
|
* |
86
|
|
|
* @return string The generated URL |
87
|
|
|
*/ |
88
|
|
|
private function generateUrl(string $route, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string |
89
|
|
|
{ |
90
|
|
|
return $this->router->generate($route, $parameters, $referenceType); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return SessionInterface|null |
95
|
|
|
*/ |
96
|
|
|
private function getSession(): ?SessionInterface |
97
|
|
|
{ |
98
|
|
|
$fb = $this->facebookConnection; |
99
|
|
|
$helper = $fb->getRedirectLoginHelper(); |
100
|
|
|
$token = $helper->getAccessToken(); |
101
|
|
|
|
102
|
|
|
if (null === $token) { |
103
|
|
|
return null; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
try { |
107
|
|
|
$response = $fb->get('/me?fields=id,name', $token); |
108
|
|
|
|
109
|
|
|
return Session::fromFacebookApi($token, $response->getGraphUser()); |
110
|
|
|
} catch (FacebookSDKException $exception) { |
111
|
|
|
$this->logger->warning(sprintf('Facebook SDK Exception: %s', $exception->getMessage())); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return null; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|