Passed
Push — master ( e12b6b...5d580f )
by Nicolaas
02:13
created

SalesforceContactApiWrapper::assertEmail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This class has three functions:
5
 *  - check if a person is subscried
6
 *  - add Contact
7
 *  - update contact
8
 *
9
 * This class adds / updates subscribers to Salesforce
10
 * NOTE: same class in caltex.co.nz - win.z.co.nz please update both.
11
 * NOTE: please update caltex.co.nz first and then win ...
12
 */
13
14
class SalesforceContactApiWrapper
15
{
16
17
    /**
18
     * @param string $email
19
     * @param string $phoneNumber
20
     * @param string $firstName
21
     * @param string $lastName
22
     * @param array $extraFields
23
     *
24
     * @return bool
25
     */
26
    public function addSubscriber(
27
        $email,
28
        $phoneNumber = null,
29
        $firstName = null,
30
        $lastName = null,
31
        $extraFields = []
32
    ): bool
33
    {
34
        $this->assertEmail($email);
35
36
        $fields = $extraFields;
37
38
        $fields['Email'] = $email;
39
        $fields['FirstName'] = '-';
40
        $fields['LastName'] = '-';
41
42
        if ($firstName) {
43
            $fields['FirstName'] = $firstName;
44
        }
45
46
        if ($lastName) {
47
            $fields['LastName'] = $lastName;
48
        }
49
50
        if ($phoneNumber) {
51
            $fields['Phone'] = $phoneNumber;
52
        }
53
54
        $response = MySalesforcePartnerAPI::create_contact($fields);
55
56
        return $response ? true : false;
57
    }
58
59
    /**
60
     * @param string $email
61
     * @param array $extraFields
62
     *
63
     * @return bool
64
     */
65
    public function updateSubscriber(
66
        $email,
67
        $extraFields = []
68
    ): bool
69
    {
70
        $this->assertEmail($email);
71
72
        $fields = $extraFields;
73
74
        $fields['Email'] = $email;
75
76
        $response = MySalesforcePartnerAPI::update_contact($fields);
77
78
        return $response ? true : false;
79
    }
80
81
    /**
82
     * @param $email
83
     */
84
    private function assertEmail($email)
85
    {
86
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
87
            throw new InvalidArgumentException('Wrong email address format');
88
        }
89
    }
90
91
    /**
92
     * @param $email
93
     *
94
     * @return bool
95
     */
96
    public function isEmailRegistered($email) : bool
97
    {
98
        $subscriber = MySalesforcePartnerAPI::retrieve_contact($email);
99
100
        return $subscriber ? true : false;
0 ignored issues
show
introduced by
$subscriber is of type SForce\SObject, thus it always evaluated to true.
Loading history...
101
    }
102
}
103