Completed
Push — master ( 613b2e...a2195f )
by Nicolaas
03:28
created

ShopAccountForm_Validator   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 114
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 8

1 Method

Rating   Name   Duplication   Size   Complexity  
F php() 0 102 22
1
<?php
2
3
4
class ShopAccountForm_Validator extends RequiredFields
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
5
{
6
7
    /**
8
     * Ensures member unique id stays unique and other basic stuff...
9
     *
10
     * @param array $data               = array Form Field Data
11
     * @param bool  $allowExistingEmail - see comment below
12
     *
13
     * @return bool
14
     **/
15
    public function php($data, $allowExistingEmail = false)
16
    {
17
        $this->form->saveDataToSession();
18
        $valid = parent::php($data);
19
        $uniqueFieldName = Member::get_unique_identifier_field();
0 ignored issues
show
Deprecated Code introduced by
The method Member::get_unique_identifier_field() has been deprecated with message: 4.0 Use the "Member.unique_identifier_field" config setting instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
20
        $loggedInMember = Member::currentUser();
21
        $loggedInMemberID = 0;
22
        if (isset($data[$uniqueFieldName]) && $data[$uniqueFieldName]) {
23
            $isShopAdmin = false;
24
            if ($loggedInMember) {
25
                $loggedInMemberID = $loggedInMember->ID;
26
                if ($loggedInMember->IsShopAdmin()) {
27
                    $isShopAdmin = true;
28
                }
29
            }
30
            if ($isShopAdmin || $allowExistingEmail) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
31
                //do nothing
32
            } else {
33
                $uniqueFieldValue = Convert::raw2sql($data[$uniqueFieldName]);
34
                //can't be taken
35
                $otherMembersWithSameEmail = Member::get()
36
                    ->filter(array($uniqueFieldName => $uniqueFieldValue))
37
                    ->exclude(array('ID' => $loggedInMemberID));
38
                if ($otherMembersWithSameEmail->count()) {
39
                    //we allow existing email
40
                    // if we are currently NOT logged in
41
                    // in case we place an order!
42
                    if ($allowExistingEmail) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
43
                        //do nothing
44
                    } else {
45
                        $message = _t(
46
                            'Account.ALREADYTAKEN',
47
                            '{uniqueFieldValue} is already taken by another member. Please log in or use another {uniqueFieldName}.',
48
                            array('uniqueFieldValue' => $uniqueFieldValue, 'uniqueFieldName' => $uniqueFieldName)
0 ignored issues
show
Documentation introduced by
array('uniqueFieldValue'...e' => $uniqueFieldName) is of type array<string,array|strin...ueFieldName":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
49
                        );
50
                        $this->validationError(
51
                            $uniqueFieldName,
52
                            $message,
53
                            'required'
54
                        );
55
                        $valid = false;
56
                    }
57
                }
58
            }
59
        }
60
        // check password fields are the same before saving
61
        if (isset($data['PasswordCheck1']) && isset($data['PasswordCheck2'])) {
62
            if ($data['PasswordCheck1'] != $data['PasswordCheck2']) {
63
                $this->validationError(
64
                    'PasswordCheck1',
65
                    _t('Account.PASSWORDSERROR', 'Passwords do not match.'),
66
                    'required'
67
                );
68
                $valid = false;
69
            }
70
            //if you are not logged in, you have not provided a password and the settings require you to be logged in then
71
            //we have a problem
72
            if (!$loggedInMember && !$data['PasswordCheck1'] && EcommerceConfig::get('EcommerceRole', 'must_have_account_to_purchase')) {
73
                $this->validationError(
74
                    'PasswordCheck1',
75
                    _t('Account.SELECTPASSWORD', 'Please select a password.'),
76
                    'required'
77
                );
78
                $valid = false;
79
            }
80
            $letterCount = strlen($data['PasswordCheck1']);
81
            $minLength = Config::inst()->get('ShopAccountForm_Validator', 'minimum_password_length');
82
            if ($letterCount > 0 && $letterCount < $minLength) {
83
                $this->validationError(
84
                    'PasswordCheck1',
85
                    _t('Account.PASSWORDMINIMUMLENGTH', 'Password does not meet minimum standards.'),
86
                    'required'
87
                );
88
                $valid = false;
89
            }
90
        }
91
        if (isset($data['FirstName'])) {
92
            if (strlen($data['FirstName']) < 2) {
93
                $this->validationError(
94
                    'FirstName',
95
                    _t('Account.NOFIRSTNAME', 'Please enter your first name.'),
96
                    'required'
97
                );
98
                $valid = false;
99
            }
100
        }
101
        if (isset($data['Surname'])) {
102
            if (strlen($data['Surname']) < 2) {
103
                $this->validationError(
104
                    'Surname',
105
                    _t('Account.NOSURNAME', 'Please enter your surname.'),
106
                    'required'
107
                );
108
                $valid = false;
109
            }
110
        }
111
        if (!$valid) {
112
            $this->form->sessionMessage(_t('Account.ERRORINFORM', 'We could not save your details, please check your errors below.'), 'bad');
113
        }
114
115
        return $valid;
116
    }
117
}
118