Completed
Push — master ( 1b679c...e05b34 )
by
unknown
12s queued 10s
created

ConfigProvider::isPromoted()   A

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
    /**
19
     * @var \Magento\Payment\Model\MethodInterface
20
     */
21
    protected $method;
22
23
    /**
24
     * @var Session
25
     */
26
    protected $checkoutSession;
27
28
    /**
29
     * @var String
30
     */
31
    protected $extraConfig;
32
33
    /**
34
     * @var String
35
     */
36
    protected $assetRepository;
37
38
    /**
39
     * @var String
40
     */
41
    protected $store;
42
43
    /**
44
     * @var Resolver
45
     */
46
    protected $resolver;
47
48
    /**
49
     * ConfigProvider constructor.
50
     *
51
     * @param \Magento\Payment\Helper\Data             $paymentHelper
52
     * @param Session                                  $checkoutSession
53
     * @param ExtraConfig                              $extraConfig
54
     * @param \Magento\Framework\View\Asset\Repository $assetRepository
55
     * @param Resolver                                 $resolver
56
     *
57
     * @throws \Magento\Framework\Exception\LocalizedException
58
     */
59
    public function __construct(
60
        \Magento\Payment\Helper\Data $paymentHelper,
61
        Session $checkoutSession,
62
        ExtraConfig $extraConfig,
63
        \Magento\Framework\View\Asset\Repository $assetRepository,
64
        Resolver $resolver
65
    ) {
66
        $this->method = $paymentHelper->getMethodInstance(self::CODE);
67
        $this->checkoutSession = $checkoutSession;
68
        $this->extraConfig = $extraConfig->getExtraConfig();
0 ignored issues
show
Documentation Bug introduced by pgarces
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...
69
        $this->assetRepository = $assetRepository;
0 ignored issues
show
Documentation Bug introduced by pgarces
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...
70
        $this->resolver = $resolver;
71
    }
72
73
    /**
74
     * Retrieve assoc array of checkout configuration
75
     *
76
     * @return array
77
     */
78
    public function getConfig()
79
    {
80
        $quote = $this->checkoutSession->getQuote();
81
82
        return [
83
            'payment' => [
84
                self::CODE => [
85
                    'total' => $quote->getGrandTotal(),
86
                    'enabled' => $this->method->getConfigData('active'),
87
                    'title' => __($this->extraConfig['PAGANTIS_TITLE']),
88
                    'subtitle' => __($this->extraConfig['PAGANTIS_TITLE_EXTRA']),
89
                    'image' => $this->assetRepository->getUrl('Pagantis_Pagantis::logopagantis.png'),
90
                    'publicKey' => $this->method->getConfigData('pagantis_public_key'),
91
                    'locale' => strstr($this->resolver->getLocale(), '_', true),
92
                    'promotedAmount' => $this->getPromotedAmount($quote)
93
                ],
94
            ],
95
        ];
96
    }
97
98
    /**
99
     * @param $quote
100
     *
101
     * @return int
102
     */
103
    private function getPromotedAmount($quote)
104
    {
105
        $promotedAmount = 0;
106
        $items = $quote->getAllVisibleItems();
107
        foreach ($items as $key => $item) {
108
            $promotedProduct = $this->isPromoted($item);
109
            if ($promotedProduct == 'true') {
110
                $promotedAmount+=$item->getPrice()*$item->getQty();
111
            }
112
        }
113
114
        return $promotedAmount;
115
    }
116
117
    /**
118
     * @param $item
119
     *
120
     * @return string
121
     */
122
    private function isPromoted($item)
123
    {
124
        $magentoProductId = $item->getProductId();
125
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
126
        $product = $objectManager->create('Magento\Catalog\Model\Product')->load($magentoProductId);
127
        return ($product->getData('pagantis_promoted') === '1') ? 'true' : 'false';
128
    }
129
}
130