Completed
Push — master ( c851e7...eb65b8 )
by Ben
02:09
created

Easychimp   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 136
Duplicated Lines 22.06 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 15
c 4
b 0
f 1
lcom 1
cbo 2
dl 30
loc 136
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isSubscribed() 15 15 3
A __construct() 0 4 1
B subscribe() 0 29 5
A unsubscribe() 15 15 3
A interestCategories() 0 6 1
A interests() 0 6 1
A hashEmail() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Easychimp;
4
5
use Mailchimp\Mailchimp;
6
7
class Easychimp
8
{
9
10
    /** @var Mailchimp */
11
    protected $api;
12
13
    public function __construct($apiKey)
14
    {
15
        $this->api = new Mailchimp($apiKey);
16
    }
17
18
    /**
19
     * @param $listId
20
     * @param $email
21
     *
22
     * @throws \Exception
23
     *
24
     * @return boolean
25
     */
26 View Code Duplication
    public function isSubscribed($listId, $email)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
27
    {
28
        try {
29
            $result = $this->api->get('lists/'.$listId.'/members/'.$this->hashEmail($email));
30
31
            return $result->get('status') == 'subscribed';
32
        } catch (\Exception $e) {
33
            # Email address isn't on this list
34
            if (str_contains($e->getMessage(), 'Resource Not Found')) {
35
                return false;
36
            }
37
38
            throw $e;
39
        }
40
    }
41
42
    /**
43
     * @param $listId
44
     * @param $email
45
     * @param array $extras
0 ignored issues
show
Bug introduced by
There is no parameter named $extras. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
46
     *
47
     * @throws \Exception
48
     *
49
     * @return boolean
50
     */
51
    public function subscribe(
52
        $listId,
53
        $email,
54
        $firstName = null,
55
        $lastName = null,
56
        array $interests = null
57
    ) {
58
        $mergeFields = [];
59
        if (!is_null($firstName)) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, please use === null instead of is_null.
Loading history...
60
            $mergeFields['FNAME'] = $firstName;
61
        }
62
63
        if (!is_null($lastName)) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, please use === null instead of is_null.
Loading history...
64
            $mergeFields['LNAME'] = $lastName;
65
        }
66
        $data = [
67
            'email_address' => $email,
68
            'status'        => 'subscribed',
69
            'merge_fields'  => (object) $mergeFields
70
        ];
71
72
        if (!is_null($interests)) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, please use === null instead of is_null.
Loading history...
73
            $data['interests'] = (object) array_flip($interests);
74
        }
75
76
        $result = $this->api->post('lists/'.$listId.'/members', $data);
77
78
        return $result->has('id') && strlen($result->get('id')) > 0;
79
    }
80
81
    /**
82
     * @param $listId
83
     * @param $email
84
     *
85
     * @throws \Exception
86
     *
87
     * @return boolean
88
     */
89 View Code Duplication
    public function unsubscribe($listId, $email)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
90
    {
91
        try {
92
            $result = $this->api->delete('lists/'.$listId.'/members/'.$this->hashEmail($email));
93
94
            return $result->count() == 0;
95
        } catch (\Exception $e) {
96
            # Email address isn't on this list
97
            if (str_contains($e->getMessage(), 'Resource Not Found')) {
98
                return true;
99
            }
100
101
            throw $e;
102
        }
103
    }
104
105
    /**
106
     * @param $listId
107
     *
108
     * @throws \Exception
109
     *
110
     * @return []
0 ignored issues
show
Documentation introduced by
The doc-type [] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
111
     */
112
    public function interestCategories($listId)
113
    {
114
        $result = $this->api->get('lists/'.$listId.'/interest-categories');
115
116
        return $result->get('categories');
117
    }
118
119
    /**
120
     * @param $listId
121
     * @param $interestCategoryId
122
     *
123
     * @throws \Exception
124
     *
125
     * @return []
0 ignored issues
show
Documentation introduced by
The doc-type [] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
126
     */
127
    public function interests($listId, $interestCategoryId)
128
    {
129
        $result = $this->api->get('lists/'.$listId.'/interest-categories/'.$interestCategoryId.'/interests');
130
131
        return $result->get('interests');
132
    }
133
134
    /**
135
     * @param $email
136
     * @return string
137
     */
138
    protected function hashEmail($email)
139
    {
140
        return md5(strtolower($email));
141
    }
142
}