AdminController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 8
dl 0
loc 125
ccs 0
cts 57
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A indexAction() 0 13 2
A authenticateAction() 0 13 1
A revokeAction() 0 12 1
A removeAction() 0 8 1
A returnAction() 0 23 3
1
<?php
2
3
namespace Happyr\GoogleSiteAuthenticatorBundle\Controller;
4
5
use Happyr\GoogleSiteAuthenticatorBundle\Model\TokenConfig;
6
use Happyr\GoogleSiteAuthenticatorBundle\Service\ClientProvider;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
8
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
13
/**
14
 * A controller made for administrators to authenticate and revoke access to google.
15
 */
16
class AdminController extends Controller
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
17
{
18
    const SESSION_KEY = 'google_token_name';
19
20
    /**
21
     * @var ClientProvider
22
     */
23
    private $clientProvider;
24
25
    /**
26
     * @var TokenConfig
27
     */
28
    private $tokenConfig;
29
30
    public function __construct(ClientProvider $clientProvider, TokenConfig $tokenConfig)
31
    {
32
        $this->clientProvider = $clientProvider;
33
        $this->tokenConfig = $tokenConfig;
34
    }
35
36
    public function indexAction()
37
    {
38
        $tokenNames = $this->tokenConfig->getAllKeys();
39
40
        $tokens = [];
41
        foreach ($tokenNames as $tokenName) {
42
            $tokens[$tokenName] = $this->clientProvider->isTokenValid($tokenName);
43
        }
44
45
        return $this->render('@HappyrGoogleSiteAuthenticator/admin/index.html.twig', [
46
            'tokens' => $tokens,
47
        ]);
48
    }
49
50
    /**
51
     * This action starts the authentication.
52
     *
53
     * @param Request $request
54
     * @param $name
55
     *
56
     * @return Response
57
     */
58
    public function authenticateAction(Request $request, $name)
59
    {
60
        /* @var \Google_Client $client */
61
        $client = $this->clientProvider->getClient($name);
62
63
        // This will allow us to get refresh the token
64
        $client->setAccessType('offline');
65
        $client->setApprovalPrompt('force');
66
67
        $request->getSession()->set(self::SESSION_KEY, $name);
68
69
        return $this->redirect($client->createAuthUrl());
70
    }
71
72
    /**
73
     * This action revokes the authentication token. This make sure the token can not be used on any other site.
74
     *
75
     * @param Request $request
0 ignored issues
show
Bug introduced by
There is no parameter named $request. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
76
     * @param $name
77
     *
78
     * @return Response
79
     */
80
    public function revokeAction($name)
81
    {
82
        /* @var \Google_Client $client */
83
        $client = $this->clientProvider->getClient($name);
84
85
        $client->revokeToken();
86
        $this->clientProvider->setAccessToken(null, $name);
87
88
        $this->get('session')->getFlashbag()->add('msg', 'Token was revoked.');
89
90
        return $this->redirect($this->generateUrl('happyr.google_site_authenticator.index'));
91
    }
92
93
    /**
94
     * This action removes the authentication token form the storage.
95
     *
96
     * @param Request $request
0 ignored issues
show
Bug introduced by
There is no parameter named $request. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
97
     * @param $name
98
     *
99
     * @return Response
100
     */
101
    public function removeAction($name)
102
    {
103
        /* @var \Google_Client $client */
104
        $this->clientProvider->setAccessToken(null, $name);
105
        $this->get('session')->getFlashbag()->add('msg', 'Token was removed.');
106
107
        return $this->redirect($this->generateUrl('happyr.google_site_authenticator.index'));
108
    }
109
110
    /**
111
     * This action is used when the user has authenticated with google.
112
     *
113
     * @param Request $request
114
     *
115
     * @return Response
116
     */
117
    public function returnAction(Request $request)
118
    {
119
        $name = $request->getSession()->get(self::SESSION_KEY, null);
120
121
        /* @var \Google_Client $client */
122
        $client = $this->clientProvider->getClient($name);
123
124
        $flashBag = $this->get('session')->getFlashbag();
125
        if ($request->query->has('code')) {
126
            try {
127
                $client->authenticate($request->query->get('code'));
128
                $this->clientProvider->setAccessToken($client->getAccessToken(), $name);
129
130
                $flashBag->add('msg', 'Successfully authenticated!');
131
            } catch (\Google_Auth_Exception $e) {
132
                $flashBag->add('error', $e->getMessage());
133
            }
134
        } else {
135
            $flashBag->add('error', 'Authentication aborted.');
136
        }
137
138
        return $this->redirect($this->generateUrl('happyr.google_site_authenticator.index'));
139
    }
140
}
141