CheckoutConfigProvider::getConfig()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 24
rs 9.6666
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Copyright © 2019 O2TI. All rights reserved.
4
 * See LICENSE.txt for license details.
5
 */
6
7
namespace O2TI\SocialLogin\ConfigProvider;
8
9
use Magento\Checkout\Model\ConfigProviderInterface;
10
use Magento\Framework\App\Config\ScopeConfigInterface;
11
use Magento\Framework\App\RequestInterface;
12
use Magento\Framework\Url\DecoderInterface;
13
use Magento\Framework\Url\EncoderInterface;
14
use Magento\Framework\Url\HostChecker;
15
use Magento\Framework\UrlInterface;
16
use Magento\Store\Model\ScopeInterface;
17
use O2TI\SocialLogin\Provider\Provider;
18
19
class CheckoutConfigProvider implements ConfigProviderInterface
20
{
21
    /**
22
     * Query param name for last url visited.
23
     */
24
    public const REFERER_QUERY_PARAM_NAME = 'referer';
25
26
    /**
27
     * @var ScopeConfigInterface
28
     */
29
    protected $scopeConfig;
30
31
    /**
32
     * @var RequestInterface
33
     */
34
    protected $request;
35
36
    /**
37
     * @var UrlInterface
38
     */
39
    protected $urlBuilder;
40
41
    /**
42
     * @var EncoderInterface
43
     */
44
    protected $urlEncoder;
45
46
    /**
47
     * @var DecoderInterface
48
     */
49
    private $urlDecoder;
50
51
    /**
52
     * @var HostChecker
53
     */
54
    private $hostChecker;
55
56
    /**
57
     * Construct.
58
     *
59
     * @param ScopeConfigInterface $scopeConfig
60
     * @param RequestInterface     $request
61
     * @param UrlInterface         $urlBuilder
62
     * @param EncoderInterface     $urlEncoder
63
     * @param DecoderInterface     $urlDecoder
64
     * @param HostChecker          $hostChecker
65
     */
66
    public function __construct(
67
        ScopeConfigInterface $scopeConfig,
68
        RequestInterface $request,
69
        UrlInterface $urlBuilder,
70
        EncoderInterface $urlEncoder,
71
        DecoderInterface $urlDecoder = null,
72
        HostChecker $hostChecker = null
73
    ) {
74
        $this->scopeConfig = $scopeConfig;
75
        $this->request = $request;
76
        $this->urlBuilder = $urlBuilder;
77
        $this->urlEncoder = $urlEncoder;
78
        $this->urlDecoder = $urlDecoder ?: \Magento\Framework\App\ObjectManager::getInstance()
79
            ->get(DecoderInterface::class);
80
        $this->hostChecker = $hostChecker ?: \Magento\Framework\App\ObjectManager::getInstance()
81
            ->get(HostChecker::class);
82
    }
83
84
    /**
85
     * Module is Enabled.
86
     *
87
     * @return bool
88
     */
89
    private function isEnabled()
90
    {
91
        return (bool) $this->scopeConfig->getValue(
92
            sprintf(Provider::CONFIG_PATH_SOCIAL_LOGIN_ENABLED),
93
            ScopeInterface::SCOPE_STORE
94
        );
95
    }
96
97
    /**
98
     * Provider is Enabled.
99
     *
100
     * @param string $provider
101
     *
102
     * @return bool
103
     */
104
    private function isProviderEnabled($provider)
105
    {
106
        return (bool) $this->scopeConfig->getValue(
107
            sprintf(Provider::CONFIG_PATH_SOCIAL_LOGIN_PROVIDER_ENABLED, $provider),
108
            ScopeInterface::SCOPE_STORE
109
        );
110
    }
111
112
    /**
113
     * Configs.
114
     *
115
     * @return array
116
     */
117
    public function getConfig()
118
    {
119
        $params = [];
120
        $referer = $this->getRequestReferrer();
121
        if ($referer) {
122
            $params = [
123
                self::REFERER_QUERY_PARAM_NAME => $referer,
124
            ];
125
        } else {
126
            $current = $this->urlBuilder->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
127
            $refererCurrent = $this->urlEncoder->encode($current);
128
            $params = [
129
                self::REFERER_QUERY_PARAM_NAME => $refererCurrent,
130
            ];
131
        }
132
133
        return [
134
            'socialLogin' => [
135
                'enabled'               => (bool) $this->isEnabled(),
136
                'redirectUrl'           => $this->urlBuilder->getUrl('sociallogin/endpoint/index', $params),
137
                'providers'             => [
138
                    'facebook'      => $this->isProviderEnabled('facebook'),
139
                    'google'        => $this->isProviderEnabled('google'),
140
                    'WindowsLive'   => $this->isProviderEnabled('WindowsLive'),
141
                ],
142
            ],
143
        ];
144
    }
145
146
    /**
147
     * Retrieve form posting url.
148
     *
149
     * @return string
150
     */
151
    public function getPostActionUrl()
152
    {
153
        return $this->getLoginPostUrl();
0 ignored issues
show
Bug introduced by
The method getLoginPostUrl() does not exist on O2TI\SocialLogin\ConfigP...\CheckoutConfigProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

153
        return $this->/** @scrutinizer ignore-call */ getLoginPostUrl();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
154
    }
155
156
    /**
157
     * Referrer.
158
     *
159
     * @return mixed|null
160
     */
161
    private function getRequestReferrer()
162
    {
163
        $referer = $this->request->getParam(self::REFERER_QUERY_PARAM_NAME);
164
        if ($referer && $this->hostChecker->isOwnOrigin($this->urlDecoder->decode($referer))) {
165
            return $referer;
166
        }
167
168
        return null;
169
    }
170
}
171