ManagesContacts::findOrCreateContact()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 4
1
<?php
2
3
namespace TestMonitor\ActiveCampaign\Actions;
4
5
use TestMonitor\ActiveCampaign\Resources\Tag;
6
use TestMonitor\ActiveCampaign\Resources\Contact;
7
use TestMonitor\ActiveCampaign\Resources\Automation;
8
use TestMonitor\ActiveCampaign\Resources\ContactTag;
9
use TestMonitor\ActiveCampaign\Resources\ContactAutomation;
10
11
trait ManagesContacts
12
{
13
    use ImplementsActions;
14
15
    /**
16
     * Get all contacts.
17
     *
18
     * @return array
19
     */
20
    public function contacts()
21
    {
22
        return $this->transformCollection(
23
            $this->get('contacts'),
24
            Contact::class,
25
            'contacts'
26
        );
27
    }
28
29
    /**
30
     * Find contact by email.
31
     *
32
     * @param string $email
33
     *
34
     * @return Contact|null
35
     */
36
    public function findContact($email)
37
    {
38
        $contacts = $this->transformCollection(
39
            $this->get('contacts', ['query' => ['email' => $email]]),
40
            Contact::class,
41
            'contacts'
42
        );
43
44
        return array_shift($contacts);
45
    }
46
47
    /**
48
     * Create new contact.
49
     *
50
     * @param string $email
51
     * @param string $firstName
52
     * @param string $lastName
53
     * @param int|null $phone
54
     *
55
     * @return Contact|null
56
     */
57 View Code Duplication
    public function createContact($email, $firstName, $lastName, $phone = null)
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...
58
    {
59
        $contacts = $this->transformCollection(
60
            $this->post('contacts', ['json' => ['contact' => compact('email', 'firstName', 'lastName', 'phone')]]),
61
            Contact::class
62
        );
63
64
        return array_shift($contacts);
65
    }
66
67
    /**
68
     * Find or create a contact.
69
     *
70
     * @param string $email
71
     * @param string $firstName
72
     * @param string $lastName
73
     * @param string $phone
74
     *
75
     * @return Contact
76
     */
77
    public function findOrCreateContact($email, $firstName, $lastName, $phone)
78
    {
79
        $contact = $this->findContact($email);
80
81
        if ($contact instanceof Contact) {
82
            return $contact;
83
        }
84
85
        return $this->createContact($email, $firstName, $lastName, $phone);
86
    }
87
88
    /**
89
     * Update or create an account.
90
     *
91
     * @param string $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. 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...
92
     * @param array $data
0 ignored issues
show
Bug introduced by
There is no parameter named $data. 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...
93
     *
94
     * @return Contact
95
     */
96 View Code Duplication
    public function updateOrCreateContact($email, $firstName, $lastName, $phone)
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...
97
    {
98
        $contacts = $this->transformCollection(
99
            $this->post('contact/sync', ['json' => ['contact' => compact('email', 'firstName', 'lastName', 'phone')]]),
100
            Contact::class
101
        );
102
103
        return array_shift($contacts);
104
    }
105
106
    /**
107
     * Get all automations of a contact.
108
     *
109
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
110
     *
111
     * @return array
112
     */
113
    public function contactAutomations(Contact $contact)
114
    {
115
        return $this->transformCollection(
116
            $this->get("contacts/{$contact->id}/contactAutomations"),
117
            ContactAutomation::class,
118
            'contactAutomations'
119
        );
120
    }
121
122
    /**
123
     * Get all tags of a contact.
124
     *
125
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
126
     *
127
     * @return array
128
     */
129
    public function contactTags(Contact $contact)
130
    {
131
        return $this->transformCollection(
132
            $this->get("contacts/{$contact->id}/contactTags"),
133
            ContactTag::class,
134
            'contactTags'
135
        );
136
    }
137
138
    /**
139
     * Removing a automation from a contact.
140
     *
141
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
142
     * @param \TestMonitor\ActiveCampaign\Resources\Automation $automation
143
     */
144 View Code Duplication
    public function removeAutomationFromContact(Contact $contact, Automation $automation)
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...
145
    {
146
        $contactAutomations = $this->contactAutomations($contact);
147
148
        $contactAutomation = current(array_filter($contactAutomations, function ($contactAutomation) use ($automation) {
149
            return $contactAutomation->automation == $automation->id;
150
        }));
151
152
        if (empty($contactAutomation)) {
153
            return;
154
        }
155
156
        $this->delete("contactAutomations/{$contactAutomation->id}");
157
    }
158
159
    /**
160
     * Removing all automations from a contact.
161
     *
162
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
163
     */
164
    public function removeAllAutomationsFromContact(Contact $contact)
165
    {
166
        $contactAutomations = $this->contactAutomations($contact);
167
168
        foreach ($contactAutomations as $contactAutomation) {
169
            $this->delete("contactAutomations/{$contactAutomation->id}");
170
        }
171
    }
172
173
    /**
174
     * Removing a tag from a contact.
175
     *
176
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
177
     * @param \TestMonitor\ActiveCampaign\Resources\Tag $tag
178
     */
179 View Code Duplication
    public function removeTagFromContact(Contact $contact, Tag $tag)
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...
180
    {
181
        $contactTags = $this->contactTags($contact);
182
183
        $contactTag = current(array_filter($contactTags, function ($contactTag) use ($tag) {
184
            return $contactTag->tag == $tag->id;
185
        }));
186
187
        if (empty($contactTag)) {
188
            return;
189
        }
190
191
        $this->delete("contactTags/{$contactTag->id}");
192
    }
193
}
194