campaignMonitorPage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
4
class EcommerceNewsletterCampaignMonitorSignupDecoratorFormFixes extends Extension
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
    private static $fields_to_hide = array(
0 ignored issues
show
Unused Code introduced by
The property $fields_to_hide is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
7
        "FirstName",
8
        "Surname",
9
        "SubscribeChoice"
10
    );
11
    /**
12
     *
13
     * @var CampaignMonitorAPIConnector
14
     */
15
    private static $_api = null;
16
17
    /**
18
     *
19
     * @return CampaignMonitorAPIConnector
20
     */
21
    public function getAPI()
22
    {
23
        if (!self::$_api) {
24
            self::$_api = CampaignMonitorAPIConnector::create();
25
            self::$_api->init();
26
        }
27
        return self::$_api;
28
    }
29
30
    public function updateFields(FieldList $fields)
31
    {
32
        if ($this->hasCampaignMonitorPage()) {
33
            $page = $this->CampaignMonitorPage();
34
            if ($page->ReadyToReceiveSubscribtions()) {
35
                // Create fields
36
                $member = Member::currentUser();
37
                $api = $this->getAPI();
38
                $currentValues = $api->getSubscriber($page->ListID, $member);
39
                if (!$currentValues) {
40
                    $emailField = null;
0 ignored issues
show
Unused Code introduced by
$emailField is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
41
                    $emailRequired = true;
0 ignored issues
show
Unused Code introduced by
$emailRequired is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
42
                }
43
                if (!$member) {
44
                    $member = new Member();
45
                }
46
                $signupField = $member->getCampaignMonitorSignupField($page->ListID, "SubscribeChoice");
47
                $fieldsToHide = Config::inst()->get("EcommerceNewsletterCampaignMonitorSignupDecoratorFormFixes", "fields_to_hide");
48
                foreach ($fieldsToHide as $field) {
0 ignored issues
show
Bug introduced by
The expression $fieldsToHide of type array|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
49
                    Requirements::customCSS("#CMCustomField".$field." {display: none;}");
50
                }
51
                $config = EcommerceDBConfig::current_ecommerce_db_config();
52
                if ($config->CampaignMonitorSignupHeader) {
53
                    $fields->push(new HeaderField("CampaignMonitorNewsletterSignupHeader", $config->CampaignMonitorSignupHeader, 3));
54
                }
55
                if ($config->CampaignMonitorSignupIntro) {
56
                    $fields->push(new LiteralField("CampaignMonitorNewsletterSignupContent", "<p class=\"campaignMonitorNewsletterSignupContent\">".$config->CampaignMonitorSignupIntro."</p>"));
57
                }
58
                $label = $config->CampaignMonitorSignupLabel;
59
                if (!$label) {
60
                    $label = _t("EcommerceNewsletterCampaignMonitorSignupDecoratorFormFixes.JOIN", "Join");
0 ignored issues
show
Unused Code introduced by
$label is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
61
                }
62
                $fields->push(new CheckboxField("CampaignMonitorNewsletterSubscribeCheckBox", $config->CampaignMonitorSignupLabel));
63
                $fields->push($signupField);
64
                Requirements::customCSS("
65
                    #SubscribeChoice {display: none!important;}
66
                    .CMFieldsCustomFieldsHolder {display: none!important;}
67
                ");
68
                Requirements::customScript("jQuery(\"#CampaignMonitorNewsletterSubscribeCheckBox\").on(\"change\", function(){jQuery(\".CMFieldsCustomFieldsHolder\").slideToggle();});");
69
            }
70
        }
71
    }
72
73
74
    /**
75
     * adds the user to the list ...
76
     * @param Array $data
77
     * @param Form $form
78
     * @param Order $order
0 ignored issues
show
Documentation introduced by
Should the type for parameter $order not be Order|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
79
     * @param Member $member
0 ignored issues
show
Documentation introduced by
Should the type for parameter $member not be Member|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
80
     */
81
    public function saveAddressExtension($data, $form, $order = null, $member = null)
0 ignored issues
show
Unused Code introduced by
The parameter $order is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $member is not used and could be removed.

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

Loading history...
82
    {
83
        if (isset($data["CampaignMonitorNewsletterSubscribeCheckBox"]) && $data["CampaignMonitorNewsletterSubscribeCheckBox"]) {
84
            if ($this->hasCampaignMonitorPage()) {
85
                $page = $this->CampaignMonitorPage();
86
                if ($page->ReadyToReceiveSubscribtions()) {
87
88
                    //true until proven otherwise.
89
                    $newlyCreatedMember = false;
90
                    $api = $this->getAPI();
0 ignored issues
show
Unused Code introduced by
$api is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
91
                    $isSubscribe = isset($data["SubscribeChoice"]) && $data["SubscribeChoice"] == "Subscribe";
92
                    $member = Member::currentUser();
93
                    if (!$member) {
94
                        $memberAlreadyLoggedIn = false;
0 ignored issues
show
Unused Code introduced by
$memberAlreadyLoggedIn is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
95
                        $existingMember = Member::get()->filter(array("Email" => Convert::raw2sql($data["Email"])))->First();
96
                        //if($isSubscribe && $existingMember){
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
97
                        //$form->addErrorMessage('Email', _t("CAMPAIGNMONITORSIGNUPPAGE.EMAIL_EXISTS", "This email is already in use. Please log in for this email or try another email address."), 'warning');
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
98
                        //$this->redirectBack();
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
99
                        //return;
100
                        //}
101
                        $member = $existingMember;
102
                        if (!$member) {
103
                            $newlyCreatedMember = true;
104
                            $member = new Member();
105
                        }
106
                    }
107
108
                    //logged in: if the member already as someone else then you can't sign up.
109
                    else {
110
                        $memberAlreadyLoggedIn = true;
0 ignored issues
show
Unused Code introduced by
$memberAlreadyLoggedIn is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
111
                        //$existingMember = Member::get()
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
112
                        //	->filter(array("Email" => Convert::raw2sql($data["CampaignMonitorEmail"])))
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
113
                        //	->exclude(array("ID" => $member->ID))
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
114
                        //	->First();
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
115
                        //if($isSubscribe && $existingMember) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
116
                            //$form->addErrorMessage('CampaignMonitorEmail', _t("CAMPAIGNMONITORSIGNUPPAGE.EMAIL_EXISTS", "This email is already in use by someone else. Please log in for this email or try another email address."), 'warning');
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
117
                            //$this->redirectBack();
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
118
                            //return;
119
                        //}
120
                    }
121
122
                    //if this is a new member then we save the member
123
                    if ($isSubscribe) {
124
                        if ($newlyCreatedMember) {
125
                            $form->saveInto($member);
126
                            $member->Email = Convert::raw2sql($data["CampaignMonitorEmail"]);
127
                            $member->SetPassword = true;
128
                            $member->Password = Member::create_new_password();
0 ignored issues
show
Deprecated Code introduced by
The method Member::create_new_password() has been deprecated with message: 3.6.0..4.0.0

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...
129
                            $member->write();
130
                        }
131
                    }
132
                    $outcome = $member->processCampaignMonitorSignupField($page, $data, $form);
0 ignored issues
show
Unused Code introduced by
$outcome is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
133
                }
134
            }
135
        }
136
    }
137
138
    /**
139
     * returns ID of Mailing List that people are subscribing to.
140
     * @return CampaignMonitorPage
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
141
     */
142
    protected function hasCampaignMonitorPage()
143
    {
144
        $config = EcommerceDBConfig::current_ecommerce_db_config();
145
        return $config->CampaignMonitorSignupPageID ? true : false;
146
    }
147
148
    /**
149
     * returns ID of Mailing List that people are subscribing to.
150
     * @return CampaignMonitorPage
151
     */
152
    protected function campaignMonitorPage()
153
    {
154
        $config = EcommerceDBConfig::current_ecommerce_db_config();
155
        return $config->CampaignMonitorSignupPage();
156
    }
157
}
158