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

Contact   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 38
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A findObjectId() 0 6 1
A afterElementSave() 0 19 2
1
<?php
2
3
namespace flipbox\hubspot\fields;
4
5
use craft\base\ElementInterface;
6
use flipbox\hubspot\HubSpot;
7
8
class Contact extends AbstractField
9
{
10
    const FIELD_TYPE = 'Contact';
11
12
    /**
13
     * @param ElementInterface $element
14
     * @return null|string
15
     */
16
    protected function findObjectId(ElementInterface $element)
17
    {
18
        return HubSpot::getInstance()->getContact()->findContactIdByElementId(
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()->getContact()->disassociate($element);
35
36
        if ($value) {
37
            HubSpot::getInstance()->getContact()->associateByIds(
38
                $element->getId(),
39
                $value
40
            );
41
        }
42
43
        parent::afterElementSave($element, $isNew);
44
    }
45
}
46