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 ( c0b5bb...9f26b8 )
by Christian
15:19
created

AuthController::successAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 0
1
<?php
2
3
/*
4
 * (c) Christian Gripp <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Core23\FacebookBundle\Controller;
11
12
use Core23\FacebookBundle\Connection\FacebookConnection;
13
use Facebook\Authentication\AccessToken;
14
use Facebook\Exceptions\FacebookSDKException;
15
use Facebook\GraphNodes\GraphUser;
16
use Psr\Log\LoggerInterface;
17
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpFoundation\Session\SessionInterface;
20
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
21
22
final class AuthController extends Controller
23
{
24
    public const SESSION_FB_ID      = '_CORE23_FACEBOOK_ID';
25
    public const SESSION_FB_NAME    = '_CORE23_FACEBOOK_NAME';
26
    public const SESSION_FB_TOKEN   = '_CORE23_FACEBOOK_TOKEN';
27
    public const SESSION_FB_EXPIRES = '_CORE23_FACEBOOK_EXPIRES';
28
29
    /**
30
     * @return Response
31
     */
32
    public function authAction(): Response
33
    {
34
        $fb     = $this->getFacebookConnection();
35
        $helper = $fb->getRedirectLoginHelper();
36
37
        return $this->redirect($helper->getLoginUrl(
38
            $this->generateUrl('core23_facebook_check', array(), UrlGeneratorInterface::ABSOLUTE_URL),
39
            $this->getParameter('core23.facebook.api.permissions')
40
        ));
41
    }
42
43
    /**
44
     * @return Response
45
     */
46
    public function checkAction(): Response
47
    {
48
        $fb     = $this->getFacebookConnection();
49
        $helper = $fb->getRedirectLoginHelper();
50
51
        try {
52
            if ($token = $helper->getAccessToken()) {
53
                $response = $fb->get('/me?fields=id,name', $token);
54
55
                $this->storeCredentials($token, $response->getGraphUser());
56
57
                return $this->redirectToRoute('core23_facebook_success');
58
            }
59
        } catch (FacebookSDKException $exception) {
60
            $this->getLogger()->warning(sprintf('Facebook SDK Exception: %s', $exception->getMessage()));
61
        }
62
63
        return $this->redirectToRoute('core23_facebook_error');
64
    }
65
66
    /**
67
     * @return Response
68
     */
69
    public function errorAction(): Response
70
    {
71
        if ($this->isAuthenticated()) {
72
            return $this->redirectToRoute('core23_facebook_success');
73
        }
74
75
        if (null !== $this->getParameter('core23.facebook.auth_error.redirect_route')) {
76
            return $this->redirectToRoute($this->getParameter('core23.facebook.auth_error.redirect_route'), $this->getParameter('core23.facebook.auth_error.redirect_route_params'));
77
        }
78
79
        return $this->render('Core23FacebookBundle:Auth:error.html.twig');
80
    }
81
82
    /**
83
     * @return Response
84
     */
85
    public function successAction(): Response
86
    {
87
        if (!$this->isAuthenticated()) {
88
            return $this->redirectToRoute('core23_facebook_error');
89
        }
90
91
        if (null !== $this->getParameter('core23.facebook.auth_success.redirect_route')) {
92
            return $this->redirectToRoute($this->getParameter('core23.facebook.auth_success.redirect_route'), $this->getParameter('core23.facebook.auth_success.redirect_route_params'));
93
        }
94
95
        $session = $this->getSession();
96
97
        return $this->render('Core23FacebookBundle:Auth:success.html.twig', array(
98
            'name' => $session->get(static::SESSION_FB_NAME),
99
        ));
100
    }
101
102
    /**
103
     * @param AccessToken $token
104
     * @param GraphUser   $user
105
     */
106
    private function storeCredentials(AccessToken $token, GraphUser $user): void
107
    {
108
        $fbid = $user->getId();
109
        $name = $user->getName();
110
111
        $session = $this->getSession();
112
        $session->set(static::SESSION_FB_ID, $fbid);
113
        $session->set(static::SESSION_FB_NAME, $name);
114
        $session->set(static::SESSION_FB_TOKEN, $token);
115
        $session->set(static::SESSION_FB_EXPIRES, $token->getExpiresAt());
116
    }
117
118
    /**
119
     * Returns the auth status.
120
     *
121
     * @return bool
122
     */
123
    private function isAuthenticated(): bool
124
    {
125
        return (bool) $this->getSession()->get(static::SESSION_FB_TOKEN);
126
    }
127
128
    /**
129
     * @return FacebookConnection
130
     */
131
    private function getFacebookConnection(): FacebookConnection
132
    {
133
        return $this->get('core23.facebook.connection');
134
    }
135
136
    /**
137
     * @return LoggerInterface
138
     */
139
    private function getLogger() : LoggerInterface
140
    {
141
        return $this->get('logger');
142
    }
143
144
    /**
145
     * @return SessionInterface
146
     */
147
    private function getSession() : SessionInterface
148
    {
149
        return $this->get('session');
150
    }
151
}
152