Completed
Push — develop ( 5f953b...639fa1 )
by Nate
02:34
created

ContactListContacts   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 9
dl 0
loc 177
ccs 0
cts 62
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getCriteria() 0 4 1
A get() 0 19 1
A add() 0 21 1
A remove() 0 21 1
A addByElement() 0 22 3
A getByCriteria() 0 10 1
A addByCriteria() 0 11 1
A removeByCriteria() 0 11 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\services\resources;
10
11
use craft\base\Element;
12
use craft\base\ElementInterface;
13
use flipbox\hubspot\connections\ConnectionInterface;
14
use flipbox\hubspot\criteria\ContactListContactsCriteria;
15
use flipbox\hubspot\criteria\ContactListContactsCriteriaInterface;
16
use flipbox\hubspot\fields\Objects;
17
use flipbox\hubspot\helpers\TransformerHelper;
18
use flipbox\hubspot\pipeline\Resource;
19
use flipbox\hubspot\transformers\collections\TransformerCollectionInterface;
20
use Flipbox\Relay\HubSpot\Builder\Resources\ContactList\Contacts\Add;
21
use Flipbox\Relay\HubSpot\Builder\Resources\ContactList\Contacts\All;
22
use Flipbox\Relay\HubSpot\Builder\Resources\ContactList\Contacts\Remove;
23
use League\Pipeline\PipelineBuilderInterface;
24
use Psr\SimpleCache\CacheInterface;
25
use yii\base\Component;
26
27
/**
28
 * @author Flipbox Factory <[email protected]>
29
 * @since 1.0.0
30
 */
31
class ContactListContacts extends Component
32
{
33
    use traits\TransformElementIdTrait;
34
35
    /**
36
     * The HubSpot Resource name
37
     */
38
    const HUBSPOT_RESOURCE = 'contactListContacts';
39
40
    /**
41
     * @inheritdoc
42
     * @return ContactListContactsCriteria
43
     */
44
    public function getCriteria(array $criteria = []): ContactListContactsCriteriaInterface
45
    {
46
        return new ContactListContactsCriteria($criteria);
47
    }
48
49
    /*******************************************
50
     * ADD FROM ELEMENT
51
     *******************************************/
52
53
    /**
54
     * @inheritdoc
55
     * @return PipelineBuilderInterface
56
     */
57
    public function addByElement(
58
        ElementInterface $element,
59
        Objects $field,
60
        ContactListContactsCriteriaInterface $criteria
61
    ): PipelineBuilderInterface {
62
        /** @var Element $element */
63
64
        if (empty($criteria->getId()) || $criteria->getId() === $field::DEFAULT_HUBSPOT_ID) {
65
            $criteria->setId($this->transformElementId($element, $field));
66
        }
67
68
        TransformerHelper::populateTransformerCollection(
69
            $criteria->getTransformer(),
70
            [
71
                'resource' => [get_class($element)]
72
            ]
73
        );
74
75
        return $this->addByCriteria(
76
            $criteria
77
        );
78
    }
79
80
81
    /*******************************************
82
     * GET
83
     *******************************************/
84
85
    /**
86
     * @inheritdoc
87
     * @return Resource
88
     */
89
    public function get(
90
        string $id,
91
        ConnectionInterface $connection,
92
        CacheInterface $cache,
93
        TransformerCollectionInterface $transformer = null
94
    ): PipelineBuilderInterface {
95
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
96
            'resource' => [All::class]
97
        ]);
98
99
        return (new Resource(
100
            (new All(
101
                $id,
102
                $connection,
103
                $cache
104
            ))->build(),
105
            $transformer
106
        ));
107
    }
108
109
    /**
110
     * @inheritdoc
111
     * @return Resource
112
     */
113
    public function getByCriteria(
114
        ContactListContactsCriteriaInterface $criteria
115
    ): PipelineBuilderInterface {
116
        return $this->get(
117
            $criteria->getId(),
118
            $criteria->getConnection(),
119
            $criteria->getCache(),
120
            $criteria->getTransformer()
121
        );
122
    }
123
124
    /**
125
     * @inheritdoc
126
     * @return Resource
127
     */
128
    public function add(
129
        string $id,
130
        array $payload,
131
        ConnectionInterface $connection,
132
        CacheInterface $cache,
133
        TransformerCollectionInterface $transformer = null
134
    ): PipelineBuilderInterface {
135
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
136
            'resource' => [Add::class]
137
        ]);
138
139
        return (new Resource(
140
            (new Add(
141
                $id,
142
                $payload,
143
                $connection,
144
                $cache
145
            ))->build(),
146
            $transformer
147
        ));
148
    }
149
150
    /**
151
     * @inheritdoc
152
     * @return Resource
153
     */
154
    public function addByCriteria(
155
        ContactListContactsCriteriaInterface $criteria
156
    ): PipelineBuilderInterface {
157
        return $this->add(
158
            $criteria->getId(),
159
            $criteria->getPayload(),
160
            $criteria->getConnection(),
161
            $criteria->getCache(),
162
            $criteria->getTransformer()
163
        );
164
    }
165
166
    /**
167
     * @inheritdoc
168
     * @return Resource
169
     */
170
    public function remove(
171
        string $id,
172
        array $payload,
173
        ConnectionInterface $connection,
174
        CacheInterface $cache,
175
        TransformerCollectionInterface $transformer = null
176
    ): PipelineBuilderInterface {
177
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
178
            'resource' => [Remove::class]
179
        ]);
180
181
        return (new Resource(
182
            (new Remove(
183
                $id,
184
                $payload,
185
                $connection,
186
                $cache
187
            ))->build(),
188
            $transformer
189
        ));
190
    }
191
192
    /**
193
     * @inheritdoc
194
     * @return Resource
195
     */
196
    public function removeByCriteria(
197
        ContactListContactsCriteriaInterface $criteria
198
    ): PipelineBuilderInterface {
199
        return $this->add(
200
            $criteria->getId(),
201
            $criteria->getPayload(),
202
            $criteria->getConnection(),
203
            $criteria->getCache(),
204
            $criteria->getTransformer()
205
        );
206
    }
207
}
208