ConfigProvider   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 109
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A getConfig() 0 9 1
A getIcons() 0 25 5
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\Vault;
10
11
use Getnet\PaymentMagento\Gateway\Config\ConfigCc;
12
use Getnet\PaymentMagento\Gateway\Config\ConfigCcVault;
13
use Magento\Checkout\Model\ConfigProviderInterface;
14
use Magento\Framework\Exception\InputException;
15
use Magento\Framework\Exception\NoSuchEntityException;
16
use Magento\Framework\View\Asset\Source;
17
use Magento\Payment\Model\CcConfig;
18
use Magento\Quote\Api\Data\CartInterface;
19
20
/**
21
 * Class ConfigProvider - Defines properties of the payment form.
22
 */
23
class ConfigProvider implements ConfigProviderInterface
24
{
25
    public const CODE = 'getnet_paymentmagento_cc_vault';
26
27
    /**
28
     * @var Config
29
     */
30
    private $configCcVault;
31
32
    /**
33
     * @var CartInterface
34
     */
35
    private $cart;
36
37
    /**
38
     * @var array
39
     */
40
    private $icons = [];
41
42
    /**
43
     * @var CcConfig
44
     */
45
    protected $ccConfig;
46
47
    /**
48
     * @var CcConfig
49
     */
50
    protected $configCc;
51
52
    /**
53
     * @var Source
54
     */
55
    protected $assetSource;
56
57
    /**
58
     * ConfigProvider constructor.
59
     *
60
     * @param CartInterface $cart
61
     * @param ConfigCc      $configCc
62
     * @param ConfigCcVault $configCcVault
63
     * @param CcConfig      $ccConfig
64
     * @param Source        $assetSource
65
     */
66
    public function __construct(
67
        CartInterface $cart,
68
        ConfigCc $configCc,
69
        ConfigCcVault $configCcVault,
70
        CcConfig $ccConfig,
71
        Source $assetSource
72
    ) {
73
        $this->cart = $cart;
74
        $this->configCc = $configCc;
0 ignored issues
show
Documentation Bug introduced by
It seems like $configCc of type Getnet\PaymentMagento\Gateway\Config\ConfigCc is incompatible with the declared type Magento\Payment\Model\CcConfig of property $configCc.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
75
        $this->assetSource = $assetSource;
76
        $this->ccConfig = $ccConfig;
77
        $this->configCcVault = $configCcVault;
0 ignored issues
show
Documentation Bug introduced by
It seems like $configCcVault of type Getnet\PaymentMagento\Gateway\Config\ConfigCcVault is incompatible with the declared type Getnet\PaymentMagento\Model\Ui\Vault\Config of property $configCcVault.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
78
    }
79
80
    /**
81
     * Retrieve assoc array of checkout configuration.
82
     *
83
     * @throws InputException
84
     * @throws NoSuchEntityException
85
     *
86
     * @return array
87
     */
88
    public function getConfig()
89
    {
90
        $storeId = $this->cart->getStoreId();
91
92
        return [
93
            'payment' => [
94
                self::CODE => [
95
                    'useCvv' => $this->configCcVault->useCvv($storeId),
96
                    'icons'  => $this->getIcons(),
97
                ],
98
            ],
99
        ];
100
    }
101
102
    /**
103
     * Get icons for available payment methods.
104
     *
105
     * @return array
106
     */
107
    public function getIcons()
108
    {
109
        if (!empty($this->icons)) {
110
            return $this->icons;
111
        }
112
        $storeId = $this->cart->getStoreId();
113
        $ccTypes = $this->configCc->getCcAvailableTypes($storeId);
0 ignored issues
show
Unused Code introduced by
The call to Magento\Payment\Model\Cc...::getCcAvailableTypes() has too many arguments starting with $storeId. ( Ignorable by Annotation )

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

113
        /** @scrutinizer ignore-call */ 
114
        $ccTypes = $this->configCc->getCcAvailableTypes($storeId);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
114
        $types = explode(',', $ccTypes);
0 ignored issues
show
Bug introduced by
$ccTypes of type array is incompatible with the type string expected by parameter $string of explode(). ( Ignorable by Annotation )

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

114
        $types = explode(',', /** @scrutinizer ignore-type */ $ccTypes);
Loading history...
115
        foreach ($types as $code => $label) {
116
            if (!array_key_exists($code, $this->icons)) {
117
                $asset = $this->ccConfig->createAsset('Getnet_PaymentMagento::images/cc/'.strtolower($label).'.svg');
118
                $placeholder = $this->assetSource->findSource($asset);
119
                if ($placeholder) {
120
                    list($width, $height) = getimagesizefromstring($asset->getSourceFile());
121
                    $this->icons[$label] = [
122
                        'url'    => $asset->getUrl(),
123
                        'width'  => $width,
124
                        'height' => $height,
125
                        'title'  => __($label),
126
                    ];
127
                }
128
            }
129
        }
130
131
        return $this->icons;
132
    }
133
}
134