ManagesCustomFields::findCustomField()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
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\Contact;
6
use TestMonitor\ActiveCampaign\Resources\CustomField;
7
8
trait ManagesCustomFields
9
{
10
    use ImplementsActions;
11
12
    /**
13
     * Get all custom fields.
14
     *
15
     * @return array
16
     */
17
    public function customFields()
18
    {
19
        return $this->transformCollection(
20
            $this->get('fields'),
21
            CustomField::class,
22
            'fields'
23
        );
24
    }
25
26
    /**
27
     * Find custom field by name.
28
     *
29
     * @param string $name
30
     *
31
     * @return CustomField|null
32
     */
33
    public function findCustomField($name)
34
    {
35
        $customFields = $this->transformCollection(
36
            $this->get('fields', ['query' => ['search' => $name]]),
37
            CustomField::class,
38
            'fields'
39
        );
40
41
        return array_shift($customFields);
42
    }
43
44
    /**
45
     * Add custom field value to contact.
46
     *
47
     * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact
48
     * @param \TestMonitor\ActiveCampaign\Resources\CustomField $customField
49
     * @param $value
50
     *
51
     * @return Contact
52
     */
53
    public function addCustomFieldToContact(Contact $contact, CustomField $customField, $value)
54
    {
55
        $data = [
56
            'contact' => $contact->id,
57
            'field' => $customField->id,
58
            'value' => $value,
59
        ];
60
61
        $contacts = $this->transformCollection(
62
            $this->post('fieldValues', ['json' => ['fieldValue' => $data]]),
63
            Contact::class,
64
            'contacts'
65
        );
66
67
        return array_shift($contacts);
68
    }
69
}
70