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

SyncByElementTrait::syncUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 14
cp 0
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 3
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\fields\Resources;
14
use flipbox\hubspot\pipeline\stages\ElementAssociationStage;
15
use flipbox\hubspot\pipeline\stages\ElementSaveStage;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
trait SyncByElementTrait
22
{
23
    use TransformElementPayloadTrait,
24
        ReadByElementTrait,
25
        UpsertByElementTrait;
26
27
    /**
28
     * @param ElementInterface $element
29
     * @param Resources $field
30
     * @param null $criteria
31
     * @return false|string
32
     */
33
    public function syncDown(
34
        ElementInterface $element,
35
        Resources $field,
36
        $criteria = null
37
    ): bool {
38
        /** @var Element $element */
39
40
        $this->readPipelineByElement(
41
            $element,
42
            $field,
43
            $criteria
44
        )->build()->pipe(
45
            new ElementSaveStage($field)
46
        )->pipe(
47
            new ElementAssociationStage($field)
48
        )(null, $element);
49
50
        return !$element->hasErrors();
51
    }
52
53
    /**
54
     * @param ElementInterface $element
55
     * @param Resources $field
56
     * @param null $criteria
57
     * @return false|string
58
     */
59
    public function syncUp(
60
        ElementInterface $element,
61
        Resources $field,
62
        $criteria = null
63
    ): bool {
64
        /** @var Element $element */
65
66
        $this->upsertPipelineByElement(
67
            $element,
68
            $field,
69
            $criteria
70
        )->build()->pipe(
71
            new ElementAssociationStage($field)
72
        )(null, $element);
73
74
        return !$element->hasErrors();
75
    }
76
}
77