Completed
Pull Request — master (#19)
by
unknown
01:07
created

ManagesContacts::addAutomationToContact()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace TestMonitor\ActiveCampaign\Actions;
4
5
use TestMonitor\ActiveCampaign\Resources\Automation;
6
use TestMonitor\ActiveCampaign\Resources\Contact;
7
use TestMonitor\ActiveCampaign\Resources\ContactAutomation;
8
use TestMonitor\ActiveCampaign\Resources\ContactTag;
9
use TestMonitor\ActiveCampaign\Resources\Tag;
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 $orgid
54
     *
55
     * @return Contact|null
56
     */
57 View Code Duplication
    public function createContact($email, $firstName, $lastName, $orgid = null, $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', 'orgid', '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 int|null $orgid
74
     * @param string $phone
75
     *
76
     * @return Contact
77
     */
78 View Code Duplication
    public function findOrCreateContact($email, $firstName, $lastName, $orgid = null, $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...
79
    {
80
        $contacts = $this->transformCollection(
81
            $this->post('contact/sync', ['json' => ['contact' => compact('email', 'firstName', 'lastName', 'orgid', 'phone')]]),
82
            Contact::class
83
        );
84
85
        return array_shift($contacts);
86
87
    }
88
89
90
    /**
91
     * Get all automations of a contact.
92
     *
93
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
94
     *
95
     * @return array
96
     */
97
    public function contactAutomations(Contact $contact)
98
    {
99
        return $this->transformCollection(
100
            $this->get("contacts/{$contact->id}/contactAutomations"),
101
            ContactAutomation::class,
102
            'contactAutomations'
103
        );
104
    }
105
106
    /**
107
     * Get all tags of a contact.
108
     *
109
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
110
     *
111
     * @return array
112
     */
113
    public function contactTags(Contact $contact)
114
    {
115
        return $this->transformCollection(
116
            $this->get("contacts/{$contact->id}/contactTags"),
117
            ContactTag::class,
118
            'contactTags'
119
        );
120
    }
121
    
122
     /**
123
     * Adds an automation to a contact.
124
     *
125
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
126
     * @param \TestMonitor\ActiveCampaign\Resources\Automation $automation
127
     */
128
    public function addAutomationToContact(Contact $contact, Automation $automation)
129
    {
130
        $contactAutomations = $this->contactAutomations($contact);
131
132
        $contactAutomation = current(array_filter($contactAutomations, function ($contactAutomation) use ($automation) {
133
            return $contactAutomation->automation == $automation->id;
134
        }));
135
136
        if (!empty($contactAutomation)) {
137
            return;
138
        }
139
140
        $data = ['contactAutomation' => ['contact'=>$contact->id, 'automation'=>$automation->id]];
141
        $this->post('contactAutomations', ['json' => $data]);
142
    }
143
144
145
    /**
146
     * Removing a automation from a contact.
147
     *
148
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
149
     * @param \TestMonitor\ActiveCampaign\Resources\Automation $automation
150
     */
151 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...
152
    {
153
        $contactAutomations = $this->contactAutomations($contact);
154
155
        $contactAutomation = current(array_filter($contactAutomations, function ($contactAutomation) use ($automation) {
156
            return $contactAutomation->automation == $automation->id;
157
        }));
158
159
        if (empty($contactAutomation)) {
160
            return;
161
        }
162
163
        $this->delete("contactAutomations/{$contactAutomation->id}");
164
    }
165
166
    /**
167
     * Removing all automations from a contact.
168
     *
169
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
170
     */
171
    public function removeAllAutomationsFromContact(Contact $contact)
172
    {
173
        $contactAutomations = $this->contactAutomations($contact);
174
175
        foreach ($contactAutomations as $contactAutomation) {
176
            $this->delete("contactAutomations/{$contactAutomation->id}");
177
        }
178
    }
179
180
    /**
181
     * Removing a tag from a contact.
182
     *
183
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
184
     * @param \TestMonitor\ActiveCampaign\Resources\Tag $tag
185
     */
186 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...
187
    {
188
        $contactTags = $this->contactTags($contact);
189
190
        $contactTag = current(array_filter($contactTags, function ($contactTag) use ($tag) {
191
            return $contactTag->tag == $tag->id;
192
        }));
193
194
        if (empty($contactTag)) {
195
            return;
196
        }
197
198
        $this->delete("contactTags/{$contactTag->id}");
199
    }
200
}
201