Completed
Push — master ( 862cc7...24e4b3 )
by Nate
03:30
created

ObjectPayloadsController::baseVariables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 10
cp 0
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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\craft\salesforce\cp\controllers\view;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use craft\helpers\StringHelper;
14
use flipbox\craft\hubspot\fields\ObjectsFieldInterface;
15
use flipbox\craft\salesforce\fields\Objects;
16
use flipbox\craft\salesforce\Force;
17
use flipbox\craft\salesforce\helpers\TransformerHelper;
18
use flipbox\craft\salesforce\transformers\CreateUpsertPayloadFromElement;
19
use yii\web\HttpException;
20
use yii\web\Response;
21
22
/**
23
 * @author Flipbox Factory <[email protected]>
24
 * @since 1.0.0
25
 */
26
class ObjectPayloadsController extends AbstractController
27
{
28
    /**
29
     * The template base path
30
     */
31
    const TEMPLATE_BASE = parent::TEMPLATE_BASE . '/objects';
32
33
    /**
34
     * The index view template path
35
     */
36
    const TEMPLATE_INDEX = self::TEMPLATE_BASE . '/payload';
37
38
    /**
39
     * @param int $field
40
     * @param int $element
41
     * @return Response
42
     * @throws HttpException
43
     */
44
    public function actionIndex(int $field, int $element): Response
45
    {
46
        $variables = [];
47
        $this->baseVariables($variables);
48
49
        /** @var Objects $field */
50
        $field = Craft::$app->getFields()->getFieldById($field);
51
        if (!$field instanceof ObjectsFieldInterface) {
0 ignored issues
show
Bug introduced by
The class flipbox\craft\hubspot\fields\ObjectsFieldInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
52
            throw new HttpException(401, "Invalid field.");
53
        }
54
        $variables['field'] = $field;
55
56
        $element = Craft::$app->getElements()->getElementById($element);
57
        if (!$element instanceof ElementInterface) {
58
            throw new HttpException(401, "Invalid element.");
59
        }
60
        $variables['element'] = $element;
61
62
        $variables['tabs'] = $this->getTabs();
63
64
        $payloads = [];
65
66
        foreach ($this->getPayloadTypes() as $type) {
67
            $payloads[$type] = $this->getPayload(
68
                $field,
69
                $element,
70
                $type
71
            );
72
        }
73
74
        $variables['payloads'] = $payloads;
75
76
        return $this->renderTemplate(
77
            static::TEMPLATE_INDEX,
78
            $variables
79
        );
80
    }
81
82
    /**
83
     * @return array
84
     */
85
    private function getPayloadTypes(): array
86
    {
87
        return [
88
            TransformerHelper::PAYLOAD_ACTION_SYNC,
89
            TransformerHelper::PAYLOAD_ACTION_SAVE
90
        ];
91
    }
92
93
    /**
94
     * @param Objects $field
95
     * @param ElementInterface $element
96
     * @param string $action
97
     * @return array
98
     */
99
    protected function getPayload(
100
        Objects $field,
101
        ElementInterface $element,
102
        string $action
103
    ): array {
104
105
        $transformer = [
106
            'class' => CreateUpsertPayloadFromElement::class,
107
            'action' => $action
108
        ];
109
110
        // Get callable used to create payload
111
        if (null === ($transformer = TransformerHelper::resolveTransformer($transformer))) {
112
            return [];
113
        }
114
115
        // Create payload
116
        $payload = call_user_func_array(
117
            $transformer,
118
            [
119
                $element,
120
                $field
121
            ]
122
        );
123
124
        return $payload;
125
    }
126
127
128
    /**
129
     * @return array
130
     */
131
    private function getTabs(): array
132
    {
133
        $return = [];
134
135
        foreach ($this->getPayloadTypes() as $type) {
136
            $return[$type] = [
137
                'label' => Force::t(StringHelper::toTitleCase($type)),
138
                'url' => '#' . $type
139
            ];
140
        }
141
142
        return $return;
143
    }
144
145
    /*******************************************
146
     * BASE PATHS
147
     *******************************************/
148
149
    /**
150
     * @return string
151
     */
152
    protected function getBaseCpPath(): string
153
    {
154
        return parent::getBaseCpPath() . '/object-fields';
155
    }
156
157
    /**
158
     * @return string
159
     */
160
    protected function getBaseActionPath(): string
161
    {
162
        return parent::getBaseActionPath() . '/object-fields';
163
    }
164
165
166
    /*******************************************
167
     * VARIABLES
168
     *******************************************/
169
170
    /**
171
     * @inheritdoc
172
     */
173
    protected function baseVariables(array &$variables = [])
174
    {
175
        parent::baseVariables($variables);
176
177
        $title = Force::t("Object Payload");
178
        $variables['title'] .= ' ' . $title;
179
180
        // Breadcrumbs
181
        $variables['crumbs'][] = [
182
            'label' => $title,
183
            'url' => ''
184
        ];
185
    }
186
}
187