Passed
Push — master ( 676080...29b226 )
by Bartosz
04:35 queued 02:45
created

TypeConvertingPrevention::getProductType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * File: TypeConvertingPrevention.php
7
 *
8
 * @author Bartosz Kubicki [email protected]>
9
 * @copyright Copyright (C) 2021 Lizard Media (http://lizardmedia.pl)
10
 */
11
12
namespace LizardMedia\ProductAttachment\Observer\Adminhtml;
13
14
use Magento\Framework\App\RequestInterface;
15
use Magento\Framework\Event\Observer;
16
use Magento\Framework\Event\ObserverInterface;
17
18
/**
19
 * Class TypeConvertingPrevention
20
 * @package LizardMedia\ProductAttachment\Observer\Adminhtml
21
 */
22
class TypeConvertingPrevention implements ObserverInterface
23
{
24
    /**
25
     * @var RequestInterface
26
     */
27
    private $request;
28
29
    /**
30
     * @param RequestInterface $request
31
     */
32
    public function __construct(
33
        RequestInterface $request
34
    ) {
35
        $this->request = $request;
36
    }
37
38
    /**
39
     * @param Observer $observer
40
     * @return void
41
     */
42
    public function execute(Observer $observer): void
43
    {
44
        $product = $observer->getEvent()->getProduct();
45
        $type = $this->request->getParam('type');
46
47
        if (!empty($type) && in_array($type, $this->getProductType(), true)) {
48
            $product->setTypeId($type);
49
        }
50
    }
51
52
    /**
53
     * @return string[]
54
     */
55
    private function getProductType(): array
56
    {
57
        return ['bundle', 'grouped'];
58
    }
59
}
60