Completed
Pull Request — master (#16)
by Pablo
01:10
created

ManagesContacts::updateContactById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
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
    /**
14
     * Get all contacts.
15
     *
16
     * @return array
17
     */
18
    public function contacts()
19
    {
20
        return $this->transformCollection(
0 ignored issues
show
Bug introduced by
It seems like transformCollection() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
21
            $this->get('contacts'),
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
22
            Contact::class,
23
            'contacts'
24
        );
25
    }
26
27
    /**
28
     * Find contact by email.
29
     *
30
     * @param string $email
31
     *
32
     * @return Contact|null
33
     */
34
    public function findContact($email)
35
    {
36
        $contacts = $this->transformCollection(
0 ignored issues
show
Bug introduced by
It seems like transformCollection() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
37
            $this->get('contacts', ['query' => ['email' => $email]]),
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
38
            Contact::class,
39
            'contacts'
40
        );
41
42
        return array_shift($contacts);
43
    }
44
45
    /**
46
     * Create new contact.
47
     *
48
     * @param string $email
49
     * @param string $firstName
50
     * @param string $lastName
51
     * @param int|null $orgid
52
     *
53
     * @return Contact|null
54
     */
55
    public function createContact($email, $firstName, $lastName, $orgid = null)
56
    {
57
        $contacts = $this->transformCollection(
0 ignored issues
show
Bug introduced by
It seems like transformCollection() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
58
            $this->post('contacts', ['json' => ['contact' => compact('email', 'firstName', 'lastName', 'orgid')]]),
0 ignored issues
show
Bug introduced by
It seems like post() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
59
            Contact::class
60
        );
61
62
        return array_shift($contacts);
63
    }
64
65
    /**
66
     * Find or create a contact.
67
     *
68
     * @param string $email
69
     * @param string $firstName
70
     * @param string $lastName
71
     * @param int|null $orgid
72
     *
73
     * @return Contact
74
     */
75
    public function findOrCreateContact($email, $firstName, $lastName, $orgid = null)
76
    {
77
        $contact = $this->findContact($email);
78
79
        if ($contact instanceof Contact) {
80
            return $contact;
81
        }
82
83
        return $this->createContact($email, $firstName, $lastName, $orgid);
84
    }
85
86
    /**
87
     * Updates a contact by its ActiveCampaign ID.
88
     *
89
     * @param      $id
90
     * @param      $email
91
     * @param      $firstName
92
     * @param      $lastName
93
     * @param null $orgid
94
     *
95
     * @return Contact|null
96
     */
97
    public function updateContactById($id, $email, $firstName, $lastName, $orgid = null)
98
    {
99
        $this->put('contacts/' . $id, ['json' => ['contact' => compact('email', 'firstName', 'lastName', 'orgid')]]);
0 ignored issues
show
Bug introduced by
It seems like put() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
100
        return $this->findContactById($id);
0 ignored issues
show
Bug introduced by
The method findContactById() does not exist on TestMonitor\ActiveCampaign\Actions\ManagesContacts. Did you maybe mean findContact()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
101
    }
102
103
    /**
104
     * Get all automations of a contact.
105
     *
106
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
107
     *
108
     * @return array
109
     */
110
    public function contactAutomations(Contact $contact)
111
    {
112
        return $this->transformCollection(
0 ignored issues
show
Bug introduced by
It seems like transformCollection() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
113
            $this->get("contacts/{$contact->id}/contactAutomations"),
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
114
            ContactAutomation::class,
115
            'contactAutomations'
116
        );
117
    }
118
119
    /**
120
     * Get all tags of a contact.
121
     *
122
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
123
     *
124
     * @return array
125
     */
126
    public function contactTags(Contact $contact)
127
    {
128
        return $this->transformCollection(
0 ignored issues
show
Bug introduced by
It seems like transformCollection() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
129
            $this->get("contacts/{$contact->id}/contactTags"),
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
130
            ContactTag::class,
131
            'contactTags'
132
        );
133
    }
134
135
    /**
136
     * Removing a automation from a contact.
137
     *
138
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
139
     * @param \TestMonitor\ActiveCampaign\Resources\Automation $automation
140
     */
141 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...
142
    {
143
        $contactAutomations = $this->contactAutomations($contact);
144
145
        $contactAutomation = current(array_filter($contactAutomations, function ($contactAutomation) use ($automation) {
146
            return $contactAutomation->automation == $automation->id;
147
        }));
148
149
        if (empty($contactAutomation)) {
150
            return;
151
        }
152
153
        $this->delete("contactAutomations/{$contactAutomation->id}");
0 ignored issues
show
Bug introduced by
It seems like delete() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
154
    }
155
156
    /**
157
     * Removing all automations from a contact.
158
     *
159
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
160
     */
161
    public function removeAllAutomationsFromContact(Contact $contact)
162
    {
163
        $contactAutomations = $this->contactAutomations($contact);
164
165
        foreach ($contactAutomations as $contactAutomation) {
166
            $this->delete("contactAutomations/{$contactAutomation->id}");
0 ignored issues
show
Bug introduced by
It seems like delete() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
167
        }
168
    }
169
170
    /**
171
     * Removing a tag from a contact.
172
     *
173
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
174
     * @param \TestMonitor\ActiveCampaign\Resources\Tag $tag
175
     */
176 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...
177
    {
178
        $contactTags = $this->contactTags($contact);
179
180
        $contactTag = current(array_filter($contactTags, function ($contactTag) use ($tag) {
181
            return $contactTag->tag == $tag->id;
182
        }));
183
184
        if (empty($contactTag)) {
185
            return;
186
        }
187
188
        $this->delete("contactTags/{$contactTag->id}");
0 ignored issues
show
Bug introduced by
It seems like delete() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
189
    }
190
}
191