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

ReadByElementTrait::readPipelineByElement()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 17
cp 0
rs 9.584
c 0
b 0
f 0
cc 3
nc 2
nop 3
crap 12
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 ReadByElementTrait
23
{
24
    /**
25
     * @param ResourceCriteriaInterface $criteria
26
     * @return PipelineBuilderInterface
27
     */
28
    public abstract function readPipelineByCriteria(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 mixed
47
     */
48
    public function readByElement(
49
        ElementInterface $element,
50
        Resources $field,
51
        $criteria = null
52
    ) {
53
        return $this->readPipelineByElement(
54
            $element,
55
            $field,
56
            $criteria
57
        )($element);
58
    }
59
60
    /**
61
     * @inheritdoc
62
     * @return PipelineBuilderInterface
63
     */
64
    public function readPipelineByElement(
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
        TransformerHelper::populateTransformerCollection(
77
            $criteria->getTransformer(),
78
            [
79
                'resource' => [get_class($element)]
80
            ]
81
        );
82
83
        return $this->readPipelineByCriteria($criteria);
84
    }
85
}
86