Completed
Push — master ( 1e5bea...c96873 )
by Thijs
14s queued 10s
created

ManagesAccountContacts::deleteAccountContact()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
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\AccountContact;
6
7
trait ManagesAccountContacts
8
{
9
    use ImplementsActions;
10
11
    /**
12
     * Retrieve all existing account association.
13
     *
14
     * @return array
15
     */
16
    public function accountContacts()
17
    {
18
        return $this->transformCollection(
19
            $this->get('accountContacts'),
20
            AccountContact::class,
21
            'accountContacts'
22
        );
23
    }
24
25
    /**
26
     * Retrieve an existing account association given the contactID and accountID.
27
     *
28
     * @param int $contactId
29
     * @param int $accountId
30
     *
31
     * @return AccountContact|null
32
     */
33
    public function findAccountContact($contactId, $accountId)
34
    {
35
        $accounts = $this->transformCollection(
36
            $this->get('accountContacts', [
37
                'query' => [
38
                    'filters[contact]' => $contactId,
39
                    'filters[account]' => $accountId,
40
                ],
41
            ]),
42
            AccountContact::class,
43
            'accountContacts'
44
        );
45
46
        return array_shift($accounts);
47
    }
48
49
    /**
50
     * Retrieve an existing account association.
51
     *
52
     * @param int $id
53
     *
54
     * @return AccountContact|null
55
     */
56
    public function getAccountContact($id)
57
    {
58
        $association = $this->transformCollection(
59
            $this->get("accountContacts/{$id}"),
60
            AccountContact::class
61
        );
62
63
        return array_shift($association);
64
    }
65
66
    /**
67
     * Create a new account association.
68
     *
69
     * @param int $contact contact ID
70
     * @param int $account account ID
71
     * @param string $jobTitle
72
     *
73
     * @return AccountContact|null
74
     */
75
    public function createAccountContact($contact, $account, $jobTitle)
76
    {
77
        $data = compact(['contact', 'account', 'jobTitle']);
78
79
        $accounts = $this->transformCollection(
80
            $this->post('accountContacts', ['json' => ['accountContact' => $data]]),
81
            AccountContact::class
82
        );
83
84
        return array_shift($accounts);
85
    }
86
87
    /**
88
     * Update an existing account association.
89
     *
90
     * @param int $associationId
91
     * @param string $jobTitle
92
     *
93
     * @return AccountContact|null
94
     */
95
    public function updateAccountContact($associationId, $jobTitle)
96
    {
97
        $data = compact('jobTitle');
98
99
        $accounts = $this->transformCollection(
100
            $this->put("accountContacts/{$associationId}", ['json' => ['accountContact' => $data]]),
101
            AccountContact::class
102
        );
103
104
        return array_shift($accounts);
105
    }
106
107
    /**
108
     * Find or create an account contact association.
109
     *
110
     * @param int $contact contact ID
111
     * @param int $account account ID
112
     * @param string $jobTitle
113
     *
114
     * @return AccountContact
115
     */
116 View Code Duplication
    public function findOrCreateAccountContact($contact, $account, $jobTitle)
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...
117
    {
118
        $association = $this->findAccountContact($contact, $account);
119
120
        if ($association instanceof AccountContact) {
121
            return $association;
122
        }
123
124
        return $this->createAccountContact($contact, $account, $jobTitle);
125
    }
126
127
    /**
128
     * Update or create an account contact.
129
     *
130
     * @param int $contact contact ID
131
     * @param int $account account ID
132
     * @param string $jobTitle
133
     *
134
     * @return AccountContact
135
     */
136 View Code Duplication
    public function updateOrCreateAccountContact($contact, $account, $jobTitle)
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...
137
    {
138
        $association = $this->findAccountContact($contact, $account);
139
140
        if ($association instanceof AccountContact) {
141
            return $this->updateAccountContact($association->id, $jobTitle);
142
        }
143
144
        return $this->createAccountContact($contact, $account, $jobTitle);
145
    }
146
147
    /**
148
     * Delete an existing account association.
149
     *
150
     * @param int $associationId
151
     *
152
     * @return void
153
     */
154
    public function deleteAccountContact($associationId)
155
    {
156
        $this->delete("accountContacts/{$associationId}");
157
    }
158
}
159