MiniLoginForm::setMiniLoginFormPopupPosition()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace SilverStripe\RealMe\Authenticator;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\RealMe\Authenticator\LoginForm;
7
8
class MiniLoginForm extends LoginForm
9
{
10
    /**
11
     * @var string The position at which the 'What's RealMe?' popup appears on hover. Can be either 'left' or 'right'.
12
     * @see self::setMiniLoginFormPopupPosition()
13
     */
14
    private $popupPosition = 'left';
15
16
    public function __construct($controller, $name)
17
    {
18
        parent::__construct($controller, $name);
19
        $this->setFormMethod('GET', true);
20
21
        $buttonName = sprintf('action_%s', self::$action_button_name);
0 ignored issues
show
Bug Best Practice introduced by
The property action_button_name does not exist on SilverStripe\RealMe\Authenticator\MiniLoginForm. Since you implemented __get, consider adding a @property annotation.
Loading history...
22
        $this->Actions()->fieldByName($buttonName)->addExtraClass('mini');
23
    }
24
25
    public function getRealMeMiniLoginLink()
26
    {
27
        $fields = $this->Fields();
28
        $buttonName = sprintf('action_%s', self::$action_button_name);
0 ignored issues
show
Bug Best Practice introduced by
The property action_button_name does not exist on SilverStripe\RealMe\Authenticator\MiniLoginForm. Since you implemented __get, consider adding a @property annotation.
Loading history...
29
        $action = $this->Actions()->fieldByName($buttonName);
30
31
        $authMethod = $fields->dataFieldByName('AuthenticationMethod')->Value();
32
        $token = $fields->dataFieldByName('SecurityID')->Value();
33
        $actionName = $action->getName();
34
        $actionValue = _t(LoginForm::class . 'LOGINBUTTON', 'LoginAction');
35
36
        $queryString = sprintf(
37
            '?AuthenticationMethod=%s&SecurityID=%s&%s=%s',
38
            $authMethod,
39
            $token,
40
            $actionName,
41
            $actionValue
42
        );
43
        return Controller::join_links($this->FormAction(), $queryString);
44
    }
45
46
    public function getMiniLoginFormPopupPosition()
47
    {
48
        return sprintf('realme_arrow_top_%s', $this->popupPosition);
49
    }
50
51
    /**
52
     * The mini login form can either have the popup appear below and to the left or right. When creating the form, call
53
     * $form->setMiniLoginFormPopupPosition(), with the first arg being either 'left' or 'right'. This is actually the
54
     * 'arrow' position, so it's the opposite of what you expect (in other words, if you set it to 'left', the box will
55
     * extend out to the right under the mini login form.
56
     */
57
    public function setMiniLoginFormPopupPosition($dir)
58
    {
59
        if (!in_array($dir, [ 'left', 'right' ])) {
60
            $dir = 'left';
61
        }
62
63
        $this->popupPosition = $dir;
64
    }
65
}
66