Completed
Push — develop ( 1d947e...a9d8b7 )
by Nate
05:38
created

ContactListContactsCriteria::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 craft\web\User;
13
use flipbox\hubspot\fields\Resources;
14
use flipbox\hubspot\HubSpot;
15
use flipbox\hubspot\services\resources\Companies;
16
use flipbox\hubspot\transformers\collections\DynamicTransformerCollection;
17
use flipbox\hubspot\transformers\DynamicModelSuccess;
18
use flipbox\hubspot\transformers\collections\TransformerCollectionInterface;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 */
24
class ContactListContactsCriteria extends BaseCriteria implements ResourceCriteriaInterface
25
{
26
    /**
27
     * @var mixed
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 mixed
43
     */
44
    public function getId()
45
    {
46
        return $this->id;
47
    }
48
49
    /**
50
     * @param ElementInterface $element
51
     * @param Resources $field
52
     */
53
    public function addElementToPayload(ElementInterface $element, Resources $field)
54
    {
55
        $hubSpotId = HubSpot::getInstance()->getResources()->getContacts()->findHubSpotIdByElement(
56
            $element,
57
            $field
58
        );
59
60
        if($hubSpotId !== null) {
61
            $this->vids[] = $hubSpotId;
62
            return;
63
        }
64
65
        if(null !== ($email = $element['email'] ?? null)) {
66
            $this->emails[] = $email;
67
            return;
68
        }
69
    }
70
71
    /**
72
     * @return mixed
73
     */
74
    public function getPayload()
75
    {
76
        return [
77
            'vids' => array_filter($this->vids),
78
            'emails' => array_filter($this->emails)
79
        ];
80
    }
81
82
//    /**
83
//     * @inheritdoc
84
//     */
85
//    protected $transformer = [
86
//        'class' => DynamicTransformerCollection::class,
87
//        'handle' => Companies::HUBSPOT_RESOURCE,
88
//        'transformers' => [
89
//            TransformerCollectionInterface::SUCCESS_KEY => [
90
//                'class' => DynamicModelSuccess::class,
91
//                'resource' => Companies::HUBSPOT_RESOURCE
92
//            ]
93
//        ]
94
//    ];
95
96
    /**
97
     * @param array $config
98
     * @param null $source
99
     * @return mixed
100
     */
101
    public function get(array $config = [], $source = null)
102
    {
103
        $this->prepare($config);
104
105
        return HubSpot::getInstance()
106
            ->getResources()
107
            ->getContactListContacts()
108
            ->getFromCriteria($this)
109
            ->execute($source);
110
    }
111
112
    /**
113
     * @param array $config
114
     * @param null $source
115
     * @return mixed
116
     */
117
    public function add(array $config = [], $source = null)
118
    {
119
        $this->prepare($config);
120
121
        return HubSpot::getInstance()
122
            ->getResources()
123
            ->getContactListContacts()
124
            ->addFromCriteria($this)
125
            ->execute($source);
126
    }
127
128
    /**
129
     * @param array $config
130
     * @param null $source
131
     * @return mixed
132
     */
133
    public function remove(array $config = [], $source = null)
134
    {
135
        $this->prepare($config);
136
137
        return HubSpot::getInstance()
138
            ->getResources()
139
            ->getContactListContacts()
140
            ->removeFromCriteria($this)
141
            ->execute($source);
142
    }
143
144
    /**
145
     * @inheritdoc
146
     */
147
    public function fetch(array $config = [])
148
    {
149
        return $this->get($config);
150
    }
151
}
152