LoginPresenter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 25 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 6
dl 21
loc 84
ccs 0
cts 48
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeRender() 0 8 2
A startup() 0 4 1
B createComponentSignInForm() 0 28 1
A renderDefault() 0 4 1
A signInFormSubmitted() 21 21 3
A actionOut() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace FrontendModule;
4
5
use Nette\Application\UI;
6
use Kdyby\BootstrapFormRenderer\BootstrapRenderer;
7
use Nette\Security as NS;
8
9
class LoginPresenter extends \FrontendModule\BasePresenter
10
{
11
    public function beforeRender()
12
    {
13
        parent::beforeRender();
14
15
        if ($this->getUser()->isLoggedIn()) {
16
            $this->forward('Homepage:');
17
        }
18
    }
19
20
    protected function startup()
21
    {
22
        parent::startup();
23
    }
24
25
    /**
26
     * Sign in form component factory.
27
     * @return UI\Form
28
     */
29
    protected function createComponentSignInForm($name)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
30
    {
31
        $form = new UI\Form();
32
33
        $form->getElementPrototype()->action = $this->link('default', array(
34
            'path' => 'loginfe',
35
            'abbr' => $this->abbr,
36
            'do' => 'signInForm-submit',
37
        ));
38
39
        $form->setRenderer(new BootstrapRenderer());
40
41
        $form->addText('username', 'Přihlašovací jméno')
42
            ->setRequired('Please provide a username.')
43
            ->setAttribute('placeholder', 'Přihlašovací jméno');
0 ignored issues
show
Documentation introduced by
'Přihlašovací jméno' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
45
        $form->addPassword('password', 'Heslo')
46
            ->setRequired('Please provide a password.')
47
            ->setAttribute('placeholder', 'Heslo');
0 ignored issues
show
Documentation introduced by
'Heslo' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
49
        $form->addCheckbox('remember', 'Permanent login?');
50
51
        $form->addSubmit('send', 'Přihlásit');
52
53
        $form->onSuccess[] = callback($this, 'signInFormSubmitted');
54
55
        return $form;
56
    }
57
58
    public function renderDefault()
59
    {
60
        $this->setLayout('login');
61
    }
62
63 View Code Duplication
    public function signInFormSubmitted($form)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
66
        try {
67
            $values = $form->getValues();
68
            if ($values->remember) {
69
                $this->getUser()->setExpiration('+ 14 days', false);
70
            } else {
71
                $this->getUser()->setExpiration('+ 20 minutes', true);
72
            }
73
            $this->getUser()->login($values->username, $values->password);
74
            $this->flashMessage('Přihlášení bylo úspěšné', 'success');
75
76
            //TODO bude upraveno - ted nestiham :)
77
            //$this->forward('Homepage:');
78
            $this->redirectUrl('https://www.zajistenainvestice.cz/uvod');
79
        } catch (NS\AuthenticationException $e) {
80
            $this->flashMessage($this->translation[$e->getMessage()], 'danger');
81
            $form->addError($e->getMessage());
82
        }
83
    }
84
85
    public function actionOut()
86
    {
87
        $this->getUser()->logout();
88
        $this->flashMessage('Odhlášení bylo úspěšné', 'success');
89
        $this->forward('Homepage:');
90
    }
91
92
}
93