Completed
Push — develop ( 2d4f17...d5975b )
by Nate
05:25
created

CreateUpsertPayloadFromElement::__invoke()   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 19
cp 0
rs 9.52
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
/**
4
 * @noinspection PhpUnusedParameterInspection
5
 *
6
 * @copyright  Copyright (c) Flipbox Digital Limited
7
 * @license    https://github.com/flipbox/hubspot/blob/master/LICENSE.md
8
 * @link       https://github.com/flipbox/hubspot
9
 */
10
11
namespace flipbox\craft\hubspot\transformers;
12
13
use craft\base\Element;
14
use craft\base\ElementInterface;
15
use flipbox\craft\hubspot\events\CreatePayloadFromElementEvent;
16
use flipbox\craft\hubspot\fields\Objects;
17
use flipbox\craft\hubspot\fields\ObjectsFieldInterface;
18
use flipbox\craft\hubspot\HubSpot;
19
use yii\base\BaseObject;
20
21
/**
22
 * @author Flipbox Factory <[email protected]>
23
 * @since 1.0.0
24
 */
25
class CreateUpsertPayloadFromElement extends BaseObject
26
{
27
    /**
28
     * An action used to assemble a unique event name.
29
     *
30
     * @var string
31
     */
32
    public $action;
33
34
    /**
35
     * @param ElementInterface|Element $element
36
     * @param ObjectsFieldInterface $field
37
     * @return array
38
     */
39
    public function __invoke(
40
        ElementInterface $element,
41
        ObjectsFieldInterface $field
42
    ): array {
43
        /** @var Objects $field */
44
45
        $event = new CreatePayloadFromElementEvent([
46
            'payload' => $this->createPayload($element, $field)
47
        ]);
48
49
        $name = $event::eventName(
50
            $field->handle,
51
            $this->action
52
        );
53
54
        HubSpot::info(sprintf(
55
            "Create payload: Event '%s', Element '%s'",
56
            $name,
57
            $element->id . ' - ' . $element->title
0 ignored issues
show
Bug introduced by
Accessing title on the interface craft\base\ElementInterface 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...
58
        ), __METHOD__);
59
60
        $element->trigger($name, $event);
61
62
        return $event->getPayload();
63
    }
64
65
    /**
66
     * @param ElementInterface $element
67
     * @param ObjectsFieldInterface $field
68
     * @return array
69
     *
70
     * @noinspection PhpUnusedParameterInspection
71
     */
72
    public function createPayload(
73
        ElementInterface $element,
1 ignored issue
show
Unused Code introduced by
The parameter $element is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74
        ObjectsFieldInterface $field
1 ignored issue
show
Unused Code introduced by
The parameter $field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    ): array {
76
        /** @var Element $element */
77
78
        return [];
79
    }
80
}
81