Completed
Push — develop ( d5a3eb...d52d90 )
by Nate
12:14
created

SyncByElementTrait::rawSyncUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 13
cp 0
rs 9.52
c 0
b 0
f 0
cc 1
nc 1
nop 6
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\traits;
10
11
use craft\base\Element;
12
use craft\base\ElementInterface;
13
use flipbox\hubspot\connections\ConnectionInterface;
14
use flipbox\hubspot\fields\Objects;
15
use flipbox\hubspot\helpers\ConnectionHelper;
16
use flipbox\hubspot\HubSpot;
17
use flipbox\hubspot\pipeline\Resource;
18
use flipbox\hubspot\pipeline\stages\ElementAssociationStage;
19
use flipbox\hubspot\pipeline\stages\ElementSaveStage;
20
use flipbox\hubspot\traits\TransformElementIdTrait;
21
use flipbox\hubspot\traits\TransformElementPayloadTrait;
22
use Psr\SimpleCache\CacheInterface;
23
24
/**
25
 * @author Flipbox Factory <[email protected]>
26
 * @since 1.0.0
27
 */
28
trait SyncByElementTrait
29
{
30
    use TransformElementIdTrait,
31
        TransformElementPayloadTrait;
32
33
    /**
34
     * @param string $id
35
     * @param ConnectionInterface|string|null $connection
36
     * @param CacheInterface|string|null $cache
37
     * @return callable
38
     */
39
    public abstract function rawHttpReadRelay(
40
        string $id,
41
        ConnectionInterface $connection = null,
42
        CacheInterface $cache = null
43
    ): callable;
44
45
    /**
46
     * @param array $payload
47
     * @param string|null $identifier
48
     * @param ConnectionInterface|string|null $connection
49
     * @param CacheInterface|string|null $cache
50
     * @return callable
51
     * @throws \yii\base\InvalidConfigException
52
     */
53
    public abstract function rawHttpUpsertRelay(
54
        array $payload,
55
        string $identifier = null,
56
        ConnectionInterface $connection = null,
57
        CacheInterface $cache = null
58
    ): callable;
59
60
    /**
61
     * @param ElementInterface $element
62
     * @param Objects $field
63
     * @param ConnectionInterface|null $connection
64
     * @param CacheInterface|null $cache
65
     * @return bool
66
     * @throws \yii\base\InvalidConfigException
67
     */
68
    public function syncDown(
69
        ElementInterface $element,
70
        Objects $field,
71
        ConnectionInterface $connection = null,
72
        CacheInterface $cache = null
73
    ): bool {
74
        /** @var Element $element */
75
76
        if (null === ($id = $this->transformElementId($element, $field))) {
77
            return false;
78
        }
79
80
        return $this->rawSyncDown(
81
            $element,
82
            $field,
83
            $id,
1 ignored issue
show
Documentation introduced by
$id is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
84
            $connection,
85
            $cache
86
        );
87
    }
88
89
    /**
90
     * @param ElementInterface $element
91
     * @param Objects $field
92
     * @param string $id
93
     * @param ConnectionInterface|null $connection
94
     * @param CacheInterface|null $cache
95
     * @return bool
96
     * @throws \yii\base\InvalidConfigException
97
     */
98
    public function rawSyncDown(
99
        ElementInterface $element,
100
        Objects $field,
101
        string $id,
102
        ConnectionInterface $connection = null,
103
        CacheInterface $cache = null
104
    ): bool {
105
        /** @var Element $element */
106
107
        (new Resource(
108
            $this->rawHttpReadRelay(
109
                $id,
110
                ConnectionHelper::resolveConnection($connection),
111
                $cache
112
            ),
113
            null,
114
            HubSpot::getInstance()->getPsrLogger()
115
        ))->build()->pipe(
116
            new ElementSaveStage($field)
117
        )->pipe(
118
            new ElementAssociationStage($field)
119
        )(null, $element);
120
121
        return !$element->hasErrors();
122
    }
123
124
    /**
125
     * @param ElementInterface $element
126
     * @param Objects $field
127
     * @param ConnectionInterface|null $connection
128
     * @param CacheInterface|null $cache
129
     * @return bool
130
     * @throws \yii\base\InvalidConfigException
131
     */
132
133
    public function syncUp(
134
        ElementInterface $element,
135
        Objects $field,
136
        ConnectionInterface $connection = null,
137
        CacheInterface $cache = null
138
    ): bool {
139
        return $this->rawSyncUp(
140
            $element,
141
            $field,
142
            $this->transformElementPayload($element, $field),
143
            $this->transformElementId($element, $field),
1 ignored issue
show
Bug introduced by
It seems like $this->transformElementId($element, $field) targeting flipbox\hubspot\traits\T...t::transformElementId() can also be of type array; however, flipbox\hubspot\services...ementTrait::rawSyncUp() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
144
            $connection,
145
            $cache
146
        );
147
    }
148
149
    /**
150
     * @param ElementInterface $element
151
     * @param Objects $field
152
     * @param array $payload
153
     * @param string|null $id
154
     * @param ConnectionInterface|null $connection
155
     * @param CacheInterface|null $cache
156
     * @return bool
157
     * @throws \yii\base\InvalidConfigException
158
     */
159
    public function rawSyncUp(
160
        ElementInterface $element,
161
        Objects $field,
162
        array $payload,
163
        string $id = null,
164
        ConnectionInterface $connection = null,
165
        CacheInterface $cache = null
166
    ): bool {
167
        /** @var Element $element */
168
169
        (new Resource(
170
            $this->rawHttpUpsertRelay(
171
                $payload,
172
                $id,
173
                $connection,
174
                $cache
175
            ),
176
            null,
177
            HubSpot::getInstance()->getPsrLogger()
178
        ))->build()->pipe(
179
            new ElementAssociationStage($field)
180
        )(null, $element);
181
182
        return !$element->hasErrors();
183
    }
184
}
185