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

ContactListContacts::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 20
cp 0
rs 9.584
c 0
b 0
f 0
cc 1
nc 1
nop 5
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\services\resources;
10
11
use craft\base\Element;
12
use craft\base\ElementInterface;
13
use flipbox\hubspot\connections\ConnectionInterface;
14
use flipbox\hubspot\criteria\ResourceCriteriaInterface;
15
use flipbox\hubspot\criteria\ContactListContactsCriteria;
16
use flipbox\hubspot\fields\Resources;
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\AddContacts;
21
use Flipbox\Relay\HubSpot\Builder\Resources\ContactList\RemoveContacts;
22
use Flipbox\Relay\HubSpot\Builder\Resources\ContactList\GetContacts;
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 = []): ResourceCriteriaInterface
45
    {
46
        return new ContactListContactsCriteria($criteria);
47
    }
48
49
    /*******************************************
50
     * HUBSPOT ID
51
     *******************************************/
52
53
    /**
54
     * @param ElementInterface $element
55
     * @param Resources $field
56
     * @return string|null
57
     */
58
    public function findHubSpotIdByElement(ElementInterface $element, Resources $field)
59
    {
60
        return $this->transformElementId($element, $field);
61
    }
62
63
64
    /*******************************************
65
     * ADD FROM ELEMENT
66
     *******************************************/
67
68
    /**
69
     * @inheritdoc
70
     * @return PipelineBuilderInterface
71
     */
72
    public function addFromElement(
73
        ElementInterface $element,
74
        Resources $field,
75
        ResourceCriteriaInterface $criteria
76
    ): PipelineBuilderInterface {
77
        /** @var Element $element */
78
79
        if (empty($criteria->getId()) || $criteria->getId() === $field::DEFAULT_HUBSPOT_ID) {
80
            $criteria->id = $this->transformElementId($element, $field);
0 ignored issues
show
Bug introduced by
Accessing id on the interface flipbox\hubspot\criteria\ResourceCriteriaInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
81
        }
82
83
        TransformerHelper::populateTransformerCollection(
84
            $criteria->getTransformer(),
85
            [
86
                'resource' => [get_class($element)]
87
            ]
88
        );
89
90
        return $this->addFromCriteria(
91
            $criteria
92
        );
93
    }
94
95
96
    /*******************************************
97
     * GET
98
     *******************************************/
99
100
    /**
101
     * @inheritdoc
102
     * @return Resource
103
     */
104
    public function get(
105
        string $id,
106
        ConnectionInterface $connection,
107
        CacheInterface $cache,
108
        TransformerCollectionInterface $transformer = null
109
    ): PipelineBuilderInterface {
110
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
111
            'resource' => [GetContacts::class]
112
        ]);
113
114
        return (new Resource(
115
            (new GetContacts(
116
                $id,
117
                $connection,
118
                $cache
119
            ))->build(),
120
            $transformer
121
        ));
122
    }
123
124
    /**
125
     * @inheritdoc
126
     * @return Resource
127
     */
128
    public function getFromCriteria(
129
        ResourceCriteriaInterface $criteria
130
    ): PipelineBuilderInterface {
131
        return $this->get(
132
            $criteria->getId(),
133
            $criteria->getConnection(),
134
            $criteria->getCache(),
135
            $criteria->getTransformer()
136
        );
137
    }
138
139
    /**
140
     * @inheritdoc
141
     * @return Resource
142
     */
143
    public function add(
144
        string $id,
145
        array $payload,
146
        ConnectionInterface $connection,
147
        CacheInterface $cache,
148
        TransformerCollectionInterface $transformer = null
149
    ): PipelineBuilderInterface {
150
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
151
            'resource' => [AddContacts::class]
152
        ]);
153
154
        return (new Resource(
155
            (new AddContacts(
156
                $id,
157
                $payload,
158
                $connection,
159
                $cache
160
            ))->build(),
161
            $transformer
162
        ));
163
    }
164
165
    /**
166
     * @inheritdoc
167
     * @return Resource
168
     */
169
    public function addFromCriteria(
170
        ResourceCriteriaInterface $criteria
171
    ): PipelineBuilderInterface {
172
        return $this->add(
173
            $criteria->getId(),
174
            $criteria->getPayload(),
175
            $criteria->getConnection(),
176
            $criteria->getCache(),
177
            $criteria->getTransformer()
178
        );
179
    }
180
181
    /**
182
     * @inheritdoc
183
     * @return Resource
184
     */
185
    public function remove(
186
        string $id,
187
        array $payload,
188
        ConnectionInterface $connection,
189
        CacheInterface $cache,
190
        TransformerCollectionInterface $transformer = null
191
    ): PipelineBuilderInterface {
192
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
193
            'resource' => [RemoveContacts::class]
194
        ]);
195
196
        return (new Resource(
197
            (new RemoveContacts(
198
                $id,
199
                $payload,
200
                $connection,
201
                $cache
202
            ))->build(),
203
            $transformer
204
        ));
205
    }
206
207
    /**
208
     * @inheritdoc
209
     * @return Resource
210
     */
211
    public function removeFromCriteria(
212
        ResourceCriteriaInterface $criteria
213
    ): PipelineBuilderInterface {
214
        return $this->add(
215
            $criteria->getId(),
216
            $criteria->getPayload(),
217
            $criteria->getConnection(),
218
            $criteria->getCache(),
219
            $criteria->getTransformer()
220
        );
221
    }
222
}
223