ObjectsController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 9
dl 0
loc 152
ccs 0
cts 88
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A actionIndex() 0 34 3
A getObjectOptions() 0 38 4
A getTabs() 0 13 1
A getBaseCpPath() 0 4 1
A getBaseActionPath() 0 4 1
A baseVariables() 0 13 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\craft\salesforce\cp\controllers\view;
10
11
use Craft;
12
use craft\helpers\UrlHelper;
13
use flipbox\craft\ember\helpers\ArrayHelper;
14
use flipbox\craft\salesforce\criteria\InstanceCriteria;
15
use flipbox\craft\salesforce\criteria\ObjectCriteria;
16
use flipbox\craft\salesforce\Force;
17
use flipbox\craft\salesforce\transformers\DynamicModelResponse;
18
use yii\base\DynamicModel;
19
use yii\base\UnknownPropertyException;
20
use yii\web\Response;
21
22
/**
23
 * @author Flipbox Factory <[email protected]>
24
 * @since 1.0.0
25
 */
26
class ObjectsController 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 . '/index';
37
38
    /**
39
     * @return Response
40
     * @throws \Exception
41
     */
42
    public function actionIndex(): Response
43
    {
44
        $variables = [];
45
        $this->baseVariables($variables);
46
47
        $variables['describedObject'] = null;
48
49
        if ($object = Craft::$app->getRequest()->getParam('object')) {
50
            $criteria = new ObjectCriteria([
51
                'object' => $object
52
            ]);
53
54
            if (null !== ($connection = $this->findActiveConnection())) {
55
                $criteria->setConnection($connection->handle);
56
57
                $model = call_user_func_array(
58
                    new DynamicModelResponse(),
59
                    [
60
                        $criteria->describe()
61
                    ]
62
                );
63
            }
64
65
            $variables['describedObject'] = $model ?? $this->invalidConnectionModel();
66
        }
67
68
        $variables['objectOptions'] = $this->getObjectOptions();
69
        $variables['tabs'] = $this->getTabs();
70
71
        return $this->renderTemplate(
72
            static::TEMPLATE_INDEX,
73
            $variables
74
        );
75
    }
76
77
    /**
78
     * @return array
79
     */
80
    private function getObjectOptions()
81
    {
82
        $describeOptions = [];
83
84
        try {
85
            if (null !== ($connection = $this->findActiveConnection())) {
86
                $criteria = new InstanceCriteria([
87
                    'connection' => $connection->handle
88
                ]);
89
90
                /** @var DynamicModel $model */
91
                $model = call_user_func_array(
92
                    new DynamicModelResponse(),
93
                    [
94
                        $criteria->describe()
95
                    ]
96
                );
97
98
                foreach (ArrayHelper::getValue($model, 'sobjects', []) as $object) {
99
                    $describeOptions[] = [
100
                        'label' => $object['label'],
101
                        'value' => $object['name']
102
                    ];
103
                }
104
            }
105
106
            // Sort them by name
107
            ArrayHelper::multisort($describeOptions, 'label');
108
        } catch (UnknownPropertyException $e) {
109
            // intentionally doing nothing
110
        }
111
112
        return [
113
                [
114
                    'label' => 'Select Salesforce Object'
115
                ]
116
            ] + $describeOptions;
117
    }
118
119
    /**
120
     * @return array
121
     */
122
    private function getTabs(): array
123
    {
124
        return [
125
            'fields' => [
126
                'label' => Force::t('Fields'),
127
                'url' => '#fields'
128
            ],
129
            'relations' => [
130
                'label' => Force::t('Relationships'),
131
                'url' => '#relations'
132
            ]
133
        ];
134
    }
135
136
    /*******************************************
137
     * BASE PATHS
138
     *******************************************/
139
140
    /**
141
     * @return string
142
     */
143
    protected function getBaseCpPath(): string
144
    {
145
        return parent::getBaseCpPath() . '/objects';
146
    }
147
148
    /**
149
     * @return string
150
     */
151
    protected function getBaseActionPath(): string
152
    {
153
        return parent::getBaseActionPath() . '/objects';
154
    }
155
156
157
    /*******************************************
158
     * VARIABLES
159
     *******************************************/
160
161
    /**
162
     * @inheritdoc
163
     */
164
    protected function baseVariables(array &$variables = [])
165
    {
166
        parent::baseVariables($variables);
167
168
        $title = Force::t("Objects");
169
        $variables['title'] .= ' ' . $title;
170
171
        // Breadcrumbs
172
        $variables['crumbs'][] = [
173
            'label' => $title,
174
            'url' => UrlHelper::url($this->getBaseCpPath())
175
        ];
176
    }
177
}
178