Completed
Pull Request — master (#3)
by Damien
07:58
created

AbstractPlugin::warning()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: dsmrt
5
 * Date: 2/10/18
6
 * Time: 9:01 PM
7
 */
8
9
namespace flipbox\saml\core;
10
11
12
use craft\base\Plugin;
13
use craft\events\RegisterTemplateRootsEvent;
14
use craft\web\View;
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
24
abstract class AbstractPlugin extends Plugin
25
{
26
27
    /**
28
     * Saml Constants
29
     */
30
    const SP = 'sp';
31
    const IDP = 'idp';
32
33
34
    /**
35
     * @return string
36
     */
37
    abstract public function getMyType();
38
39
    public function init()
40
    {
41
        parent::init();
42
43
        $this->initCore();
44
    }
45
46
    /**
47
     *
48
     */
49
    public function initCore()
50
    {
51
        /**
52
         * Register Core Module on Craft
53
         */
54
        if (! \Craft::$app->getModule(Saml::MODULE_ID)) {
55
            \Craft::$app->setModule(Saml::MODULE_ID, [
56
                'class' => Saml::class
57
            ]);
58
        }
59
60
        $this->registerTemplates();
61
62
    }
63
64
    /**
65
     * @return Saml|null
66
     */
67
    public function getCore()
68
    {
69
        return \Craft::$app->getModule(Saml::MODULE_ID);
70
    }
71
72
    /**
73
     *
74
     */
75
    protected function registerTemplates()
76
    {
77
        Event::on(
78
            View::class,
79
            View::EVENT_REGISTER_CP_TEMPLATE_ROOTS,
80
            function (RegisterTemplateRootsEvent $e) {
81
                if (is_dir($baseDir = Saml::getTemplateRoot())) {
82
                    $e->roots[Saml::getTemplateRootKey($this)] = $baseDir;
83
                }
84
            }
85
        );
86
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function getRemoteType()
93
    {
94
        $type = static::SP;
95
        if ($this->getMyType() === static::SP) {
96
            $type = static::IDP;
97
        }
98
99
        return $type;
100
    }
101
102
    /**
103
     * Components
104
     */
105
106
    /**
107
     * @returns ProviderServiceInterface
108
     */
109
    public function getProvider()
110
    {
111
        return $this->get('provider');
112
    }
113
114
    /**
115
     * @returns ProviderIdentityServiceInterface
116
     */
117
    public function getProviderIdentity()
118
    {
119
        return $this->get('providerIdentity');
120
    }
121
122
    /**
123
     * @return MetadataServiceInterface
124
     */
125
    public function getMetadata()
126
    {
127
        return $this->get('metadata');
128
    }
129
130
    /**
131
     * @return SamlRequestInterface
132
     * @throws \yii\base\InvalidConfigException
133
     */
134
    public function getLogoutRequest()
135
    {
136
        return $this->get('logoutRequest');
137
    }
138
139
    /**
140
     * @return SamlResponseInterface
141
     * @throws \yii\base\InvalidConfigException
142
     */
143
    public function getLogoutResponse()
144
    {
145
        return $this->get('logoutResponse');
146
    }
147
148
    /**
149
     * Bindings
150
     */
151
152
    /**
153
     * @return AbstractHttpPost
154
     */
155
    public function getHttpPost()
156
    {
157
        return $this->get('httpPost');
158
    }
159
160
    /**
161
     * @return AbstractHttpRedirect
162
     */
163
    public function getHttpRedirect()
164
    {
165
        return $this->get('httpRedirect');
166
    }
167
168
169
    /**
170
     * Log Functions
171
     */
172
173
    /**
174
     * @param $message
175
     */
176
    public static function error($message)
177
    {
178
        \Craft::error($message, static::getInstance()->getHandle());
179
    }
180
    /**
181
     * @param $message
182
     */
183
    public static function warning($message)
184
    {
185
        \Craft::warning($message, static::getInstance()->getHandle());
186
    }
187
    /**
188
     * @param $message
189
     */
190
    public static function info($message)
191
    {
192
        \Craft::info($message, static::getInstance()->getHandle());
193
    }
194
    /**
195
     * @param $message
196
     */
197
    public static function debug($message)
198
    {
199
        \Craft::debug($message, static::getInstance()->getHandle());
200
    }
201
}