Completed
Push — master ( 444139...b6b37e )
by Damien
09:32
created

AbstractEditController::actionNewIdp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 9.1563
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: dsmrt
5
 * Date: 3/10/18
6
 * Time: 8:40 PM
7
 */
8
9
namespace flipbox\saml\core\controllers\cp\view\metadata;
10
11
use Craft;
12
use flipbox\saml\core\AbstractPlugin;
13
use flipbox\saml\core\controllers\cp\view\AbstractController;
14
use flipbox\saml\core\records\ProviderInterface;
15
use craft\helpers\UrlHelper;
16
17
/**
18
 * Class AbstractEditController
19
 * @package flipbox\saml\core\controllers\cp\view\metadata
20
 */
21
abstract class AbstractEditController extends AbstractController
22
{
23
    use VariablesTrait;
24
25
    const TEMPLATE_INDEX = DIRECTORY_SEPARATOR . '_cp' . DIRECTORY_SEPARATOR . 'metadata';
26
27
    /**
28
     * @param string|null $providerId
29
     * @return \yii\web\Response
30
     */
31
    public function actionIndex($providerId = null, $overwriteVariables = [])
32
    {
33
        $variables = $this->prepVariables($providerId);
34
        $provider = $variables['provider'];
35
36
        $variables['title'] = Craft::t(
37
            $this->getSamlPlugin()->getHandle(),
38
            'Edit ' . $this->getTitle($provider->getType())
39
        );
40
41
        $variables['createType'] = $variables['remoteType'];
42
43
        if (isset($variables['provider']) && $variables['provider'] instanceof ProviderInterface) {
44
45
            /**
46
             * Actions
47
             */
48
            $variables['actions'] = $this->getActions($variables['provider']);
49
        }
50
51
        $variables = array_merge($variables, $overwriteVariables);
52
        return $this->renderTemplate(
53
            $this->getTemplateIndex() . static::TEMPLATE_INDEX . DIRECTORY_SEPARATOR . 'edit',
54
            $variables
55
        );
56
    }
57
58
    /**
59
     * @return \yii\web\Response
60
     */
61
    public function actionNewIdp()
62
    {
63
        /** @var AbstractPlugin $plugin */
64
        $plugin = $this->getSamlPlugin();
65
        return $this->actionIndex(null, [
66
            'title'      => 'New ' . $this->getTitle($plugin::IDP),
67
            'createType' => $plugin::IDP,
68
            'crumbs'     => [
69
                [
70
                    'url'   => UrlHelper::cpUrl(
71
                        implode(
72
                            '/',
73
                            [
74
                                $plugin->getHandle(),
75
                            ]
76
                        )
77
                    ),
78
                    'label' => $plugin->name,
79
                ],
80
                [
81
                    'url'   => UrlHelper::cpUrl(
82
                        implode(
83
                            '/',
84
                            [
85
                                $plugin->getHandle(),
86
                                'metadata',
87
                            ]
88
                        )
89
                    ),
90
                    'label' => 'Provider List',
91
                ],
92
                [
93
                    'url'   => UrlHelper::cpUrl(
94
                        implode(
95
                            '/',
96
                            [
97
                                $plugin->getHandle(),
98
                                'metadata',
99
                                'new-idp'
100
                            ]
101
                        )
102
                    ),
103
                    'label' => 'New IDP',
104
                ],
105
            ],
106
        ]);
107
    }
108
109
    /**
110
     * @return \yii\web\Response
111
     */
112
    public function actionNewSp()
113
    {
114
        /** @var AbstractPlugin $plugin */
115
        $plugin = $this->getSamlPlugin();
116
        return $this->actionIndex(null, [
117
            'title'      => 'New ' . $this->getTitle($plugin::SP),
118
            'createType' => $plugin::SP,
119
            'crumbs'     => [
120
                [
121
                    'url'   => UrlHelper::cpUrl(
122
                        implode(
123
                            '/',
124
                            [
125
                                $plugin->getHandle(),
126
                            ]
127
                        )
128
                    ),
129
                    'label' => $plugin->name,
130
                ],
131
                [
132
                    'url'   => UrlHelper::cpUrl(
133
                        implode(
134
                            '/',
135
                            [
136
                                $plugin->getHandle(),
137
                                'metadata',
138
                            ]
139
                        )
140
                    ),
141
                    'label' => 'Provider List',
142
                ],
143
                [
144
                    'url'   => UrlHelper::cpUrl(
145
                        implode(
146
                            '/',
147
                            [
148
                                $plugin->getHandle(),
149
                                'metadata',
150
                                'new-sp'
151
                            ]
152
                        )
153
                    ),
154
                    'label' => 'New SP',
155
                ],
156
            ],
157
        ]);
158
    }
159
160
    /**
161
     * @return \yii\web\Response
162
     */
163
    public function actionMyProvider()
164
    {
165
        $provider = $this->getSamlPlugin()->getProvider()->findOwn();
166
        $variables = $this->prepVariables(
167
            $provider ? $provider : null
168
        );
169
170
        if ($provider) {
171
            $variables['provider'] = $provider;
172
173
            $variables = array_merge(
174
                $variables,
175
                $this->addUrls($provider)
176
            );
177
        } else {
178
            $provider = $variables['provider'];
179
            $variables['provider']->entityId = $this->getSamlPlugin()->getSettings()->getEntityId();
180
            $variables['provider']->providerType = $this->getSamlPlugin()->getMyType();
181
            $variables['provider']->label = 'My Provider';
182
        }
183
184
        /**
185
         * Actions
186
         */
187
        $variables['actions'] = $this->getActions($provider);
188
189
        /**
190
         * Edit Title
191
         */
192
        $variables['title'] = Craft::t(
193
            $this->getSamlPlugin()->getHandle(),
194
            'My Provider (' . strtoupper($variables['provider']->providerType) . ')'
195
        );
196
197
        $variables['createType'] = $variables['myType'];
198
199
        return $this->renderTemplate(
200
            $this->getTemplateIndex() . static::TEMPLATE_INDEX . DIRECTORY_SEPARATOR . 'edit',
201
            $variables
202
        );
203
    }
204
}
205