Completed
Push — master ( e45dbf...3a73d5 )
by Nate
05:43 queued 04:08
created

Cp::getProviderIcon()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 15
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace flipbox\patron\cp;
4
5
use Craft;
6
use craft\events\RegisterTemplateRootsEvent;
7
use craft\web\View;
8
use Flipbox\OAuth2\Client\Provider\Guardian;
9
use flipbox\patron\events\RegisterProviderIcon;
10
use flipbox\patron\events\RegisterProviders;
11
use flipbox\patron\Patron;
12
use League\OAuth2\Client\Provider\Facebook;
13
use League\OAuth2\Client\Provider\Github;
14
use League\OAuth2\Client\Provider\Google;
15
use League\OAuth2\Client\Provider\Instagram;
16
use League\OAuth2\Client\Provider\LinkedIn;
17
use yii\base\Event;
18
use yii\base\Module;
19
use yii\web\NotFoundHttpException;
20
21
/**
22
 * @property Patron $module
23
 */
24
class Cp extends Module
25
{
26
    /**
27
     * Event to register providers
28
     */
29
    const EVENT_REGISTER_PROVIDERS = 'registerProviders';
30
31
    /**
32
     * @var array
33
     */
34
    private $icons = [];
35
36
    /**
37
     * @var array
38
     */
39
    protected $defaultIcons = [
40
        Google::class => '@vendor/flipboxfactory/patron/src/icons/google.svg',
41
        LinkedIn::class => '@vendor/flipboxfactory/patron/src/icons/linkedin.svg',
42
        Facebook::class => '@vendor/flipboxfactory/patron/src/icons/facebook.svg',
43
        Instagram::class => '@vendor/flipboxfactory/patron/src/icons/instagram.svg',
44
        Github::class => '@vendor/flipboxfactory/patron/src/icons/github.svg',
45
        Guardian::class => '@vendor/flipboxfactory/patron/src/icons/guardian.svg',
46
    ];
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function init()
52
    {
53
        parent::init();
54
55
        // Components
56
        $this->setComponents([
57
            'settings' => services\Settings::class
58
        ]);
59
60
        // Ember templates
61
        Event::on(
62
            View::class,
63
            View::EVENT_REGISTER_CP_TEMPLATE_ROOTS,
64
            function (RegisterTemplateRootsEvent $e) {
65
                $e->roots['patron/ember/card'] = Craft::$app->getPath()->getVendorPath() .
66
                    '/flipboxfactory/craft-assets-card/src/templates';
67
                $e->roots['patron/ember/circle-icon'] = Craft::$app->getPath()->getVendorPath() .
68
                    '/flipboxfactory/craft-assets-circle-icon/src/templates';
69
            }
70
        );
71
    }
72
73
    /**
74
     * @inheritdoc
75
     * @throws NotFoundHttpException
76
     */
77
    public function beforeAction($action)
78
    {
79
        if (!Craft::$app->request->getIsCpRequest()) {
80
            throw new NotFoundHttpException();
81
        }
82
83
        return parent::beforeAction($action);
84
    }
85
86
    /*******************************************
87
     * SERVICES
88
     *******************************************/
89
90
    /**
91
     * @noinspection PhpDocMissingThrowsInspection
92
     * @return services\Settings
93
     */
94
    public function getSettings(): services\Settings
95
    {
96
        /** @noinspection PhpUnhandledExceptionInspection */
97
        /** @noinspection PhpIncompatibleReturnTypeInspection */
98
        return $this->get('settings');
99
    }
100
101
    /**
102
     * @return array
103
     */
104
    public function getProviders()
105
    {
106
        $event = new RegisterProviders([
107
            'providers' => [
108
                Google::class,
109
                LinkedIn::class,
110
                Facebook::class,
111
                Instagram::class,
112
                Github::class,
113
                Guardian::class
114
            ]
115
        ]);
116
117
        $this->trigger(
118
            static::EVENT_REGISTER_PROVIDERS,
119
            $event
120
        );
121
122
        return $event->providers;
123
    }
124
125
    /**
126
     * @param string $class
127
     * @return mixed
128
     */
129
    public function getProviderIcon(string $class)
130
    {
131
        if (!array_key_exists($class, $this->icons)) {
132
            $event = new RegisterProviderIcon([
133
                'icon' => $this->defaultIcons[$class] ?? null
134
            ]);
135
136
            Event::trigger(
137
                $class,
138
                RegisterProviderIcon::REGISTER_ICON,
139
                $event
140
            );
141
142
            $this->icons[$class] = $event->icon;
143
        }
144
145
        return $this->icons[$class];
146
    }
147
}
148