Passed
Branch master (6f4de6)
by Sathish
01:37
created

ProfileForm::getCustomMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace UserManagement\Forms;
3
4
use SilverStripe\Forms\FieldList;
5
use SilverStripe\Forms\Form;
6
use SilverStripe\Forms\ReadOnlyField;
7
use SilverStripe\Forms\FormAction;
8
use SilverStripe\SiteConfig\SiteConfig;
9
use SilverStripe\Security\Security;
10
11
/**
12
 * Class ProfileForm
13
 *
14
 * @package user-management
15
 */
16
class ProfileForm extends SignUpForm
17
{
18
    
19
    protected $siteConfig;
20
21
    public function __construct($controller, $name)
22
    {
23
        $this->setAttribute('id', 'ProfileForm');
24
        $this->setsiteConfig();
25
        parent::__construct($controller, $name, $this->getFormFields($controller), $this->getFormActions());
0 ignored issues
show
Unused Code introduced by
The call to UserManagement\Forms\SignUpForm::__construct() has too many arguments starting with $this->getFormFields($controller). ( Ignorable by Annotation )

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

25
        parent::/** @scrutinizer ignore-call */ 
26
                __construct($controller, $name, $this->getFormFields($controller), $this->getFormActions());

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
26
    }
27
    protected function getFormFields($controller = null)
28
    {
29
        $member = Security::getCurrentUser();
30
        $fieldList = parent::getFormFields();
31
        $fieldList->removeByName('Password');
32
        $fieldList->removeByName('Email');
33
        $this->setAttribute('id', 'ProfileForm');
34
        $fieldList->insertBefore(ReadOnlyField::create("Email", "Email", $member->Email), 'Mobile');
0 ignored issues
show
Bug introduced by
'Mobile' of type string is incompatible with the type SilverStripe\Forms\FormField expected by parameter $item of SilverStripe\Forms\FieldList::insertBefore(). ( Ignorable by Annotation )

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

34
        $fieldList->insertBefore(ReadOnlyField::create("Email", "Email", $member->Email), /** @scrutinizer ignore-type */ 'Mobile');
Loading history...
35
        return $fieldList;
36
    }
37
38
    /**
39
     * @return FieldList Actions for this form.
40
     */
41
    protected function getFormActions()
42
    {
43
        return FieldList::create(
44
            FormAction::create("doSubmitProfile", "Submit")
45
        );
46
    }
47
48
    /**
49
     * Form action, updates the user profile.
50
     */
51
    public function doSubmitProfile($data, Form $form)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

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

51
    public function doSubmitProfile(/** @scrutinizer ignore-unused */ $data, Form $form)

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

Loading history...
52
    {
53
        $member = Security::getCurrentUser();
54
        if ($member) {
0 ignored issues
show
introduced by
$member is of type SilverStripe\Security\Member, thus it always evaluated to true.
Loading history...
55
            try {
56
                $form->saveInto($member);
57
                $member->write();
58
                $msg = $this->getCustomMessage('ProfileUpdateSuccess')!=""
59
                    ? $this->getCustomMessage('ProfileUpdateSuccess') : "Profile updated!";
60
                $form->sessionMessage($msg, 'good');
61
            } 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...
62
                $msg = $this->getCustomMessage('ProfileUpdatError')!=""
63
                    ? $this->getCustomMessage('ProfileUpdatError') : "Technical issue, Profile not updated!";
64
                $form->sessionMessage($msg, 'bad');
65
            }
66
        } else {
67
            return $this->controller->redirect($this->siteConfig->LoginUrl()->URLSegment);
68
        }
69
        return $this->controller->redirectBack();
70
    }
71
72
    
73
    public function getCustomMessage($field)
74
    {
75
        return $this->siteConfig->$field;
76
    }
77
   
78
    protected function setsiteConfig()
79
    {
80
        $this->siteConfig = SiteConfig::current_site_config();
81
    }
82
}
83