ConfigProvider::isPromoted()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Pagantis\Pagantis\Model\Ui;
4
5
use Magento\Checkout\Model\ConfigProviderInterface;
6
use Magento\Checkout\Model\Session;
7
use Pagantis\Pagantis\Helper\ExtraConfig;
8
use Magento\Framework\Locale\Resolver;
9
10
/**
11
 * Class ConfigProvider
12
 * @package Pagantis\Pagantis\Model\Ui
13
 */
14
final class ConfigProvider implements ConfigProviderInterface
15
{
16
    const CODE = 'pagantis';
17
18
    const CODE4X = 'pagantis4x';
19
20
    /**
21
     * @var \Magento\Payment\Model\MethodInterface
22
     */
23
    protected $method;
24
25
    /**
26
     * @var Session
27
     */
28
    protected $checkoutSession;
29
30
    /**
31
     * @var String
32
     */
33
    protected $extraConfig;
34
35
    /**
36
     * @var String
37
     */
38
    protected $assetRepository;
39
40
    /**
41
     * @var String
42
     */
43
    protected $store;
44
45
    /**
46
     * @var Resolver
47
     */
48
    protected $resolver;
49
50
    /**
51
     * ConfigProvider constructor.
52
     *
53
     * @param \Magento\Payment\Helper\Data             $paymentHelper
54
     * @param Session                                  $checkoutSession
55
     * @param ExtraConfig                              $extraConfig
56
     * @param \Magento\Framework\View\Asset\Repository $assetRepository
57
     * @param Resolver                                 $resolver
58
     *
59
     * @throws \Magento\Framework\Exception\LocalizedException
60
     */
61
    public function __construct(
62
        \Magento\Payment\Helper\Data $paymentHelper,
63
        Session $checkoutSession,
64
        ExtraConfig $extraConfig,
65
        \Magento\Framework\View\Asset\Repository $assetRepository,
66
        Resolver $resolver
67
    ) {
68
        $this->method = $paymentHelper->getMethodInstance(self::CODE);
69
        $this->checkoutSession = $checkoutSession;
70
        $this->extraConfig = $extraConfig->getExtraConfig();
0 ignored issues
show
Documentation Bug introduced by
It seems like $extraConfig->getExtraConfig() of type array or array is incompatible with the declared type string of property $extraConfig.

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...
71
        $this->assetRepository = $assetRepository;
0 ignored issues
show
Documentation Bug introduced by
It seems like $assetRepository of type Magento\Framework\View\Asset\Repository is incompatible with the declared type string of property $assetRepository.

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...
72
        $this->resolver = $resolver;
73
    }
74
75
    /**
76
     * Retrieve assoc array of checkout configuration
77
     *
78
     * @return array
79
     * @throws \Magento\Framework\Exception\LocalizedException
80
     * @throws \Magento\Framework\Exception\NoSuchEntityException
81
     */
82
    public function getConfig()
83
    {
84
        $quote = $this->checkoutSession->getQuote();
85
86
        $positionSelector = $this->extraConfig['PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'];
87
        if ($positionSelector == 'default') {
88
            $positionSelector = '.pagantisSimulator';
89
            $positionSelector4x = '.pagantisSimulator4x';
90
        }
91
92
        return [
93
            'payment' => [
94
                self::CODE => [
95
                    'total' => $quote->getGrandTotal(),
96
                    'enabled' => $this->method->getConfigData('active_12x'),
97
                    'product_simulator' => $this->method->getConfigData('product_simulator'),
98
                    'title' => __($this->extraConfig['PAGANTIS_TITLE']),
99
                    'subtitle' => __($this->extraConfig['PAGANTIS_TITLE_EXTRA']),
100
                    'image' => 'https://cdn.digitalorigin.com/assets/master/logos/pg-130x30.svg',
101
                    'publicKey' => $this->method->getConfigData('pagantis_public_key'),
102
                    'locale' => strstr($this->resolver->getLocale(), '_', true),
103
                    'country' => strstr($this->resolver->getLocale(), '_', true),
104
                    'promotedAmount' => $this->getPromotedAmount($quote),
105
                    'thousandSeparator' => $this->extraConfig['PAGANTIS_SIMULATOR_THOUSANDS_SEPARATOR'],
106
                    'decimalSeparator' => $this->extraConfig['PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR'],
107
                    'quotesStart' => $this->extraConfig['PAGANTIS_SIMULATOR_START_INSTALLMENTS'],
108
                    'type'      => $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_TYPE_CHECKOUT'],
109
                    'skin'      => $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_SKIN'],
110
                    'position'  => $positionSelector
111
                ],
112
                self::CODE4X => [
113
                    'total' => $quote->getGrandTotal(),
114
                    'enabled' => $this->method->getConfigData('active_4x'),
115
                    'product_simulator' => "1",
116
                    'title' => __($this->extraConfig['PAGANTIS_TITLE_4x']),
117
                    'subtitle' => __($this->extraConfig['PAGANTIS_TITLE_EXTRA']),
118
                    'image' => 'https://cdn.digitalorigin.com/assets/master/logos/pg-130x30.svg',
119
                    'publicKey' => $this->method->getConfigData('pagantis_public_key_4x'),
120
                    'locale' => strstr($this->resolver->getLocale(), '_', true),
121
                    'country' => strstr($this->resolver->getLocale(), '_', true),
122
                    'promotedAmount' => $this->getPromotedAmount($quote),
123
                    'thousandSeparator' => $this->extraConfig['PAGANTIS_SIMULATOR_THOUSANDS_SEPARATOR'],
124
                    'decimalSeparator' => $this->extraConfig['PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR'],
125
                    'quotesStart' => $this->extraConfig['PAGANTIS_SIMULATOR_START_INSTALLMENTS'],
126
                    'type'      => $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_TYPE_CHECKOUT'],
127
                    'skin'      => $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_SKIN'],
128
                    'position'  => $positionSelector4x
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $positionSelector4x does not seem to be defined for all execution paths leading up to this point.
Loading history...
129
                ],
130
            ],
131
        ];
132
    }
133
134
    /**
135
     * @param $quote
136
     *
137
     * @return int
138
     */
139
    private function getPromotedAmount($quote)
140
    {
141
        $promotedAmount = 0;
142
        $items = $quote->getAllVisibleItems();
143
        foreach ($items as $key => $item) {
144
            $promotedProduct = $this->isPromoted($item);
145
            if ($promotedProduct == 'true') {
146
                $promotedAmount+=$item->getPrice()*$item->getQty();
147
            }
148
        }
149
150
        return $promotedAmount;
151
    }
152
153
    /**
154
     * @param $item
155
     *
156
     * @return string
157
     */
158
    private function isPromoted($item)
159
    {
160
        $magentoProductId = $item->getProductId();
161
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
162
        $product = $objectManager->create('Magento\Catalog\Model\Product')->load($magentoProductId);
163
        return ($product->getData('pagantis_promoted') === '1') ? 'true' : 'false';
164
    }
165
}
166