Completed
Push — master ( 1589ee...153d10 )
by Nate
08:23
created

AbstractController::findDefaultConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\craft\hubspot\cp\controllers\view;
10
11
use Craft;
12
use craft\web\Controller;
13
use flipbox\craft\ember\helpers\UrlHelper;
14
use flipbox\craft\hubspot\cp\Cp as CpModule;
15
use flipbox\craft\hubspot\HubSpot;
16
use flipbox\craft\hubspot\records\Connection;
17
use yii\base\DynamicModel;
18
19
/**
20
 * @author Flipbox Factory <[email protected]>
21
 * @since 1.0.0
22
 *
23
 * @property CpModule $module
24
 */
25
abstract class AbstractController extends Controller
26
{
27
    /**
28
     * The index view template path
29
     */
30
    const TEMPLATE_BASE = 'hubspot' . '/_cp';
31
32
    /*******************************************
33
     * BASE PATHS
34
     *******************************************/
35
36
    /**
37
     * @return string
38
     */
39
    protected function getBaseActionPath(): string
40
    {
41
        return HubSpot::getInstance()->getUniqueId() . '/cp';
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    protected function getBaseCpPath(): string
48
    {
49
        return HubSpot::getInstance()->getUniqueId();
50
    }
51
52
    /**
53
     * @param string $endpoint
54
     * @return string
55
     */
56
    protected function getBaseContinueEditingUrl(string $endpoint = ''): string
57
    {
58
        return $this->getBaseCpPath() . $endpoint;
59
    }
60
61
    /*******************************************
62
     * CONNECTIONS
63
     *******************************************/
64
65
    /**
66
     * @return array
67
     */
68
    protected function getConnections(): array
69
    {
70
        return Connection::findAll(['enabled' => true]);
71
    }
72
73
    /**
74
     * @return Connection|null
75
     */
76
    protected function findDefaultConnection()
77
    {
78
        return Connection::findOne([
79
            'enabled' => true,
80
            'handle' => HubSpot::getInstance()->getSettings()->getDefaultConnection()
81
        ]);
82
    }
83
84
    /**
85
     * @return Connection|null
86
     */
87
    protected function findActiveConnection()
88
    {
89
        $selectedConnection = Craft::$app->getRequest()->getParam(
90
            'connection',
91
            HubSpot::getInstance()->getSettings()->getDefaultConnection()
92
        );
93
94
        $connection = Connection::findOne([
95
            'enabled' => true,
96
            'handle' => $selectedConnection
97
        ]) ?: $this->findDefaultConnection();
98
99
        if ($connection === null) {
100
            $connections = $this->getConnections();
101
102
            if(count($connections) === 1) {
103
                return reset($connections);
104
            }
105
        }
106
107
        return $connection;
108
    }
109
110
    /**
111
     * @return DynamicModel
112
     */
113
    protected function invalidConnectionModel(): DynamicModel
114
    {
115
        $model = new DynamicModel();
116
        $model->addError(
117
            'connection',
118
            'Invalid connection. ' .
119
            '<a href="' . UrlHelper::cpUrl('hubspot/settings/connections') . '">' .
120
            'Manage connections to HubSpot' .
121
            '</a>.'
122
        );
123
124
        return $model;
125
    }
126
127
    /*******************************************
128
     * VARIABLES
129
     *******************************************/
130
131
    /**
132
     * @inheritdoc
133
     */
134
    protected function baseVariables(array &$variables = [])
135
    {
136
        $module = HubSpot::getInstance();
137
138
        $title = HubSpot::t("HubSpot");
139
140
        // Settings
141
        $variables['settings'] = $module->getSettings();
142
        $variables['title'] = $title;
143
144
        // Connections
145
        $variables['availableConnections'] = $this->getConnections();
146
        $variables['defaultConnection'] = $this->findDefaultConnection();
147
        $variables['activeConnection'] = $this->findActiveConnection();
148
149
        // Path to controller actions
150
        $variables['baseActionPath'] = $this->getBaseActionPath();
151
152
        // Path to CP
153
        $variables['baseCpPath'] = $this->getBaseCpPath();
154
155
        // Set the "Continue Editing" URL
156
        $variables['continueEditingUrl'] = $this->getBaseCpPath();
157
158
        // Select our sub-nav
159
        if (!$activeSubNav = Craft::$app->getRequest()->getSegment(2)) {
160
            $activeSubNav = 'queries';
161
        }
162
        $variables['selectedSubnavItem'] = 'hubspot.' . $activeSubNav;
163
164
        // Breadcrumbs
165
        $variables['crumbs'][] = [
166
            'label' => $title,
167
            'url' => UrlHelper::url(HubSpot::getInstance()->getUniqueId())
168
        ];
169
    }
170
171
    /*******************************************
172
     * UPSERT VARIABLES
173
     *******************************************/
174
175
    /**
176
     * @param array $variables
177
     */
178
    protected function insertVariables(array &$variables)
179
    {
180
        // apply base view variables
181
        $this->baseVariables($variables);
182
183
        // Set the "Continue Editing" URL
184
        $variables['continueEditingUrl'] = $this->getBaseContinueEditingUrl('/{id}');
185
186
        // Append title
187
        $variables['title'] .= ' - ' . HubSpot::t('New');
188
    }
189
}
190