Completed
Push — master ( 22c6fd...5ce682 )
by
unknown
11:12
created

LoginPresenter::createComponentSignInForm()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
ccs 0
cts 18
cp 0
rs 8.8571
cc 1
eloc 17
nc 1
nop 1
crap 2
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', $this->translation['Username']);
44
45
        $form->addPassword('password', 'Heslo')
46
            ->setRequired('Please provide a password.')
47
            ->setAttribute('placeholder', $this->translation['Password']);
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
            $this->forward('Homepage:');
76
        } catch (NS\AuthenticationException $e) {
77
            $this->flashMessage($this->translation[$e->getMessage()], 'danger');
78
            $form->addError($e->getMessage());
79
        }
80
    }
81
82
}
83