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

CreateItem   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 81
ccs 0
cts 42
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 13 1
A runInternal() 0 16 3
A performAction() 0 20 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/force/license
6
 * @link       https://www.flipboxfactory.com/software/force/
7
 */
8
9
namespace flipbox\hubspot\cp\actions\fields;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use flipbox\hubspot\fields\Resources;
14
use flipbox\hubspot\HubSpot;
15
use flipbox\hubspot\records\ResourceAssociation;
16
use flipbox\ember\actions\traits\Manage;
17
use yii\base\Action;
18
use yii\web\HttpException;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 */
24
class CreateItem extends Action
25
{
26
    use traits\ElementResolverTrait,
27
        traits\FieldResolverTrait,
28
        Manage;
29
30
    /**
31
     * @param string $field
32
     * @param string $element
33
     * @param string|null $id
34
     * @return mixed
35
     * @throws HttpException
36
     * @throws \yii\base\Exception
37
     * @throws \yii\web\UnauthorizedHttpException
38
     */
39
    public function run(string $field, string $element, string $id = null)
40
    {
41
        $field = $this->resolveField($field);
42
        $element = $this->resolveElement($element);
43
44
        $record = HubSpot::getInstance()->getResourceAssociations()->create([
45
            'hubSpotId' => $id,
46
            'elementId' => $element->getId(),
47
            'siteId' => $element->siteId,
48
        ]);
49
50
        return $this->runInternal($field, $element, $record);
51
    }
52
53
    /**
54
     * @param Resources $field
55
     * @param ElementInterface $element
56
     * @param ResourceAssociation $record
57
     * @return mixed
58
     * @throws \yii\base\Exception
59
     * @throws \yii\web\UnauthorizedHttpException
60
     */
61
    protected function runInternal(
62
        Resources $field,
63
        ElementInterface $element,
64
        ResourceAssociation $record
65
    ) {
66
        // Check access
67
        if (($access = $this->checkAccess($field, $element, $record)) !== true) {
68
            return $access;
69
        }
70
71
        if (null === ($html = $this->performAction($field, $record))) {
72
            return $this->handleFailResponse($html);
73
        }
74
75
        return $this->handleSuccessResponse($html);
76
    }
77
78
    /**
79
     * @param Resources $field
80
     * @param ResourceAssociation $record
81
     * @return array
82
     * @throws \yii\base\Exception
83
     */
84
    public function performAction(
85
        Resources $field,
86
        ResourceAssociation $record
87
    ): array {
88
89
        $view = Craft::$app->getView();
90
91
        return [
92
            'html' => $view->renderTemplateMacro(
93
                'hubspot/_components/fieldtypes/Resources/input',
94
                'createItem',
95
                [
96
                    'field' => $field,
97
                    'record' => $record
98
                ]
99
            ),
100
            'headHtml' => $view->getHeadHtml(),
101
            'footHtml' => $view->getBodyHtml()
102
        ];
103
    }
104
}
105