Completed
Push — develop ( eb3b7f...ef26fd )
by Damien
17:04 queued 06:46
created

AbstractPlugin::getCpNavItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @copyright  Copyright (c) Flipbox Digital Limited
4
 * @license    https://flipboxfactory.com/software/saml-core/license
5
 * @link       https://www.flipboxfactory.com/software/saml-core/
6
 */
7
8
namespace flipbox\saml\core;
9
10
use craft\base\Plugin;
11
use craft\events\RegisterTemplateRootsEvent;
12
use craft\web\twig\variables\CraftVariable;
13
use craft\web\View;
14
use flipbox\saml\core\services\AbstractCp;
15
use flipbox\saml\core\services\bindings\AbstractHttpPost;
16
use flipbox\saml\core\services\bindings\AbstractHttpRedirect;
17
use flipbox\saml\core\services\messages\MetadataServiceInterface;
18
use flipbox\saml\core\services\messages\SamlRequestInterface;
19
use flipbox\saml\core\services\messages\SamlResponseInterface;
20
use flipbox\saml\core\services\ProviderIdentityServiceInterface;
21
use flipbox\saml\core\services\ProviderServiceInterface;
22
use yii\base\Event;
23
use craft\helpers\StringHelper;
24
25
/**
26
 * Class AbstractPlugin
27
 * @package flipbox\saml\core
28
 */
