Passed
Push — master ( 36d94c...6f4de6 )
by Sathish
01:39
created

SignUpForm   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormFields() 0 16 1
A __construct() 0 5 1
A setsiteConfig() 0 3 1
A doSubmit() 0 19 4
A getFormActions() 0 4 1
1
<?php
2
namespace UserManagement\Forms;
3
4
use SilverStripe\Forms\FieldList;
5
use SilverStripe\Forms\Form;
6
use SilverStripe\Forms\EmailField;
7
use SilverStripe\Forms\DateField;
8
use SilverStripe\Forms\FormAction;
9
use SilverStripe\Forms\TextField;
10
use SilverStripe\Security\Member;
11
use SilverStripe\SiteConfig\SiteConfig;
12
use SilverStripe\Control\Controller;
13
use SilverStripe\Forms\OptionsetField;
14
use SilverStripe\Forms\ConfirmedPasswordField;
15
use SilverStripe\Security\Security;
16
use SilverStripe\Security\RequestAuthenticationHandler;
17
use SilverStripe\Core\Injector\Injector;
18
use SilverStripe\Security\IdentityStore;
19
use Psr\Log\LoggerInterface;
20
21
/**
22
 * Class SignUpForm
23
 *
24
 * @package user-management
25
 */
26
class SignUpForm extends Form
27
{
28
29
    protected $siteConfig;
30
    /**
31
     * Constructor
32
     */
33
    public function __construct($controller, $name)
34
    {
35
        $this->setsiteConfig();
36
        $this->setAttribute('id', 'SignUpForm');
37
        parent::__construct($controller, $name, $this->getFormFields($controller), $this->getFormActions());
38
    }
39
    
40
    /**
41
     * Form action, Register user and redirect to the call back url with auto login.
42
     */
43
    public function doSubmit($data, Form $form)
44
    {
45
       
46
        try {
47
            $signUpPersonal = Member::create();
48
            $form->saveInto($signUpPersonal);
49
            if (isset($data['DOB']) && $data['DOB']) {
50
                  $signUpPersonal->DOB = date('Y-m-d', strtotime($data['DOB']));
51
            }
52
            $signUpPersonal->write();
53
            $Member = Member::get()->byId($signUpPersonal->ID);
54
            $assignGroup = $signUpPersonal->Groups();
55
            $assignGroup->add($this->siteConfig->CustomerGroup());
56
            Injector::inst()->get(IdentityStore::class)->logIn($Member);
57
            //TO DO
58
            //$this->sessionMessage('Profile Created!', 'good');
59
            return $this->controller->redirect($this->siteConfig->LoginCallBackUrl()->URLSegment); #TODO
60
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type UserManagement\Forms\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
61
            $this->logger->log('Error occured in signup');
0 ignored issues
show
Bug Best Practice introduced by
The property logger does not exist on UserManagement\Forms\SignUpForm. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method log() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
            $this->logger->/** @scrutinizer ignore-call */ 
62
                           log('Error occured in signup');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
        }
63
    }
64
65
66
     /**
67
     * @param Controller $controller the controller instance that is being passed to the form
68
     * @return FieldList Fields for this form.
69
     */
70
    protected function getFormFields($controller = null)
71
    {
72
        $fields = FieldList::create(
73
            TextField::create("FirstName", "FirstName"),
74
            TextField::create("Surname", "Surname"),
75
            TextField::create("NickName", "NickName"),
76
            DateField::create("DOB", "DOB"),
77
            ConfirmedPasswordField::create("Password", "Password"),
78
            EmailField::create("Email", "Email"),
79
            TextField::create("Mobile", "Mobile"),
80
            TextField::create("SecurityQuestion", "SecurityQuestion"),
81
            TextField::create("SecurityAnswer", "SecurityAnswer")
82
        );
83
        // Update the fields using updateSignUpForm
84
        $this->extend('updateSignUpForm', $fields);
85
        return $fields;
86
    }
87
88
89
    /**
90
     * @return FieldList Actions for this form.
91
     */
92
    protected function getFormActions()
93
    {
94
        return FieldList::create(
95
            FormAction::create("doSubmit", "Submit")
96
        );
97
    }
98
99
100
    protected function setsiteConfig()
101
    {
102
        $this->siteConfig = SiteConfig::current_site_config();
103
    }
104
}
105