GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

CheckAuthAction::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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