Completed
Push — develop ( f37ae4...1d947e )
by Nate
18:30
created

DynamicModelSuccess   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 5
dl 0
loc 97
ccs 0
cts 58
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 24 4
A populateSource() 0 40 3
A transform() 0 4 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\transformers;
10
11
use craft\base\ElementInterface;
12
use flipbox\hubspot\HubSpot;
13
use flipbox\flux\helpers\TransformerHelper;
14
use Flipbox\Transform\Factory;
15
use Flipbox\Transform\Scope;
16
use Flipbox\Transform\Transformers\AbstractTransformer;
17
use yii\base\DynamicModel;
18
19
class DynamicModelSuccess extends AbstractTransformer
20
{
21
    /**
22
     * The HubSpot resource
23
     *
24
     * @var string
25
     */
26
    public $resource;
27
28
    /**
29
     * @param $data
30
     * @param Scope $scope
31
     * @param string|null $identifier
32
     * @param ElementInterface|null $source
33
     * @param string|null $resource
34
     * @return mixed
35
     */
36
    public function __invoke(
37
        $data,
38
        Scope $scope,
39
        string $identifier = null,
40
        ElementInterface $source = null,
41
        $resource = null
42
    ) {
43
        if (!is_array($data)) {
44
            $data = [$data];
45
        }
46
47
        if (!$source instanceof ElementInterface) {
48
            HubSpot::warning(
49
                "Unable to populate element because an element 'source' does not exist.",
50
                __METHOD__
51
            );
52
53
            return $this->transform($data);
54
        }
55
56
        $this->populateSource($source, $data, $resource ?: $this->resource);
57
58
        return $this->transform($data);
59
    }
60
61
    /**
62
     * @param ElementInterface $element
63
     * @param array $data
64
     * @param string|null $resource
65
     */
66
    protected function populateSource(ElementInterface $element, array $data, $resource = null)
67
    {
68
        $event = ['populate'];
69
70
        if (null !== $resource) {
71
            array_unshift($event, $resource);
72
        }
73
74
        $event = TransformerHelper::eventName($event);
75
        $class = get_class($element);
76
77
        if (null === ($transformer = HubSpot::getInstance()->getTransformers()->find($event, $class))) {
78
            HubSpot::warning(
79
                sprintf(
80
                    "Populate element '%s' transformer could not be found for event '%s'",
81
                    $class,
82
                    $event
83
                ),
84
                __METHOD__
85
            );
86
87
            return;
88
        }
89
90
        HubSpot::info(
91
            sprintf(
92
                "Populate element '%s' with transformer event '%s'",
93
                $class,
94
                $event
95
            ),
96
            __METHOD__
97
        );
98
99
        Factory::item(
100
            $transformer,
101
            $data,
102
            [],
103
            ['source' => $element, 'sObject' => $resource]
104
        );
105
    }
106
107
    /**
108
     * @param array $data
109
     * @return DynamicModel
110
     */
111
    protected function transform(array $data): DynamicModel
112
    {
113
        return new DynamicModel(array_keys($data), $data);
114
    }
115
}
116