Main::hookConstructControllerFrontend()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.2
cc 4
eloc 6
nc 3
nop 1
1
<?php
2
3
/**
4
 * @package Social Login
5
 * @author Iurii Makukh
6
 * @copyright Copyright (c) 2018, Iurii Makukh
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\social_login;
11
12
use Exception;
13
use gplcart\core\controllers\frontend\Controller;
14
use gplcart\core\Module;
15
use InvalidArgumentException;
16
17
/**
18
 * Main class for Social Login module
19
 */
20
class Main
21
{
22
23
    /**
24
     * Module class instance
25
     * @var \gplcart\core\Module $module
26
     */
27
    protected $module;
28
29
    /**
30
     * @param Module $module
31
     */
32
    public function __construct(Module $module)
33
    {
34
        $this->module = $module;
35
    }
36
37
    /**
38
     * Implements hook "construct.controller.frontend"
39
     * @param \gplcart\core\controllers\frontend\Controller $controller
40
     */
41
    public function hookConstructControllerFrontend(Controller $controller)
42
    {
43
        if (!$controller->isInternalRoute() && !$controller->isLoggedIn()) {
44
45
            $controller->setData('print_social_login_buttons', function ($controller) {
46
                return $this->getButtonsSafely($controller);
47
            });
48
49
            if (in_array($controller->path(), array('login', 'register'))) {
50
                $controller->setData('social_login_buttons', $this->getButtonsSafely($controller));
51
            }
52
        }
53
    }
54
55
    /**
56
     * Implements hook "route.list"
57
     * @param array $routes
58
     */
59
    public function hookRouteList(array &$routes)
60
    {
61
        $routes['admin/module/settings/social_login'] = array(
62
            'access' => 'module_edit',
63
            'handlers' => array(
64
                'controller' => array('gplcart\\modules\\social_login\\controllers\\Settings', 'editSettings')
65
            )
66
        );
67
    }
68
69
    /**
70
     * Implements hook "module.oauth.providers"
71
     * @param array $providers
72
     */
73
    public function hookModuleOauthProviders(array &$providers)
74
    {
75
        $providers = array_merge($providers, $this->getProviders());
76
    }
77
78
    /**
79
     * Returns an array of rendered buttons keyed by a provider ID
80
     * @param \gplcart\core\controllers\frontend\Controller $controller
81
     * @param array $params
82
     * @return array
83
     * @throws InvalidArgumentException
84
     */
85
    public function getButtons(Controller $controller, array $params = array())
86
    {
87
88
        /** @var \gplcart\modules\oauth\Main $module */
89
        $module = $this->module->getInstance('oauth');
90
        $model = $module->getModel();
91
92
        $buttons = array();
93
94
        foreach ($module->getProviders(array('type' => 'login')) as $id => $provider) {
95
96
            if (empty($provider['status'])
97
                || empty($provider['settings']['client_id'])
98
                || empty($provider['settings']['client_secret'])) {
99
                continue;
100
            }
101
102
            $data = array('provider' => $provider, 'url' => $model->getAuthUrl($provider, $params));
103
            $buttons[$id] = $controller->render($provider['template']['button'], $data);
104
        }
105
106
        return $buttons;
107
    }
108
109
    /**
110
     * Returns an array of rendered buttons or caught exception messages
111
     * @param \gplcart\core\controllers\frontend\Controller $controller
112
     * @return array
113
     */
114
    protected function getButtonsSafely(Controller $controller)
115
    {
116
        try {
117
            return $this->getButtons($controller);
118
        } catch (Exception $ex) {
119
            return array($ex->getMessage());
120
        }
121
    }
122
123
    /**
124
     * Returns an array of providers
125
     * @return array
126
     */
127
    protected function getProviders()
128
    {
129
        $providers = array(
130
            'facebook' => array(
131
                'name' => 'Facebook', // @text
132
                'scope' => 'email',
133
                'url' => array(
134
                    'auth' => 'https://www.facebook.com/v2.8/dialog/oauth',
135
                    'token' => 'https://graph.facebook.com/v2.8/oauth/access_token'
136
                )
137
            ),
138
            'google' => array(
139
                'name' => 'Google+', // @text
140
                'scope' => 'email',
141
                'url' => array(
142
                    'auth' => 'https://accounts.google.com/o/oauth2/auth',
143
                    'token' => 'https://accounts.google.com/o/oauth2/token'
144
                )
145
            )
146
        );
147
148
        return $this->prepareProviders($providers);
149
    }
150
151
    /**
152
     * Prepare an array of providers
153
     * @param array $providers
154
     * @return array
155
     */
156
    protected function prepareProviders(array $providers)
157
    {
158
        $settings = $this->module->getSettings('social_login');
159
160
        foreach ($providers as $provider_id => &$provider) {
161
            $provider += array(
162
                'id' => $provider_id,
163
                'type' => 'login',
164
                'status' => !empty($settings['status'][$provider_id]),
165
                'settings' => array(
166
                    'register' => $settings['register'],
167
                    'register_login' => $settings['register_login'],
168
                    'register_status' => $settings['register_status'],
169
                    'client_id' => isset($settings['client_id'][$provider_id]) ? $settings['client_id'][$provider_id] : '',
170
                    'client_secret' => isset($settings['client_secret'][$provider_id]) ? $settings['client_secret'][$provider_id] : '',
171
                ),
172
                'template' => array('button' => "social_login|buttons/$provider_id"),
173
                'handlers' => array(
174
                    'authorize' => array("gplcart\\modules\\social_login\\handlers\\$provider_id", 'authorize'),
175
                )
176
            );
177
        }
178
179
        return $providers;
180
    }
181
182
183
}
184