subscribeToMailchimp()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 18

Duplication

Lines 7
Ratio 26.92 %

Importance

Changes 0
Metric Value
dl 7
loc 26
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 18
nc 2
nop 1
1
<?php
2
3
4
5
class EcommerceMailchimpSignupMemberExtension extends DataExtension
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...
6
{
7
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db 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...
8
        'SignedUpToMailchimp' => "Boolean"
9
    );
10
11
    /**
12
     * Store the user in MailChimp
13
     * @param array $mergeVars
14
     * @return Boolean
15
     */
16
    public function subscribeToMailchimp($mergeVars = array())
17
    {
18
        $listID = Config::inst()->get('EcommerceMailchimpSignupDecoratorFormFixes', 'mailchimp_list_id');
19
20
        $mailChimp = $this->getMailChimpAPI();
21
22
        $result = $mailChimp->post(
0 ignored issues
show
Unused Code introduced by
$result 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...
23
            "lists/".$listID."/members",
24
            array(
25
                'status'        => 'subscribed',
26
                'email_address' => $this->owner->Email,
27
                "merge_fields" => $mergeVars + array(
28
                    "FNAME" => $this->owner->FirstName,
29
                    "LNAME" => $this->owner->Surname
30
                )
31
            )
32
        );
33 View Code Duplication
        if ($mailChimp->success()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
            $this->owner->SignedUpToMailchimp = true;
35
            $this->owner->write();
36
            return true;
37
        } else {
38
            return false;
39
        }
40
        return $result;
0 ignored issues
show
Unused Code introduced by
return $result; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
41
    }
42
43
    /**
44
     *
45
     * @return bool
46
     */
47
    public function existsOnMailchimp()
48
    {
49
        if ($this->owner->SignedUpToMailchimp) {
50
            return true;
51
        }
52
53
        $mailChimp = $this->getMailChimpAPI();
54
55
        $listID = Config::inst()->get('EcommerceMailchimpSignupDecoratorFormFixes', 'mailchimp_list_id');
56
57
        $subscriberHash = $mailChimp->subscriberHash($this->owner->Email);
58
59
        $result = $mailChimp->get(
0 ignored issues
show
Unused Code introduced by
$result 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...
60
            "lists/".$listID."/members/".$subscriberHash
61
        );
62 View Code Duplication
        if ($mailChimp->success()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
            $this->owner->SignedUpToMailchimp = true;
64
            $this->owner->write();
65
            return true;
66
        } else {
67
            return false;
68
        }
69
    }
70
71
    /**
72
     *
73
     * @return bool
74
     */
75
    public function updateOnMailchimp()
76
    {
77
        $mailChimp = $this->getMailChimpAPI();
78
79
        $listID = Config::inst()->get('EcommerceMailchimpSignupDecoratorFormFixes', 'mailchimp_list_id');
80
81
        $subscriberHash = $mailChimp->subscriberHash($this->owner->Email);
82
83
        $result = $mailChimp->patch(
0 ignored issues
show
Unused Code introduced by
$result 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...
84
            "lists/".$listID."/members/".$subscriberHash,
85
            array(
86
                'merge_fields' => array(
87
                    'FNAME'=> $this->owner->FirstName,
88
                    'LNAME'=>$this->owner->Surname
89
                )
90
            )
91
        );
92
        if ($mailChimp->success()) {
0 ignored issues
show
Coding Style introduced by
The if-else statement can be simplified to return (bool) $mailChimp->success();.
Loading history...
93
            return true;
94
        } else {
95
            return false;
96
        }
97
    }
98
99
    /**
100
     * casted variable
101
     */
102
    private static $_mailchimp_api = null;
103
104
    /**
105
     * @return MailChimp
106
     */
107
    protected function getMailChimpAPI()
108
    {
109
        require_once(Director::baseFolder().'/vendor/drewm/mailchimp-api/src/MailChimp.php');
110
        if (self::$_mailchimp_api) {
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...
111
            //..
112
        } else {
113
            self::$_mailchimp_api = new \DrewM\MailChimp\MailChimp(Config::inst()->get('EcommerceMailchimpSignupDecoratorFormFixes', 'mailchimp_api_key'));
114
        }
115
        return self::$_mailchimp_api;
116
    }
117
}
118