Completed
Push — master ( e78df8...6c6364 )
by Nate
26:21 queued 16:45
created

ContactListContactsMutator::getPayload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\hubspot\fields\Objects;
13
use flipbox\hubspot\HubSpot;
14
use yii\base\BaseObject;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
class ContactListContactsMutator extends BaseObject implements ObjectMutatorInterface
21
{
22
    use traits\TransformerCollectionTrait,
23
        traits\ConnectionTrait,
24
        traits\CacheTrait;
25
26
    /**
27
     * @var string
28
     */
29
    public $id;
30
31
    /**
32
     * @var array
33
     */
34
    public $vids = [];
35
36
    /**
37
     * @var array
38
     */
39
    public $emails = [];
40
41
    /**
42
     * @return string
43
     */
44
    public function getId(): string
45
    {
46
        return (string)$this->id;
47
    }
48
49
    /**
50
     * @return array
51
     */
52
    public function getPayload(): array
53
    {
54
        return array_filter(
55
            [
56
                'vids' => array_filter($this->vids),
57
                'emails' => array_filter($this->emails)
58
            ]
59
        );
60
    }
61
62
    /**
63
     * @param ElementInterface $element
64
     * @param Objects $field
65
     */
66
    public function addElement(ElementInterface $element, Objects $field)
67
    {
68
        $objectId = HubSpot::getInstance()->getObjectAssociations()->findObjectIdByElement(
69
            $element,
70
            $field
71
        );
72
73
        if ($objectId !== null) {
74
            $this->vids[] = $objectId;
75
            return;
76
        }
77
78
        if (null !== ($email = $element['email'] ?? null)) {
79
            $this->emails[] = $email;
80
            return;
81
        }
82
    }
83
}
84