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

UpsertByElementTrait::upsertPipelineByElement()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 22
cp 0
rs 9.488
c 0
b 0
f 0
cc 4
nc 4
nop 3
crap 20
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\criteria\ResourceCriteriaInterface;
14
use flipbox\hubspot\fields\Resources;
15
use flipbox\hubspot\helpers\TransformerHelper;
16
use League\Pipeline\PipelineBuilderInterface;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
trait UpsertByElementTrait
23
{
24
    /**
25
     * @param ResourceCriteriaInterface $criteria
26
     * @return PipelineBuilderInterface
27
     */
28
    public abstract function upsertPipelineByCriteria(ResourceCriteriaInterface $criteria): PipelineBuilderInterface;
29
30
    /**
31
     * @param ElementInterface $element
32
     * @param Resources $field
33
     * @return string|null
34
     */
35
    protected abstract function transformElementId(ElementInterface $element, Resources $field);
36
37
    /**
38
     * @param Resources $field
39
     * @param null $criteria
40
     * @return ResourceCriteriaInterface
41
     */
42
    protected abstract function resolveCriteria(Resources $field, $criteria = null): ResourceCriteriaInterface;
43
44
    /**
45
     * @inheritdoc
46
     * @return PipelineBuilderInterface
47
     */
48
    public function upsertByElement(
49
        ElementInterface $element,
50
        Resources $field,
51
        $criteria = null
52
    ): PipelineBuilderInterface {
53
        return $this->upsertPipelineByElement(
54
            $element,
55
            $field,
56
            $criteria
57
        )($element);
58
    }
59
60
    /**
61
     * @inheritdoc
62
     * @return PipelineBuilderInterface
63
     */
64
    public function upsertPipelineByElement(
65
        ElementInterface $element,
66
        Resources $field,
67
        $criteria = null
68
    ): PipelineBuilderInterface {
69
        /** @var Element $element */
70
        $criteria = $this->resolveCriteria($field, $criteria);
71
72
        if (empty($criteria->getId()) || $criteria->getId() === $field::DEFAULT_HUBSPOT_ID) {
73
            $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...
74
        }
75
76
        if (empty($criteria->getPayload())) {
77
            $criteria->payload = $this->transformElementPayload($element, $field);
0 ignored issues
show
Bug introduced by
Accessing payload 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...
Bug introduced by
It seems like transformElementPayload() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
78
        }
79
80
        TransformerHelper::populateTransformerCollection(
81
            $criteria->getTransformer(),
82
            [
83
                'resource' => [get_class($element)]
84
            ]
85
        );
86
87
        return $this->upsertPipelineByCriteria(
88
            $criteria
89
        );
90
    }
91
}
92