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

SyncByElementTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 6
dl 0
loc 56
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A syncDown() 0 19 1
A syncUp() 0 17 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\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