Completed
Push — develop ( 3644a8...72e19e )
by Nate
06:59
created

ContactListContactsCriteria   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 6
dl 0
loc 143
ccs 0
cts 64
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 4 1
A addElementToPayload() 0 17 3
A getPayload() 0 7 1
A get() 0 10 1
A add() 0 10 1
A remove() 0 10 1
A fetch() 0 4 1
A prepare() 0 7 1
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\Resources;
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 ObjectCriteriaInterface
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
     * @return mixed
44
     */
45
    public function getId()
46
    {
47
        return $this->id;
48
    }
49
50
    /**
51
     * @param ElementInterface $element
52
     * @param Resources $field
53
     */
54
    public function addElementToPayload(ElementInterface $element, Resources $field)
55
    {
56
        $hubSpotId = HubSpot::getInstance()->getResources()->getContacts()->findHubSpotIdByElement(
57
            $element,
58
            $field
59
        );
60
61
        if ($hubSpotId !== null) {
62
            $this->vids[] = $hubSpotId;
63
            return;
64
        }
65
66
        if (null !== ($email = $element['email'] ?? null)) {
67
            $this->emails[] = $email;
68
            return;
69
        }
70
    }
71
72
    /**
73
     * @return mixed
74
     */
75
    public function getPayload()
76
    {
77
        return [
78
            'vids' => array_filter($this->vids),
79
            'emails' => array_filter($this->emails)
80
        ];
81
    }
82
83
//    /**
84
//     * @inheritdoc
85
//     */
86
//    protected $transformer = [
87
//        'class' => DynamicTransformerCollection::class,
88
//        'handle' => Companies::HUBSPOT_RESOURCE,
89
//        'transformers' => [
90
//            TransformerCollectionInterface::SUCCESS_KEY => [
91
//                'class' => DynamicModelSuccess::class,
92
//                'resource' => Companies::HUBSPOT_RESOURCE
93
//            ]
94
//        ]
95
//    ];
96
97
    /**
98
     * @param array $config
99
     * @param null $source
100
     * @return mixed
101
     */
102
    public function get(array $config = [], $source = null)
103
    {
104
        $this->prepare($config);
105
106
        return HubSpot::getInstance()
107
            ->getResources()
108
            ->getContactListContacts()
109
            ->getFromCriteria($this)
110
            ->execute($source);
111
    }
112
113
    /**
114
     * @param array $config
115
     * @param null $source
116
     * @return mixed
117
     */
118
    public function add(array $config = [], $source = null)
119
    {
120
        $this->prepare($config);
121
122
        return HubSpot::getInstance()
123
            ->getResources()
124
            ->getContactListContacts()
125
            ->addFromCriteria($this)
126
            ->execute($source);
127
    }
128
129
    /**
130
     * @param array $config
131
     * @param null $source
132
     * @return mixed
133
     */
134
    public function remove(array $config = [], $source = null)
135
    {
136
        $this->prepare($config);
137
138
        return HubSpot::getInstance()
139
            ->getResources()
140
            ->getContactListContacts()
141
            ->removeFromCriteria($this)
142
            ->execute($source);
143
    }
144
145
    /**
146
     * @inheritdoc
147
     */
148
    public function fetch(array $config = [])
149
    {
150
        return $this->get($config);
151
    }
152
153
    /**
154
     * @inheritdoc
155
     */
156
    protected function prepare(array $criteria = [])
157
    {
158
        ObjectHelper::populate(
159
            $this,
160
            $criteria
161
        );
162
    }
163
}
164