Composite::modifyMeta()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 3
nc 2
nop 1
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * File: Composite.php
7
 *
8
 * @author Bartosz Kubicki [email protected]>
9
 * @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl)
10
 */
11
12
namespace LizardMedia\ProductAttachment\Ui\DataProvider\Product\Form\Modifier;
13
14
use Magento\Catalog\Model\Locator\LocatorInterface;
15
use Magento\Catalog\Model\Product\Type as CatalogType;
16
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
17
use Magento\Downloadable\Model\Product\Type as DownloadableType;
18
use Magento\Ui\DataProvider\Modifier\ModifierFactory;
19
use Magento\Ui\DataProvider\Modifier\ModifierInterface;
20
use function in_array;
21
22
/**
23
 * Class Composite
24
 * @package LizardMedia\ProductAttachment\Ui\DataProvider\Product\Form\Modifier
25
 */
26
class Composite extends AbstractModifier
27
{
28
    /**
29
     * Path elements.
30
     *
31
     * @var string
32
     */
33
    const CHILDREN_PATH = 'product_attachment/children';
34
    const CONTAINER_ATTACHMENTS = 'container_attachments';
35
36
    const CONFIGURABLE_TYPE_CODE = 'configurable';
37
    const GROUPED_TYPE_CODE = 'grouped';
38
39
    /**
40
     * @var array
41
     */
42
    private $modifiers;
43
44
    /**
45
     * @var LocatorInterface
46
     */
47
    private $locator;
48
49
    /**
50
     * @var ModifierFactory
51
     */
52
    private $modifierFactory;
53
54
    /**
55
     * @var ModifierInterface[]
56
     */
57
    private $modifiersInstances = [];
58
59
    /**
60
     * @param LocatorInterface $locator
61
     * @param ModifierFactory $modifierFactory
62
     * @param array $modifiers
63
     */
64
    public function __construct(
65
        LocatorInterface $locator,
66
        ModifierFactory $modifierFactory,
67
        array $modifiers = []
68
    ) {
69
        $this->locator = $locator;
70
        $this->modifierFactory = $modifierFactory;
71
        $this->modifiers = $modifiers;
72
    }
73
74
    /**
75
     * @param array $data
76
     * @return array $data
77
     */
78
    public function modifyData(array $data) : array
79
    {
80
        if ($this->canShowAttachmentPanel()) {
81
            foreach ($this->getModifiers() as $modifier) {
82
                $data = $modifier->modifyData($data);
83
            }
84
        }
85
86
        return $data;
87
    }
88
89
90
    /**
91
     * @param array $meta
92
     * @return array $meta
93
     */
94
    public function modifyMeta(array $meta) : array
95
    {
96
        if ($this->canShowAttachmentPanel()) {
97
            foreach ($this->getModifiers() as $modifier) {
98
                $meta = $modifier->modifyMeta($meta);
99
            }
100
        }
101
102
        return $meta;
103
    }
104
105
106
    /**
107
     * @return ModifierInterface[]
108
     */
109
    private function getModifiers() : array
110
    {
111
        if (empty($this->modifiersInstances)) {
112
            foreach ($this->modifiers as $modifierClass) {
113
                $this->modifiersInstances[$modifierClass] = $this->modifierFactory->create($modifierClass);
114
            }
115
        }
116
117
        return $this->modifiersInstances;
118
    }
119
120
    /**
121
     * @return bool
122
     */
123
    private function canShowAttachmentPanel() : bool
124
    {
125
        $productTypes = [
126
            CatalogType::TYPE_SIMPLE,
127
            CatalogType::TYPE_VIRTUAL,
128
            CatalogType::TYPE_BUNDLE,
129
            self::CONFIGURABLE_TYPE_CODE,
130
            DownloadableType::TYPE_DOWNLOADABLE,
131
            self::GROUPED_TYPE_CODE
132
        ];
133
134
        return in_array((string) $this->locator->getProduct()->getTypeId(), $productTypes, true);
135
    }
136
}
137