Completed
Push — master ( d5a3eb...e78df8 )
by Nate
10:09 queued 08:05
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
        /** @var string $id */
77
        if (null === ($id = $this->transformElementId($element, $field))) {
78
            return false;
79
        }
80
81
        return $this->rawSyncDown(
82
            $element,
83
            $field,
84
            $id,
85
            $connection,
86
            $cache
87
        );
88
    }
89
90
    /**
91
     * @param ElementInterface $element
92
     * @param Objects $field
93
     * @param string $id
94
     * @param ConnectionInterface|null $connection
95
     * @param CacheInterface|null $cache
96
     * @return bool
97
     * @throws \yii\base\InvalidConfigException
98
     */
99
    public function rawSyncDown(
100
        ElementInterface $element,
101
        Objects $field,
102
        string $id,
103
        ConnectionInterface $connection = null,
104
        CacheInterface $cache = null
105
    ): bool {
106
        /** @var Element $element */
107
108
        (new Resource(
109
            $this->rawHttpReadRelay(
110
                $id,
111
                ConnectionHelper::resolveConnection($connection),
112
                $cache
113
            ),
114
            null,
115
            HubSpot::getInstance()->getPsrLogger()
116
        ))->build()->pipe(
117
            new ElementSaveStage($field)
118
        )->pipe(
119
            new ElementAssociationStage($field)
120
        )(null, $element);
121
122
        return !$element->hasErrors();
123
    }
124
125
    /**
126
     * @param ElementInterface $element
127
     * @param Objects $field
128
     * @param ConnectionInterface|null $connection
129
     * @param CacheInterface|null $cache
130
     * @return bool
131
     * @throws \yii\base\InvalidConfigException
132
     */
133
134
    public function syncUp(
135
        ElementInterface $element,
136
        Objects $field,
137
        ConnectionInterface $connection = null,
138
        CacheInterface $cache = null
139
    ): bool {
140
        return $this->rawSyncUp(
141
            $element,
142
            $field,
143
            $this->transformElementPayload($element, $field),
144
            $this->transformElementId($element, $field),
145
            $connection,
146
            $cache
147
        );
148
    }
149
150
    /**
151
     * @param ElementInterface $element
152
     * @param Objects $field
153
     * @param array $payload
154
     * @param string|null $id
155
     * @param ConnectionInterface|null $connection
156
     * @param CacheInterface|null $cache
157
     * @return bool
158
     * @throws \yii\base\InvalidConfigException
159
     */
160
    public function rawSyncUp(
161
        ElementInterface $element,
162
        Objects $field,
163
        array $payload,
164
        string $id = null,
165
        ConnectionInterface $connection = null,
166
        CacheInterface $cache = null
167
    ): bool {
168
        /** @var Element $element */
169
170
        (new Resource(
171
            $this->rawHttpUpsertRelay(
172
                $payload,
173
                $id,
174
                $connection,
175
                $cache
176
            ),
177
            null,
178
            HubSpot::getInstance()->getPsrLogger()
179
        ))->build()->pipe(
180
            new ElementAssociationStage($field)
181
        )(null, $element);
182
183
        return !$element->hasErrors();
184
    }
185
}
186