29
abstract class AbstractPlugin extends Plugin
30
{
31
32
    /**
33
     * Saml Constants
34
     */
35
    const SP = 'sp';
36
    const IDP = 'idp';
37
38
    /**
39
     * @var bool
40
     */
41
    public $hasCpSettings = true;
42
43
    /**
44
     * @var bool
45
     */
46
    public $hasCpSection = true;
47
48
    /**
49
     * @return string
50
     */
51
    abstract public function getMyType();
52
53
    public function init()
54
    {
55
        parent::init();
56
57
        $this->initCore();
58
    }
59
60
    /**
61
     *
62
     */
63
    public function initCore()
64
    {
65
        \Craft::setAlias('@flipbox/saml/core', __DIR__);
66
67
        /**
68
         * Register Core Module on Craft
69
         */
70
        if (! \Craft::$app->getModule(Saml::MODULE_ID)) {
71
            \Craft::$app->setModule(Saml::MODULE_ID, [
72
                'class' => Saml::class
73
            ]);
74
        }
75
76
        $this->registerTemplates();
77
78
        /**
79
         * Set saml plugin to the craft variable
80
         */
81
        Event::on(
82
            CraftVariable::class,
83
            CraftVariable::EVENT_INIT,
84
            function (Event $event) {
85
                /** @var CraftVariable $variable */
86
                $variable = $event->sender;
87
                $variable->set($this->getPluginVariableHandle(), self::getInstance());
88
            }
89
        );
90
    }
91
92
    /**
93
     * @return array
94
     */
95
    private function getSubNav()
96
    {
97
        return [
98
            'saml.setup'      => [
99
                'url'   => $this->getHandle() . '/',
100
                'label' => \Craft::t(
101
                    $this->getHandle(),
102
                    'Setup'
103
                ),
104
            ],
105
            'saml.myProvider' => [
106
                'url'   => $this->getHandle() . '/metadata/my-provider',
107
                'label' => \Craft::t(
108
                    $this->getHandle(),
109
                    'My Provider'
110
                ),
111
            ],
112
            'saml.providers'    => [
113
                'url'   => $this->getHandle() . '/metadata',
114
                'label' => \Craft::t(
115
                    $this->getHandle(),
116
                    'Provider List'
117
                ),
118
            ],
119
            'saml.keychain'   => [
120
                'url'   => $this->getHandle() . '/keychain',
121
                'label' => \Craft::t(
122
                    $this->getHandle(),
123
                    'Keychain'
124
                ),
125
            ],
126
            'saml.settings'   => [
127
                'url'   => $this->getHandle() . '/settings',
128
                'label' => \Craft::t(
129
                    $this->getHandle(),
130
                    'Settings'
131
                ),
132
            ],
133
        ];
134
    }
135
136
    /**
137
     * @inheritdoc
138
     */
139
    public function getCpNavItem()
140
    {
141
        return array_merge(parent::getCpNavItem(), [
142
            'subnav' => $this->getSubNav()
143
        ]);
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public function getPluginVariableHandle()
150
    {
151
        return StringHelper::camelCase($this->handle);
152
    }
153
154
    /**
155
     * Registering the core templates for SP and IDP to use.
156
     */
157
    protected function registerTemplates()
158
    {
159
        Event::on(
160
            View::class,
161
            View::EVENT_REGISTER_CP_TEMPLATE_ROOTS,
162
            function (RegisterTemplateRootsEvent $e) {
163
                if (is_dir($baseDir = Saml::getTemplateRoot())) {
164
                    $e->roots[Saml::getTemplateRootKey($this)] = $baseDir;
165
                }
166
            }
167
        );
168
    }
169
170
    /**
171
     * @return string
172
     */
173
    public function getRemoteType()
174
    {
175
        $type = static::SP;
176
        if ($this->getMyType() === static::SP) {
177
            $type = static::IDP;
178
        }
179
180
        return $type;
181
    }
182
183
    /**
184
     * Components
185
     */
186
187
    /**
188
     * @noinspection PhpDocMissingThrowsInspection
189
     * @returns AbstractCp
190
     */
191
    public function getCp()
192
    {
193
194
        /** @noinspection PhpUnhandledExceptionInspection */
195
        /** @noinspection PhpIncompatibleReturnTypeInspection */
196
        return $this->get('cp');
197
    }
198
199
    /**
200
     * @noinspection PhpDocMissingThrowsInspection
201
     * @returns ProviderServiceInterface
202
     */
203
    public function getProvider()
204
    {
205
206
        /** @noinspection PhpUnhandledExceptionInspection */
207
        /** @noinspection PhpIncompatibleReturnTypeInspection */
208
        return $this->get('provider');
209
    }
210
211
    /**
212
     * @noinspection PhpDocMissingThrowsInspection
213
     * @returns ProviderIdentityServiceInterface
214
     */
215
    public function getProviderIdentity()
216
    {
217
        /** @noinspection PhpUnhandledExceptionInspection */
218
        /** @noinspection PhpIncompatibleReturnTypeInspection */
219
        return $this->get('providerIdentity');
220
    }
221
222
    /**
223
     * @noinspection PhpDocMissingThrowsInspection
224
     * @return MetadataServiceInterface
225
     */
226
    public function getMetadata()
227
    {
228
        /** @noinspection PhpUnhandledExceptionInspection */
229
        /** @noinspection PhpIncompatibleReturnTypeInspection */
230
        return $this->get('metadata');
231
    }
232
233
    /**
234
     * @noinspection PhpDocMissingThrowsInspection
235
     * @return SamlRequestInterface
236
     * @throws \yii\base\InvalidConfigException
237
     */
238
    public function getLogoutRequest()
239
    {
240
        /** @noinspection PhpUnhandledExceptionInspection */
241
        /** @noinspection PhpIncompatibleReturnTypeInspection */
242
        return $this->get('logoutRequest');
243
    }
244
245
    /**
246
     * @noinspection PhpDocMissingThrowsInspection
247
     * @return SamlResponseInterface
248
     * @throws \yii\base\InvalidConfigException
249
     */
250
    public function getLogoutResponse()
251
    {
252
        /** @noinspection PhpUnhandledExceptionInspection */
253
        /** @noinspection PhpIncompatibleReturnTypeInspection */
254
        return $this->get('logoutResponse');
255
    }
256
257
    /**
258
     * Bindings
259
     */
260
261
    /**
262
     * @noinspection PhpDocMissingThrowsInspection
263
     * @return AbstractHttpPost
264
     */
265
    public function getHttpPost()
266
    {
267
        /** @noinspection PhpUnhandledExceptionInspection */
268
        /** @noinspection PhpIncompatibleReturnTypeInspection */
269
        return $this->get('httpPost');
270
    }
271
272
    /**
273
     * @noinspection PhpDocMissingThrowsInspection
274
     * @return AbstractHttpRedirect
275
     */
276
    public function getHttpRedirect()
277
    {
278
        /** @noinspection PhpUnhandledExceptionInspection */
279
        /** @noinspection PhpIncompatibleReturnTypeInspection */
280
        return $this->get('httpRedirect');
281
    }
282
283
284
    /**
285
     * Log Functions
286
     */
287
288
    /**
289
     * @param $message
290
     */
291
    public static function error($message)
292
    {
293
        \Craft::error($message, static::getInstance()->getHandle());
294
    }
295
296
    /**
297
     * @param $message
298
     */
299
    public static function warning($message)
300
    {
301
        \Craft::warning($message, static::getInstance()->getHandle());
302
    }
303
304
    /**
305
     * @param $message
306
     */
307
    public static function info($message)
308
    {
309
        \Craft::info($message, static::getInstance()->getHandle());
310
    }
311
312
    /**
313
     * @param $message
314
     */
315
    public static function debug($message)
316
    {
317
        \Craft::debug($message, static::getInstance()->getHandle());
318
    }
319
}
320