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

Block/Product/Simulator.php (1 issue)

1
<?php
2
3
namespace Pagantis\Pagantis\Block\Product;
4
5
use Magento\Backend\Block\Template\Context;
6
use Magento\Catalog\Model\Category;
7
use Magento\Catalog\Model\Product;
8
use Magento\Framework\Registry;
9
use Magento\Framework\View\Element\Template;
10
use Pagantis\Pagantis\Helper\ExtraConfig;
11
use Magento\Framework\Locale\Resolver;
12
13
/**
14
 * Class Simulator
15
 * @package Pagantis\Pagantis\Block\Product
16
 */
17
class Simulator extends Template
18
{
19
    const PROMOTIONS_CATEGORY = 'pagantis-promotion-product';
20
21
    /**
22
     * @var bool
23
     */
24
    protected $enabled;
25
26
    /**
27
     * @var string
28
     */
29
    protected $publicKey;
30
31
    /**
32
     * @var string
33
     */
34
    protected $productSimulator;
35
36
    /**
37
     * @var string
38
     */
39
    protected $promotionProductExtra;
40
41
    /**
42
     * @var Product
43
     */
44
    protected $product;
45
46
    /**
47
     * @var float
48
     */
49
    protected $minAmount;
50
51
    /**
52
     * @var int
53
     */
54
    protected $minInstallments;
55
56
    /**
57
     * @var string
58
     */
59
    protected $priceSelector;
60
61
    /**
62
     * @var string
63
     */
64
    protected $quantitySelector;
65
66
    /**
67
     * @var String
68
     */
69
    protected $positionSelector;
70
71
    /**
72
     * @var Registry
73
     */
74
    protected $registry;
75
76
    /**
77
     * @var Config
78
     */
79
    protected $extraConfig;
80
81
    /**
82
     * @var String
83
     */
84
    protected $simulatorType;
85
86
    /**
87
     * @var String
88
     */
89
    protected $store;
90
91
    /**
92
     * Simulator constructor.
93
     *
94
     * @param Context        $context
95
     * @param Registry       $registry
96
     * @param ExtraConfig    $extraConfig
97
     * @param Resolver $store
98
     * @param array          $data
99
     */
100
    public function __construct(
101
        Context $context,
102
        Registry $registry,
103
        ExtraConfig $extraConfig,
104
        Resolver $store,
105
        array $data = []
106
    ) {
107
        parent::__construct($context, $data);
108
        $this->registry = $registry;
109
        $this->store = $store;
110
        /** @var \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig */
111
        $scopeConfig = $this->_scopeConfig;
112
        $config = $scopeConfig->getValue('payment/pagantis');
113
114
        $this->enabled = $config['active'];
115
        $this->publicKey = isset($config['pagantis_public_key']) ? $config['pagantis_public_key'] : '';
116
        $this->productSimulator = $config['product_simulator'];
117
        $this->extraConfig = $extraConfig->getExtraConfig();
118
119
        $this->minAmount = $this->extraConfig['PAGANTIS_DISPLAY_MIN_AMOUNT'];
120
        $this->minInstallments = $this->extraConfig['PAGANTIS_SIMULATOR_START_INSTALLMENTS'];
121
        $this->priceSelector = $this->extraConfig['PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'];
122
        $this->quantitySelector = $this->extraConfig['PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'];
123
        $this->positionSelector = $this->extraConfig['PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'];
124
        $this->simulatorType = $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_TYPE'];
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function getLocale()
131
    {
132
        return strstr($this->store->getLocale(), '_', true);
133
    }
134
135
    /**
136
     * @param $locale
137
     *
138
     * @return bool
139
     */
140
    public function getAllowedCountry($locale)
141
    {
142
        $locale = strtolower($locale);
143
        $allowedCountries = unserialize($this->extraConfig['PAGANTIS_ALLOWED_COUNTRIES']);
144
        return (in_array(strtolower($locale), $allowedCountries));
145
    }
146
147
    /**
148
     * @return Product
149
     */
150
    protected function getProduct()
151
    {
152
        if (is_null($this->product)) {
153
            $this->product = $this->registry->registry('product');
154
        }
155
156
        return $this->product;
157
    }
158
159
    /**
160
     * @return bool
161
     */
162
    public function isEnabled()
163
    {
164
        return $this->enabled;
165
    }
166
167
    /**
168
     * @return string
169
     */
170
    public function getPublicKey()
171
    {
172
        return $this->publicKey;
173
    }
174
175
    /**
176
     * @return string
177
     */
178
    public function getPromotionProductExtra()
179
    {
180
        return $this->promotionProductExtra;
181
    }
182
183
    /**
184
     * @return bool
185
     */
186
    public function isProductInPromotion()
187
    {
188
        try {
189
            /** @var Category $category */
190
            $category = $this->getProduct()->getCategoryCollection()->addFieldToFilter(
191
                'name',
192
                array('eq' => self::PROMOTIONS_CATEGORY)
193
            )->getFirstItem();
194
        } catch (\Exception $exception) {
195
            return false;
196
        }
197
198
        return $category->getName() === self::PROMOTIONS_CATEGORY ? 1 : 0;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $category->getNam...OTIONS_CATEGORY ? 1 : 0 returns the type integer which is incompatible with the documented return type boolean.
Loading history...
199
    }
200
201
    /**
202
     * @return float
203
     */
204
    public function getFinalPrice()
205
    {
206
        return $this->getProduct()->getFinalPrice();
207
    }
208
209
    /**
210
     * @return array|false|string
211
     */
212
    public function getProductSimulator()
213
    {
214
        return $this->productSimulator;
215
    }
216
217
    /**
218
     * @param int $productSimulator
219
     */
220
    public function setProductSimulator($productSimulator)
221
    {
222
        $this->productSimulator = $productSimulator;
223
    }
224
225
    /**
226
     * @return float
227
     */
228
    public function getMinAmount()
229
    {
230
        return $this->minAmount;
231
    }
232
233
    /**
234
     * @param float $minAmount
235
     */
236
    public function setMinAmount($minAmount)
237
    {
238
        $this->minAmount = $minAmount;
239
    }
240
241
    /**
242
     * @return int
243
     */
244
    public function getMinInstallments()
245
    {
246
        return $this->minInstallments;
247
    }
248
249
    /**
250
     * @param int $minInstallments
251
     */
252
    public function setMinInstallments($minInstallments)
253
    {
254
        $this->minInstallments = $minInstallments;
255
    }
256
257
    /**
258
     * @return string
259
     */
260
    public function getPriceSelector()
261
    {
262
        return $this->priceSelector;
263
    }
264
265
    /**
266
     * @param string $priceSelector
267
     */
268
    public function setPriceSelector($priceSelector)
269
    {
270
        $this->priceSelector = $priceSelector;
271
    }
272
273
    /**
274
     * @return string
275
     */
276
    public function getQuantitySelector()
277
    {
278
        return $this->quantitySelector;
279
    }
280
281
    /**
282
     * @param string $quantitySelector
283
     */
284
    public function setQuantitySelector($quantitySelector)
285
    {
286
        $this->quantitySelector = $quantitySelector;
287
    }
288
289
    /**
290
     * @return String
291
     */
292
    public function getPositionSelector()
293
    {
294
        return $this->positionSelector;
295
    }
296
297
    /**
298
     * @param String $positionSelector
299
     */
300
    public function setPositionSelector($positionSelector)
301
    {
302
        $this->positionSelector = $positionSelector;
303
    }
304
305
306
    /**
307
     * @return String
308
     */
309
    public function getSimulatorType()
310
    {
311
        return $this->simulatorType;
312
    }
313
314
    /**
315
     * @param String $simulatorType
316
     */
317
    public function setSimulatorType($simulatorType)
318
    {
319
        $this->simulatorType = $simulatorType;
320
    }
321
}
322