Completed
Pull Request — master (#19)
by
unknown
09:59
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
    public function createContact($email, $firstName, $lastName, $orgid = null)
58
    {
59
        $contacts = $this->transformCollection(
60
            $this->post('contacts', ['json' => ['contact' => compact('email', 'firstName', 'lastName', 'orgid')]]),
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
     *
75
     * @return Contact
76
     */
77
    public function findOrCreateContact($email, $firstName, $lastName, $orgid = null)
78
    {
79
        $contact = $this->findContact($email);
80
81
        if ($contact instanceof Contact) {
82
            return $contact;
83
        }
84
85
        return $this->createContact($email, $firstName, $lastName, $orgid);
86
    }
87
88
    /**
89
     * Get all automations of a contact.
90
     *
91
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
92
     *
93
     * @return array
94
     */
95
    public function contactAutomations(Contact $contact)
96
    {
97
        return $this->transformCollection(
98
            $this->get("contacts/{$contact->id}/contactAutomations"),
99
            ContactAutomation::class,
100
            'contactAutomations'
101
        );
102
    }
103
104
    /**
105
     * Get all tags of a contact.
106
     *
107
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
108
     *
109
     * @return array
110
     */
111
    public function contactTags(Contact $contact)
112
    {
113
        return $this->transformCollection(
114
            $this->get("contacts/{$contact->id}/contactTags"),
115
            ContactTag::class,
116
            'contactTags'
117
        );
118
    }
119
    
120
     /**
121
     * Adds an automation to a contact.
122
     *
123
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
124
     * @param \TestMonitor\ActiveCampaign\Resources\Automation $automation
125
     */
126
    public function addAutomationToContact(Contact $contact, Automation $automation)
127
    {
128
        $contactAutomations = $this->contactAutomations($contact);
129
130
        $contactAutomation = current(array_filter($contactAutomations, function ($contactAutomation) use ($automation) {
131
            return $contactAutomation->automation == $automation->id;
132
        }));
133
134
        if (!empty($contactAutomation)) {
135
            return;
136
        }
137
138
        $data = ['contactAutomation' => ['contact'=>$contact->id, 'automation'=>$automation->id]];
139
        $this->post('contactAutomations', ['json' => $data]);
140
    }
141
142
143
    /**
144
     * Removing a automation from a contact.
145
     *
146
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
147
     * @param \TestMonitor\ActiveCampaign\Resources\Automation $automation
148
     */
149 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...
150
    {
151
        $contactAutomations = $this->contactAutomations($contact);
152
153
        $contactAutomation = current(array_filter($contactAutomations, function ($contactAutomation) use ($automation) {
154
            return $contactAutomation->automation == $automation->id;
155
        }));
156
157
        if (empty($contactAutomation)) {
158
            return;
159
        }
160
161
        $this->delete("contactAutomations/{$contactAutomation->id}");
162
    }
163
164
    /**
165
     * Removing all automations from a contact.
166
     *
167
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
168
     */
169
    public function removeAllAutomationsFromContact(Contact $contact)
170
    {
171
        $contactAutomations = $this->contactAutomations($contact);
172
173
        foreach ($contactAutomations as $contactAutomation) {
174
            $this->delete("contactAutomations/{$contactAutomation->id}");
175
        }
176
    }
177
178
    /**
179
     * Removing a tag from a contact.
180
     *
181
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
182
     * @param \TestMonitor\ActiveCampaign\Resources\Tag $tag
183
     */
184 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...
185
    {
186
        $contactTags = $this->contactTags($contact);
187
188
        $contactTag = current(array_filter($contactTags, function ($contactTag) use ($tag) {
189
            return $contactTag->tag == $tag->id;
190
        }));
191
192
        if (empty($contactTag)) {
193
            return;
194
        }
195
196
        $this->delete("contactTags/{$contactTag->id}");
197
    }
198
}
199