Completed
Push — master ( 8c95bb...199f9c )
by Nate
15:05
created

ContactList::findObjectId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace flipbox\hubspot\fields;
4
5
use craft\base\ElementInterface;
6
use flipbox\hubspot\HubSpot;
7
8
class ContactList extends AbstractField
9
{
10
    const FIELD_TYPE = 'ContactList';
11
12
    /**
13
     * @param ElementInterface $element
14
     * @return null|string
15
     */
16
    protected function findObjectId(ElementInterface $element)
17
    {
18
        return HubSpot::getInstance()->getContactList()->findContactListIdByElementId(
19
            $element->getId()
20
        );
21
    }
22
23
    /**
24
     * @inheritdoc
25
     */
26
    public function afterElementSave(ElementInterface $element, bool $isNew)
27
    {
28
29
        $value = $element->getFieldValue(
30
            $this->handle
31
        );
32
33
        // Remove existing associations
34
        HubSpot::getInstance()->getContactList()->disassociate($element);
35
36
        if ($value) {
37
            HubSpot::getInstance()->getContactList()->associateByIds(
38
                $element->getId(),
39
                $value
40
            );
41
        }
42
43
        parent::afterElementSave($element, $isNew);
44
    }
45
}
46