Providers::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 19
rs 9.9666
cc 3
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * Copyright © 2019 O2TI. All rights reserved.
4
 * See LICENSE.txt for license details.
5
 */
6
7
namespace O2TI\SocialLogin\Block;
8
9
use Magento\Framework\App\Config\ScopeConfigInterface;
10
use Magento\Framework\App\RequestInterface;
11
use Magento\Framework\Url\DecoderInterface;
12
use Magento\Framework\Url\EncoderInterface;
13
use Magento\Framework\Url\HostChecker;
14
use Magento\Framework\UrlInterface;
15
use Magento\Framework\View\Element\Template;
16
use Magento\Store\Model\ScopeInterface;
17
use O2TI\SocialLogin\Provider\Provider;
18
19
class Providers extends Template
20
{
21
    /**
22
     * Query param name for last url visited.
23
     */
24
    public const REFERER_QUERY_PARAM_NAME = 'referer';
25
26
    /**
27
     * Module is Enabled.
28
     */
29
    public const CONFIG_PATH_SOCIAL_LOGIN_ENABLED = 'social_login/config/enabled';
30
31
    /**
32
     * @var ScopeConfigInterface
33
     */
34
    private $scopeConfig;
35
36
    /**
37
     * @var UrlInterface
38
     */
39
    protected $urlBuilder;
40
41
    /**
42
     * @var RequestInterface
43
     */
44
    protected $request;
45
46
    /**
47
     * @var EncoderInterface
48
     */
49
    protected $urlEncoder;
50
51
    /**
52
     * @var DecoderInterface
53
     */
54
    private $urlDecoder;
55
56
    /**
57
     * @var HostChecker
58
     */
59
    private $hostChecker;
60
61
    /**
62
     * Construct.
63
     *
64
     * @param Context              $context
0 ignored issues
show
Bug introduced by
The type O2TI\SocialLogin\Block\Context was not found. Did you mean Context? If so, make sure to prefix the type with \.
Loading history...
65
     * @param ScopeConfigInterface $scopeConfig
66
     * @param RequestInterface     $request
67
     * @param UrlInterface         $urlBuilder
68
     * @param EncoderInterface     $urlEncoder
69
     * @param DecoderInterface     $urlDecoder
70
     * @param HostChecker          $hostChecker
71
     * @param array                $data
72
     */
73
    public function __construct(
74
        \Magento\Backend\Block\Template\Context $context,
75
        ScopeConfigInterface $scopeConfig,
76
        RequestInterface $request,
77
        UrlInterface $urlBuilder,
78
        EncoderInterface $urlEncoder,
79
        DecoderInterface $urlDecoder = null,
80
        HostChecker $hostChecker = null,
81
        array $data = []
82
    ) {
83
        $this->scopeConfig = $scopeConfig;
84
        $this->request = $request;
85
        $this->urlBuilder = $urlBuilder;
86
        $this->urlEncoder = $urlEncoder;
87
        $this->urlDecoder = $urlDecoder ?: \Magento\Framework\App\ObjectManager::getInstance()
88
            ->get(DecoderInterface::class);
89
        $this->hostChecker = $hostChecker ?: \Magento\Framework\App\ObjectManager::getInstance()
90
            ->get(HostChecker::class);
91
        parent::__construct($context, $data);
92
    }
93
94
    /**
95
     * Enabled.
96
     *
97
     * @param string $provider
98
     *
99
     * @return bool
100
     */
101
    private function isEnabled($provider)
102
    {
103
        return (bool) $this->scopeConfig->getValue(
104
            sprintf(Provider::CONFIG_PATH_SOCIAL_LOGIN_PROVIDER_ENABLED, $provider),
105
            ScopeInterface::SCOPE_STORE
106
        );
107
    }
108
109
    /**
110
     * Configs.
111
     *
112
     * @return array
113
     */
114
    public function getSocialConfig()
115
    {
116
        $params = [];
117
        $referer = $this->getRequestReferrer();
118
        if ($referer) {
119
            $params = [
120
                self::REFERER_QUERY_PARAM_NAME => $referer,
121
            ];
122
        } else {
123
            $current = $this->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
124
            $refererCurrent = $this->urlEncoder->encode($current);
125
            $params = [
126
                self::REFERER_QUERY_PARAM_NAME => $refererCurrent,
127
            ];
128
        }
129
130
        return [
131
            'socialLogin' => [
132
                'social-login-url' => $this->getLoginPostUrlBase(),
133
                'enabled'          => (bool) $this->scopeConfig->getValue(
134
                    self::CONFIG_PATH_SOCIAL_LOGIN_ENABLED,
135
                    ScopeInterface::SCOPE_STORE
136
                ),
137
                'redirectUrl'           => $this->urlBuilder->getUrl('sociallogin/endpoint/index', $params),
138
                'providers'             => [
139
                    'facebook'      => $this->isEnabled('facebook'),
140
                    'google'        => $this->isEnabled('google'),
141
                    'WindowsLive'   => $this->isEnabled('WindowsLive'),
142
                ],
143
            ],
144
        ];
145
    }
146
147
    /**
148
     * Referrer.
149
     *
150
     * @return mixed|null
151
     */
152
    private function getRequestReferrer()
153
    {
154
        $referer = $this->request->getParam(self::REFERER_QUERY_PARAM_NAME);
155
        if ($referer && $this->hostChecker->isOwnOrigin($this->urlDecoder->decode($referer))) {
156
            return $referer;
157
        }
158
159
        return null;
160
    }
161
162
    /**
163
     * Retrieve customer login POST URL.
164
     *
165
     * @return string
166
     */
167
    public function getLoginPostUrlBase()
168
    {
169
        $params = [];
170
        $referer = $this->getRequestReferrer();
171
        if ($referer) {
172
            $params = [
173
                self::REFERER_QUERY_PARAM_NAME => $referer,
174
            ];
175
        }
176
177
        return $this->urlBuilder->getUrl('sociallogin/endpoint/index', $params);
178
    }
179
}
180