Passed
Branch [email protected] (3810d0)
by Bruno
11:09
created

ConfigProviderCc   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 63
c 1
b 0
f 0
dl 0
loc 156
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A getConfig() 0 22 1
A getIcons() 0 25 5
A getLogo() 0 16 2
1
<?php
2
/**
3
 * Copyright © Getnet. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See LICENSE for license details.
7
 */
8
9
namespace Getnet\PaymentMagento\Model\Ui;
10
11
use Getnet\PaymentMagento\Gateway\Config\Config as ConfigBase;
12
use Getnet\PaymentMagento\Gateway\Config\ConfigCc;
13
use Magento\Checkout\Model\ConfigProviderInterface;
14
use Magento\Framework\Session\SessionManager;
15
use Magento\Framework\View\Asset\Source;
16
use Magento\Payment\Model\CcConfig;
17
use Magento\Quote\Api\Data\CartInterface;
18
19
/**
20
 * Class ConfigProviderCc - Defines properties of the payment form.
21
 *
22
 * @SuppressWarnings(PHPCPD)
23
 */
24
class ConfigProviderCc implements ConfigProviderInterface
25
{
26
    /*
27
     * @const string
28
     */
29
    public const CODE = 'getnet_paymentmagento_cc';
30
31
    /*
32
     * @const string
33
     */
34
    public const VAULT_CODE = 'getnet_paymentmagento_cc_vault';
35
36
    /**
37
     * @var ConfigBase
38
     */
39
    private $configBase;
40
41
    /**
42
     * @var configCc
43
     */
44
    private $configCc;
45
46
    /**
47
     * @var CartInterface
48
     */
49
    private $cart;
50
51
    /**
52
     * @var array
53
     */
54
    private $icons = [];
55
56
    /**
57
     * @var CcConfig
58
     */
59
    protected $ccConfig;
60
61
    /**
62
     * @var Source
63
     */
64
    protected $assetSource;
65
66
    /**
67
     * @var SessionManager
68
     */
69
    protected $session;
70
71
    /**
72
     * @param ConfigBase     $configBase
73
     * @param ConfigCc       $configCc
74
     * @param CartInterface  $cart
75
     * @param CcConfig       $ccConfig
76
     * @param Source         $assetSource
77
     * @param SessionManager $session
78
     */
79
    public function __construct(
80
        ConfigBase $configBase,
81
        ConfigCc $configCc,
82
        CartInterface $cart,
83
        CcConfig $ccConfig,
84
        Source $assetSource,
85
        SessionManager $session
86
    ) {
87
        $this->configBase = $configBase;
88
        $this->configCc = $configCc;
89
        $this->cart = $cart;
90
        $this->ccConfig = $ccConfig;
91
        $this->assetSource = $assetSource;
92
        $this->session = $session;
93
    }
94
95
    /**
96
     * Retrieve assoc array of checkout configuration.
97
     *
98
     * @return array
99
     */
100
    public function getConfig()
101
    {
102
        $storeId = $this->cart->getStoreId();
103
104
        return [
105
            'payment' => [
106
                ConfigCc::METHOD => [
107
                    'isActive'             => $this->configCc->isActive($storeId),
108
                    'title'                => $this->configCc->getTitle($storeId),
109
                    'useCvv'               => $this->configCc->isCvvEnabled($storeId),
110
                    'ccTypesMapper'        => $this->configCc->getCcTypesMapper($storeId),
111
                    'logo'                 => $this->getLogo(),
112
                    'icons'                => $this->getIcons(),
113
                    'tax_document_capture' => $this->configCc->hasUseTaxDocumentCapture($storeId),
114
                    'phone_capture'        => $this->configCc->hasUsePhoneCapture($storeId),
115
                    'fraud_manager'        => $this->configCc->hasUseFraudManager($storeId),
116
                    'info_interest'        => $this->configCc->getInfoInterest($storeId),
117
                    'min_installment'      => $this->configCc->getMinInstallment($storeId),
118
                    'max_installment'      => $this->configCc->getMaxInstallment($storeId),
119
                    'ccVaultCode'          => self::VAULT_CODE,
120
                    'fingerPrintSessionId' => $this->session->getSessionId(),
121
                    'fingerPrintCode'      => $this->configBase->getMerchantGatewayOnlineMetrixCode($storeId),
122
                ],
123
            ],
124
        ];
125
    }
126
127
    /**
128
     * Get icons for available payment methods.
129
     *
130
     * @return array
131
     */
132
    public function getIcons()
133
    {
134
        if (!empty($this->icons)) {
135
            return $this->icons;
136
        }
137
        $storeId = $this->cart->getStoreId();
138
        $ccTypes = $this->configCc->getCcAvailableTypes($storeId);
139
        $types = explode(',', $ccTypes);
140
        foreach ($types as $code => $label) {
141
            if (!array_key_exists($code, $this->icons)) {
142
                $asset = $this->ccConfig->createAsset('Getnet_PaymentMagento::images/cc/'.strtolower($label).'.svg');
143
                $placeholder = $this->assetSource->findSource($asset);
144
                if ($placeholder) {
145
                    list($width, $height) = getimagesizefromstring($asset->getSourceFile());
146
                    $this->icons[$label] = [
147
                        'url'    => $asset->getUrl(),
148
                        'width'  => $width,
149
                        'height' => $height,
150
                        'title'  => __($label),
151
                    ];
152
                }
153
            }
154
        }
155
156
        return $this->icons;
157
    }
158
159
    /**
160
     * Get icons for available payment methods.
161
     *
162
     * @return array
163
     */
164
    public function getLogo()
165
    {
166
        $logo = [];
167
        $asset = $this->ccConfig->createAsset('Getnet_PaymentMagento::images/cc/logo.svg');
168
        $placeholder = $this->assetSource->findSource($asset);
169
        if ($placeholder) {
170
            list($width, $height) = getimagesizefromstring($asset->getSourceFile());
171
            $logo = [
172
                'url'    => $asset->getUrl(),
173
                'width'  => $width,
174
                'height' => $height,
175
                'title'  => __('Getnet'),
176
            ];
177
        }
178
179
        return $logo;
180
    }
181
}
182