CreateFieldItem::run()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 27
c 0
b 0
f 0
ccs 0
cts 21
cp 0
rs 9.488
cc 3
nc 4
nop 4
crap 12
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-integration/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-integration/
7
 */
8
9
namespace flipbox\craft\integration\actions\fields;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use flipbox\craft\ember\actions\ManageTrait;
14
use flipbox\craft\ember\helpers\SiteHelper;
15
use flipbox\craft\integration\actions\ResolverTrait;
16
use flipbox\craft\integration\fields\Integrations;
17
use flipbox\craft\integration\records\IntegrationAssociation;
18
use yii\base\Action;
19
use yii\web\HttpException;
20
21
/**
22
 * @author Flipbox Factory <[email protected]>
23
 * @since 2.0.0
24
 */
25
class CreateFieldItem extends Action
26
{
27
    use ManageTrait,
28
        ResolverTrait;
29
30
    /**
31
     * @param string $field
32
     * @param string $element
33
     * @param string|null $id
34
     * @param int|null $sortOrder
35
     * @return mixed
36
     * @throws HttpException
37
     * @throws \Twig_Error_Loader
38
     * @throws \yii\base\Exception
39
     * @throws \yii\web\UnauthorizedHttpException
40
     */
41
    public function run(
42
        string $field,
43
        string $element,
44
        string $id = null,
45
        int $sortOrder = null
46
    ) {
47
        $field = $this->resolveField($field);
48
        $element = $this->resolveElement($element);
49
50
        $recordClass = $field::recordClass();
51
52
        /** @var $record IntegrationAssociation */
53
        $record = new $recordClass();
54
        $record->setField($field)
55
            ->setElement($element)
56
            ->setSiteId(SiteHelper::ensureSiteId($element->siteId));
57
58
        if ($id !== null) {
59
            $record->objectId = $id;
60
        }
61
62
        if ($sortOrder !== null) {
63
            $record->sortOrder = $sortOrder;
64
        }
65
66
        return $this->runInternal($field, $element, $record);
67
    }
68
69
    /**
70
     * @param Integrations $field
71
     * @param ElementInterface $element
72
     * @param IntegrationAssociation $record
73
     * @return mixed
74
     * @throws \Twig_Error_Loader
75
     * @throws \yii\base\Exception
76
     * @throws \yii\web\UnauthorizedHttpException
77
     */
78
    protected function runInternal(
79
        Integrations $field,
80
        ElementInterface $element,
81
        IntegrationAssociation $record
82
    ) {
83
84
        // Check access
85
        if (($access = $this->checkAccess($field, $element, $record)) !== true) {
86
            return $access;
87
        }
88
89
        if (null === ($html = $this->performAction($field, $record))) {
90
            return $this->handleFailResponse($html);
91
        }
92
93
        return $this->handleSuccessResponse($html);
94
    }
95
96
    /**
97
     * @param Integrations $field
98
     * @param IntegrationAssociation $record
99
     * @return array
100
     * @throws \Twig_Error_Loader
101
     * @throws \yii\base\Exception
102
     */
103
    public function performAction(
104
        Integrations $field,
105
        IntegrationAssociation $record
106
    ): array {
107
108
109
        $view = Craft::$app->getView();
110
111
        return [
112
            'html' => $view->renderTemplate(
113
                $field::INPUT_ITEM_TEMPLATE_PATH,
114
                [
115
                    'field' => $field,
116
                    'record' => $record,
117
                    'translationCategory' => $field::TRANSLATION_CATEGORY
118
                ]
119
            ),
120
            'headHtml' => $view->getHeadHtml(),
121
            'footHtml' => $view->getBodyHtml()
122
        ];
123
    }
124
}
125