Completed
Push — develop ( 2d4f17...d5975b )
by Nate
05:25
created

ConnectionsController::getBaseCpPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
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\settings\view;
10
11
use Craft;
12
use craft\helpers\UrlHelper;
13
use flipbox\craft\hubspot\connections\SavableConnectionInterface;
14
use flipbox\craft\hubspot\HubSpot;
15
use flipbox\craft\hubspot\records\Connection;
16
use yii\di\Instance;
17
use yii\web\Response;
18
19
/**
20
 * @author Flipbox Factory <[email protected]>
21
 * @since 1.0.0
22
 */
23
class ConnectionsController extends AbstractController
24
{
25
    /**
26
     * The template base path
27
     */
28
    const TEMPLATE_BASE = parent::TEMPLATE_BASE . '/connections';
29
30
    /**
31
     * The index view template path
32
     */
33
    const TEMPLATE_INDEX = self::TEMPLATE_BASE . '/index';
34
35
    /**
36
     * The index view template path
37
     */
38
    const TEMPLATE_UPSERT = self::TEMPLATE_BASE . '/upsert';
39
40
    /**
41
     * @return Response
42
     * @throws \yii\base\InvalidConfigException
43
     */
44
    public function actionIndex(): Response
45
    {
46
        $variables = [];
47
        $this->baseVariables($variables);
48
49
        $variables['connections'] = Connection::find()->all();
50
        $variables['types'] = $this->getAvailableConnections();
51
52
        return $this->renderTemplate(
53
            static::TEMPLATE_INDEX,
54
            $variables
55
        );
56
    }
57
58
    /**
59
     * @param null $identifier
60
     * @param Connection|null $connection
61
     * @return Response
62
     * @throws \craft\errors\MissingComponentException
63
     * @throws \yii\base\InvalidConfigException
64
     */
65
    public function actionUpsert($identifier = null, Connection $connection = null): Response
66
    {
67
        if (null === $connection) {
68
            if (null !== $identifier) {
69
                $connection = Connection::findOne($identifier);
70
            } else {
71
                $connection = new Connection;
72
            }
73
        }
74
75
        // Bad connection
76
        if (null === $connection) {
77
            $message = HubSpot::t(
78
                "Connection '{$identifier}' is not configurable",
79
                [
80
                    'identifier' => $identifier
81
                ]
82
            );
83
            Craft::$app->getSession()->setError($message);
84
            $this->redirect($this->getBaseCpPath());
85
        }
86
87
        $variables = [];
88
        if ($connection === null || $connection->getIsNewRecord()) {
89
            $this->insertVariables($variables);
90
        } else {
91
            $this->updateVariables($variables, $connection);
1 ignored issue
show
Compatibility introduced by
$connection of type object<flipbox\craft\ember\records\ActiveRecord> is not a sub-type of object<flipbox\craft\hubspot\records\Connection>. It seems like you assume a child class of the class flipbox\craft\ember\records\ActiveRecord to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
92
        }
93
94
        $variables['types'] = $this->getAvailableConnections();
95
        $variables['connection'] = $connection;
96
        $variables['fullPageForm'] = true;
97
98
        return $this->renderTemplate(static::TEMPLATE_UPSERT, $variables);
99
    }
100
101
    /**
102
     * @return array
103
     * @throws \yii\base\InvalidConfigException
104
     */
105
    private function getAvailableConnections()
106
    {
107
        $classes = HubSpot::getInstance()->getConnections()->getTypes();
108
109
        $connections = [];
110
        foreach ($classes as $connection) {
111
            if (!$connection instanceof SavableConnectionInterface) {
112
                $connection = Instance::ensure($connection, SavableConnectionInterface::class);
113
            }
114
115
            $connections[get_class($connection)] = $connection;
116
        }
117
118
        return $connections;
119
    }
120
121
    /*******************************************
122
     * BASE PATHS
123
     *******************************************/
124
125
    /**
126
     * @return string
127
     */
128
    protected function getBaseCpPath(): string
129
    {
130
        return parent::getBaseCpPath() . '/connections';
131
    }
132
133
    /**
134
     * @return string
135
     */
136
    protected function getBaseActionPath(): string
137
    {
138
        return parent::getBaseActionPath() . '/connections';
139
    }
140
141
    /*******************************************
142
     * UPDATE VARIABLES
143
     *******************************************/
144
145
    /**
146
     * @param array $variables
147
     * @param Connection $connection
148
     */
149
    protected function updateVariables(array &$variables, Connection $connection)
150
    {
151
        $this->baseVariables($variables);
152
        $variables['title'] .= ' - ' . Craft::t('hubspot', 'Edit') . ' ' . $connection->handle;
153
        $variables['continueEditingUrl'] = $this->getBaseContinueEditingUrl('/' . $connection->getId());
154
        $variables['crumbs'][] = [
155
            'label' => $connection->handle,
156
            'url' => UrlHelper::url($variables['continueEditingUrl'])
157
        ];
158
    }
159
}
160