Completed
Pull Request — master (#17)
by Pablo
01:07
created

ManagesContacts::findContactById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 View Code Duplication
    public function findContact($email)
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...
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
     * Find contact by ID.
47
     *
48
     * @param int $id
49
     *
50
     * @return Contact|null
51
     */
52 View Code Duplication
    public function findContactById($id)
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...
53
    {
54
        $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...
55
            $this->get('contacts', ['query' => ['id' => $id]]),
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...
56
            Contact::class,
57
            'contacts'
58
        );
59
60
        return array_shift($contacts);
61
    }
62
63
    /**
64
     * Create new contact.
65
     *
66
     * @param string $email
67
     * @param string $firstName
68
     * @param string $lastName
69
     * @param int|null $orgid
70
     *
71
     * @return Contact|null
72
     */
73
    public function createContact($email, $firstName, $lastName, $orgid = null)
74
    {
75
        $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...
76
            $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...
77
            Contact::class
78
        );
79
80
        return array_shift($contacts);
81
    }
82
83
    /**
84
     * Find or create a contact.
85
     *
86
     * @param string $email
87
     * @param string $firstName
88
     * @param string $lastName
89
     * @param int|null $orgid
90
     *
91
     * @return Contact
92
     */
93
    public function findOrCreateContact($email, $firstName, $lastName, $orgid = null)
94
    {
95
        $contact = $this->findContact($email);
96
97
        if ($contact instanceof Contact) {
98
            return $contact;
99
        }
100
101
        return $this->createContact($email, $firstName, $lastName, $orgid);
102
    }
103
104
    /**
105
     * Updates a contact by its ActiveCampaign ID.
106
     *
107
     * @param      $id
108
     * @param      $email
109
     * @param      $firstName
110
     * @param      $lastName
111
     * @param null $orgid
112
     *
113
     * @return Contact|null
114
     */
115
    public function updateContactById($id, $email, $firstName, $lastName, $orgid = null)
116
    {
117
        $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...
118
119
        return $this->findContactById($id);
120
    }
121
122
    /**
123
     * Deletes a contact by its ActiveCampaign ID.
124
     *
125
     * @param int $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. 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...
126
     *
127
     * @return Contact|null
128
     */
129
    public function deleteContactById($idl)
0 ignored issues
show
Unused Code introduced by
The parameter $idl is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
130
    {
131
        $this->delete('contacts/'.$id);
0 ignored issues
show
Bug introduced by
The variable $id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The method delete() does not exist on TestMonitor\ActiveCampaign\Actions\ManagesContacts. Did you maybe mean deleteContactById()?

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...
132
    }
133
134
    /**
135
     * Get all automations of a contact.
136
     *
137
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
138
     *
139
     * @return array
140
     */
141
    public function contactAutomations(Contact $contact)
142
    {
143
        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...
144
            $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...
145
            ContactAutomation::class,
146
            'contactAutomations'
147
        );
148
    }
149
150
    /**
151
     * Get all tags of a contact.
152
     *
153
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
154
     *
155
     * @return array
156
     */
157
    public function contactTags(Contact $contact)
158
    {
159
        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...
160
            $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...
161
            ContactTag::class,
162
            'contactTags'
163
        );
164
    }
165
166
    /**
167
     * Removing a automation from a contact.
168
     *
169
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
170
     * @param \TestMonitor\ActiveCampaign\Resources\Automation $automation
171
     */
172 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...
173
    {
174
        $contactAutomations = $this->contactAutomations($contact);
175
176
        $contactAutomation = current(array_filter($contactAutomations, function ($contactAutomation) use ($automation) {
177
            return $contactAutomation->automation == $automation->id;
178
        }));
179
180
        if (empty($contactAutomation)) {
181
            return;
182
        }
183
184
        $this->delete("contactAutomations/{$contactAutomation->id}");
0 ignored issues
show
Bug introduced by
The method delete() does not exist on TestMonitor\ActiveCampaign\Actions\ManagesContacts. Did you maybe mean deleteContactById()?

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...
185
    }
186
187
    /**
188
     * Removing all automations from a contact.
189
     *
190
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
191
     */
192
    public function removeAllAutomationsFromContact(Contact $contact)
193
    {
194
        $contactAutomations = $this->contactAutomations($contact);
195
196
        foreach ($contactAutomations as $contactAutomation) {
197
            $this->delete("contactAutomations/{$contactAutomation->id}");
0 ignored issues
show
Bug introduced by
The method delete() does not exist on TestMonitor\ActiveCampaign\Actions\ManagesContacts. Did you maybe mean deleteContactById()?

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...
198
        }
199
    }
200
201
    /**
202
     * Removing a tag from a contact.
203
     *
204
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
205
     * @param \TestMonitor\ActiveCampaign\Resources\Tag $tag
206
     */
207 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...
208
    {
209
        $contactTags = $this->contactTags($contact);
210
211
        $contactTag = current(array_filter($contactTags, function ($contactTag) use ($tag) {
212
            return $contactTag->tag == $tag->id;
213
        }));
214
215
        if (empty($contactTag)) {
216
            return;
217
        }
218
219
        $this->delete("contactTags/{$contactTag->id}");
0 ignored issues
show
Bug introduced by
The method delete() does not exist on TestMonitor\ActiveCampaign\Actions\ManagesContacts. Did you maybe mean deleteContactById()?

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...
220
    }
221
}
222