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.
Completed
Push — master ( cc1a00...2cee89 )
by Christian
15:25
created

CheckAuthAction::__invoke()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 6
nop 1
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\SessionManager;
17
use Facebook\Exceptions\FacebookSDKException;
18
use Psr\Log\LoggerAwareInterface;
19
use Psr\Log\LoggerAwareTrait;
20
use Symfony\Component\HttpFoundation\RedirectResponse;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
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 FacebookConnection
37
     */
38
    private $facebookConnection;
39
40
    /**
41
     * @var SessionManager
42
     */
43
    private $sessionManager;
44
45
    /**
46
     * CheckAuthAction constructor.
47
     *
48
     * @param RouterInterface    $router
49
     * @param FacebookConnection $facebookConnection
50
     * @param SessionManager     $sessionManager
51
     */
52
    public function __construct(
53
        RouterInterface $router,
54
        FacebookConnection $facebookConnection,
55
        SessionManager $sessionManager
56
    ) {
57
        $this->router             = $router;
58
        $this->facebookConnection = $facebookConnection;
59
        $this->sessionManager     = $sessionManager;
60
    }
61
62
    /**
63
     * @param Request $request
64
     *
65
     * @return Response
66
     */
67
    public function __invoke(Request $request): Response
68
    {
69
        $fb     = $this->facebookConnection;
70
        $helper = $fb->getRedirectLoginHelper();
71
72
        try {
73
            if ($token = $helper->getAccessToken()) {
74
                $response = $fb->get('/me?fields=id,name', $token);
75
76
                $this->sessionManager->store(Session::fromFacebookApi($token, $response->getGraphUser()));
77
78
                return $this->redirectToRoute('core23_facebook_success');
79
            }
80
        } catch (FacebookSDKException $exception) {
81
            $this->logger->warning(sprintf('Facebook SDK Exception: %s', $exception->getMessage()));
82
        }
83
84
        return $this->redirectToRoute('core23_facebook_error');
85
    }
86
87
    /**
88
     * Returns a RedirectResponse to the given route with the given parameters.
89
     *
90
     * @param string $route      The name of the route
91
     * @param array  $parameters An array of parameters
92
     * @param int    $status     The status code to use for the Response
93
     *
94
     * @return RedirectResponse
95
     */
96
    private function redirectToRoute($route, array $parameters = [], $status = 302): RedirectResponse
97
    {
98
        return new RedirectResponse($this->generateUrl($route, $parameters), $status);
99
    }
100
101
    /**
102
     * Generates a URL from the given parameters.
103
     *
104
     * @param string $route         The name of the route
105
     * @param array  $parameters    An array of parameters
106
     * @param int    $referenceType The type of reference (one of the constants in UrlGeneratorInterface)
107
     *
108
     * @return string The generated URL
109
     */
110
    private function generateUrl($route, array $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string
111
    {
112
        return $this->router->generate($route, $parameters, $referenceType);
113
    }
114
}
115