ProfileForm   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 96.55%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 75
ccs 28
cts 29
cp 0.9655
rs 10
c 0
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getFormActions() 0 4 1
A getFormFields() 0 9 1
A getCustomMessage() 0 3 1
A doSubmitProfile() 0 13 3
A setsiteConfig() 0 3 1
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
     * @var \SilverStripe\SiteConfig\SiteConfig
20
     */
21
    protected $siteConfig;
22
23 2
    public function __construct($controller, $name)
24
    {
25 2
        $this->setAttribute('id', 'ProfileForm');
26 2
        $this->setsiteConfig();
27 2
        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

27
        parent::/** @scrutinizer ignore-call */ 
28
                __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...
28
    }
29
30
    /**
31
     * Required FieldList creation on a ProfileForm
32
     *
33
     * @return \SilverStripe\Forms\FieldList
34
     */
35 2
    protected function getFormFields($controller = null)
36
    {
37 2
        $member = Security::getCurrentUser();
38 2
        $fieldList = parent::getFormFields();
39 2
        $fieldList->removeByName('Password');
40 2
        $fieldList->removeByName('Email');
41 2
        $this->setAttribute('id', 'ProfileForm');
42 2
        $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

42
        $fieldList->insertBefore(ReadOnlyField::create("Email", "Email", $member->Email), /** @scrutinizer ignore-type */ "Mobile");
Loading history...
43 2
        return $fieldList;
44
    }
45
46
    /**
47
     * Returns the Form action
48
     * @return FieldList Actions for this form.
49
     */
50 2
    protected function getFormActions()
51
    {
52 2
        return FieldList::create(
53 2
            FormAction::create("doSubmitProfile", "Submit")
54
        );
55
    }
56
57
    /**
58
     * Form action, updates the user profile.
59
     * @return \SilverStripe\Control\HTTPResponse
60
     */
61 1
    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

61
    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...
62
    {
63 1
        $member = Security::getCurrentUser();
64 1
        if ($member) {
0 ignored issues
show
introduced by
$member is of type SilverStripe\Security\Member, thus it always evaluated to true.
Loading history...
65 1
                $form->saveInto($member);
66 1
                $member->write();
67 1
                $msg = $this->getCustomMessage('ProfileUpdateSuccess') != ""
68 1
                    ? $this->getCustomMessage('ProfileUpdateSuccess') : "Profile updated!";
69 1
                $form->sessionMessage($msg, 'good');
70
        } else {
71
            return $this->controller->redirect($this->siteConfig->LoginUrl()->URLSegment);
72
        }
73 1
        return $this->controller->redirectBack();
74
    }
75
76
    /**
77
     * Return the Message from siteconfig
78
     * @return string
79
     */
80 1
    public function getCustomMessage($field)
81
    {
82 1
        return $this->siteConfig->$field;
83
    }
84
   
85
    /**
86
     * Assign siteconfig object
87
     */
88 2
    protected function setsiteConfig()
89
    {
90 2
        $this->siteConfig = SiteConfig::current_site_config();
91
    }
92
}
93