Completed
Push — develop ( caa666...8d2465 )
by Nate
28:00
created

ContactListContactsCriteria::setId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/hubspot/license
6
 * @link       https://www.flipboxfactory.com/software/hubspot/
7
 */
8
9
namespace flipbox\hubspot\criteria;
10
11
use craft\base\ElementInterface;
12
use flipbox\ember\helpers\ObjectHelper;
13
use flipbox\hubspot\fields\Objects;
14
use flipbox\hubspot\HubSpot;
15
use yii\base\BaseObject;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
class ContactListContactsCriteria extends BaseObject implements ContactListContactsCriteriaInterface
22
{
23
    use traits\TransformerCollectionTrait,
24
        traits\ConnectionTrait,
25
        traits\CacheTrait;
26
27
    /**
28
     * @var string
29
     */
30
    public $id = '';
31
32
    /**
33
     * @var array
34
     */
35
    public $vids = [];
36
37
    /**
38
     * @var array
39
     */
40
    public $emails = [];
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function getId()
46
    {
47
        return $this->id;
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function setId(string $id)
54
    {
55
        $this->id = $id;
56
        return $this;
57
    }
58
59
    /**
60
     * @param ElementInterface $element
61
     * @param Objects $field
62
     */
63
    public function addElementToPayload(ElementInterface $element, Objects $field)
64
    {
65
        $objectId = HubSpot::getInstance()->getObjectAssociations()->findObjectIdByElement(
66
            $element,
67
            $field
68
        );
69
70
        if ($objectId !== null) {
71
            $this->vids[] = $objectId;
72
            return;
73
        }
74
75
        if (null !== ($email = $element['email'] ?? null)) {
76
            $this->emails[] = $email;
77
            return;
78
        }
79
    }
80
81
    /**
82
     * @return mixed
83
     */
84
    public function getPayload()
85
    {
86
        return [
87
            'vids' => array_filter($this->vids),
88
            'emails' => array_filter($this->emails)
89
        ];
90
    }
91
92
    /**
93
     * @param array $config
94
     * @param null $source
95
     * @return mixed
96
     */
97
    public function get(array $config = [], $source = null)
98
    {
99
        $this->prepare($config);
100
101
        return HubSpot::getInstance()
102
            ->getResources()
103
            ->getContactListContacts()
104
            ->getFromCriteria($this)
105
            ->execute($source);
106
    }
107
108
    /**
109
     * @param array $config
110
     * @param null $source
111
     * @return mixed
112
     */
113
    public function add(array $config = [], $source = null)
114
    {
115
        $this->prepare($config);
116
117
        return HubSpot::getInstance()
118
            ->getResources()
119
            ->getContactListContacts()
120
            ->addFromCriteria($this)
121
            ->execute($source);
122
    }
123
124
    /**
125
     * @param array $config
126
     * @param null $source
127
     * @return mixed
128
     */
129
    public function remove(array $config = [], $source = null)
130
    {
131
        $this->prepare($config);
132
133
        return HubSpot::getInstance()
134
            ->getResources()
135
            ->getContactListContacts()
136
            ->removeFromCriteria($this)
137
            ->execute($source);
138
    }
139
140
    /**
141
     * @inheritdoc
142
     */
143
    public function fetch(array $config = [])
144
    {
145
        return $this->get($config);
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151
    protected function prepare(array $criteria = [])
152
    {
153
        ObjectHelper::populate(
154
            $this,
155
            $criteria
156
        );
157
    }
158
}
159