Failed Conditions
Push — issue#767 ( d652de...c7fd2d )
by Guilherme
05:20
created

onAuthenticationSuccess()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 2
dl 0
loc 19
ccs 13
cts 13
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\CoreBundle\Handler;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\Security\Http\HttpUtils;
17
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
18
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
19
use LoginCidadao\CoreBundle\Entity\AccessSession;
20
21
class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler
22
{
23
    /** @var EntityManagerInterface */
24
    private $em;
25
26
    /**
27
     * Constructor
28
     * @param HttpUtils $httpUtils
29
     * @param EntityManagerInterface $em
30
     * @param array $options
31
     */
32 1
    public function __construct(
33
        HttpUtils $httpUtils,
34
        EntityManagerInterface $em,
35
        $options
36
    ) {
37 1
        parent::__construct($httpUtils, $options);
38 1
        $this->em = $em;
39 1
    }
40
41
    /**
42
     * This is called when an interactive authentication attempt succeeds. This
43
     * is called by authentication listeners inheriting from AbstractAuthenticationListener.
44
     * @param Request $request
45
     * @param TokenInterface $token
46
     * @return Response The response to return
47
     */
48 1
    function onAuthenticationSuccess(Request $request, TokenInterface $token)
0 ignored issues
show
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for onAuthenticationSuccess.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
49
    {
50 1
        $form = $request->get('login_form_type');
51 1
        if (isset($form['username'])) {
52
            $vars = array(
53 1
                'ip' => $request->getClientIp(),
54 1
                'username' => $form['username'],
55
            );
56 1
            $accessSession = $this->em->getRepository('LoginCidadaoCoreBundle:AccessSession')->findOneBy($vars);
57 1
            if (!$accessSession) {
0 ignored issues
show
introduced by
$accessSession is of type object, thus it always evaluated to true.
Loading history...
58 1
                $accessSession = new AccessSession();
59 1
                $accessSession->fromArray($vars);
60
            }
61 1
            $accessSession->setVal(0);
62 1
            $this->em->persist($accessSession);
63 1
            $this->em->flush();
64
        }
65
66 1
        return parent::onAuthenticationSuccess($request, $token);
67
    }
68
}
69