Attachments   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 266
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 1
dl 0
loc 266
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A modifyData() 0 9 1
A modifyMeta() 0 40 2
A getDynamicRows() 0 16 1
A getRecord() 0 35 1
A getTitleColumn() 0 22 1
B getAttachmentColumn() 0 61 1
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * File: Attachements.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 LizardMedia\ProductAttachment\Model\Attachment;
15
use LizardMedia\ProductAttachment\Ui\DataProvider\Product\Form\Modifier\Data\Attachments as AttachmentsData;
16
use Magento\Catalog\Model\Locator\LocatorInterface;
17
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
18
use Magento\Downloadable\Model\Source\TypeUpload;
19
use Magento\Framework\Stdlib\ArrayManager;
20
use Magento\Framework\UrlInterface;
21
use Magento\Store\Model\StoreManagerInterface;
22
use Magento\Ui\Component\Container;
23
use Magento\Ui\Component\DynamicRows;
24
use Magento\Ui\Component\Form;
25
26
/**
27
 * Class Attachments
28
 * @package LizardMedia\ProductAttachment\Ui\DataProvider\Product\Form\Modifier
29
 */
30
class Attachments extends AbstractModifier
31
{
32
    /**
33
     * @var AttachmentsData
34
     */
35
    private $attachmentsData;
36
37
    /**
38
     * @var LocatorInterface
39
     */
40
    private $locator;
41
42
    /**
43
     * @var TypeUpload
44
     */
45
    private $typeUpload;
46
47
    /**
48
     * @var ArrayManager
49
     */
50
    private $arrayManager;
51
52
    /**
53
     * @var UrlInterface
54
     */
55
    private $urlBuilder;
56
57
    /**
58
     * @var StoreManagerInterface
59
     */
60
    private $storeManager;
61
62
    /**
63
     * @param AttachmentsData $attachmentsData
64
     * @param LocatorInterface $locator
65
     * @param TypeUpload $typeUpload
66
     * @param ArrayManager $arrayManager
67
     * @param UrlInterface $urlBuilder
68
     * @param StoreManagerInterface $storeManager
69
     */
70
    public function __construct(
71
        AttachmentsData $attachmentsData,
72
        LocatorInterface $locator,
73
        TypeUpload $typeUpload,
74
        ArrayManager $arrayManager,
75
        UrlInterface $urlBuilder,
76
        StoreManagerInterface $storeManager
77
    ) {
78
        $this->attachmentsData = $attachmentsData;
79
        $this->locator = $locator;
80
        $this->typeUpload = $typeUpload;
81
        $this->arrayManager = $arrayManager;
82
        $this->urlBuilder = $urlBuilder;
83
        $this->storeManager = $storeManager;
84
    }
85
86
    /**
87
     * @param array $data
88
     * @return array $data
89
     */
90
    public function modifyData(array $data) : array
91
    {
92
        $model = $this->locator->getProduct();
93
94
        $data[$model->getId()][self::DATA_SOURCE_DEFAULT]['attachments_title'] = $this->attachmentsData->getAttachmentsTitle();
95
        $data[$model->getId()]['downloadable']['attachment'] = $this->attachmentsData->getAttachmentsData();
96
97
        return $data;
98
    }
99
100
    /**
101
     * @param array $meta
102
     * @return array $meta
103
     */
104
    public function modifyMeta(array $meta) : array
105
    {
106
        $attachmentsPath = Composite::CHILDREN_PATH . '/' .  Composite::CONTAINER_ATTACHMENTS;
107
        $attachmentsContainer['arguments']['data']['config'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$attachmentsContainer was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attachmentsContainer = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
108
            'additionalClasses' => 'admin__fieldset-section',
109
            'componentType' => Form\Fieldset::NAME,
110
            'label' => __('Attachments'),
111
            'dataScope' => '',
112
            'visible' => true,
113
            'sortOrder' => 10,
114
        ];
115
116
        $attachmentsTitle['arguments']['data']['config'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$attachmentsTitle was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attachmentsTitle = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
117
            'componentType' => Form\Field::NAME,
118
            'formElement' => Form\Element\Input::NAME,
119
            'dataType' => Form\Element\DataType\Text::NAME,
120
            'label' => __('Title'),
121
            'dataScope' => 'product.attachments_title',
122
            'scopeLabel' => $this->storeManager->isSingleStoreMode() ? '' : '[STORE VIEW]',
123
        ];
124
125
        $informationAttachments['arguments']['data']['config'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$informationAttachments was never initialized. Although not strictly required by PHP, it is generally a good practice to add $informationAttachments = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
126
            'componentType' => Container::NAME,
127
            'component' => 'Magento_Ui/js/form/components/html',
128
            'additionalClasses' => 'admin__fieldset-note',
129
            'content' => __('Alphanumeric, dash and underscore characters are recommended for filenames. Improper characters are replaced with \'_\'.'),
130
        ];
131
132
        $attachmentsContainer = $this->arrayManager->set(
133
            'children',
134
            $attachmentsContainer,
135
            [
136
                'attachments_title' => $attachmentsTitle,
137
                'attachment' => $this->getDynamicRows(),
138
                'information_attachments' => $informationAttachments,
139
            ]
140
        );
141
142
        return $this->arrayManager->set($attachmentsPath, $meta, $attachmentsContainer);
143
    }
144
145
    /**
146
     * @return array
147
     */
148
    private function getDynamicRows() : array
149
    {
150
        $dynamicRows['arguments']['data']['config'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$dynamicRows was never initialized. Although not strictly required by PHP, it is generally a good practice to add $dynamicRows = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
151
            'addButtonLabel' => __('Add attachment'),
152
            'componentType' => DynamicRows::NAME,
153
            'itemTemplate' => 'record',
154
            'renderDefaultRecord' => false,
155
            'columnsHeader' => true,
156
            'additionalClasses' => 'admin__field-wide',
157
            'dataScope' => 'downloadable',
158
            'deleteProperty'=> 'is_delete',
159
            'deleteValue' => '1',
160
        ];
161
162
        return $this->arrayManager->set('children/record', $dynamicRows, $this->getRecord());
163
    }
164
165
166
    /**
167
     * @return array
168
     */
169
    private function getRecord() : array
170
    {
171
        $record['arguments']['data']['config'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$record was never initialized. Although not strictly required by PHP, it is generally a good practice to add $record = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
172
            'componentType' => Container::NAME,
173
            'isTemplate' => true,
174
            'is_collection' => true,
175
            'component' => 'Magento_Ui/js/dynamic-rows/record',
176
            'dataScope' => '',
177
        ];
178
179
        $recordPosition['arguments']['data']['config'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$recordPosition was never initialized. Although not strictly required by PHP, it is generally a good practice to add $recordPosition = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
180
            'componentType' => Form\Field::NAME,
181
            'formElement' => Form\Element\Input::NAME,
182
            'dataType' => Form\Element\DataType\Number::NAME,
183
            'dataScope' => 'sort_order',
184
            'visible' => false,
185
        ];
186
187
        $recordActionDelete['arguments']['data']['config'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$recordActionDelete was never initialized. Although not strictly required by PHP, it is generally a good practice to add $recordActionDelete = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
188
            'label' => null,
189
            'componentType' => 'actionDelete',
190
            'fit' => true,
191
        ];
192
193
        return $this->arrayManager->set(
194
            'children',
195
            $record,
196
            [
197
                'container_attachment_title' => $this->getTitleColumn(),
198
                'container_attachments' => $this->getAttachmentColumn(),
199
                'position' => $recordPosition,
200
                'action_delete' => $recordActionDelete,
201
            ]
202
        );
203
    }
204
205
    /**
206
     * @return array
207
     */
208
    private function getTitleColumn() : array
209
    {
210
        $titleContainer['arguments']['data']['config'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$titleContainer was never initialized. Although not strictly required by PHP, it is generally a good practice to add $titleContainer = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
211
            'componentType' => Container::NAME,
212
            'formElement' => Container::NAME,
213
            'component' => 'Magento_Ui/js/form/components/group',
214
            'label' => __('Title'),
215
            'dataScope' => '',
216
        ];
217
218
        $titleField['arguments']['data']['config'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$titleField was never initialized. Although not strictly required by PHP, it is generally a good practice to add $titleField = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
219
            'formElement' => Form\Element\Input::NAME,
220
            'componentType' => Form\Field::NAME,
221
            'dataType' => Form\Element\DataType\Text::NAME,
222
            'dataScope' => 'title',
223
            'validation' => [
224
                'required-entry' => true,
225
            ],
226
        ];
227
228
        return $this->arrayManager->set('children/attachment_title', $titleContainer, $titleField);
229
    }
230
231
    /**
232
     * @return array
233
     */
234
    private function getAttachmentColumn() : array
235
    {
236
        $attachmentContainer['arguments']['data']['config'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$attachmentContainer was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attachmentContainer = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
237
            'componentType' => Container::NAME,
238
            'formElement' => Container::NAME,
239
            'component' => 'Magento_Ui/js/form/components/group',
240
            'label' => __('File'),
241
            'dataScope' => '',
242
        ];
243
244
        $attachmentType['arguments']['data']['config'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$attachmentType was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attachmentType = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
245
            'formElement' => Form\Element\Select::NAME,
246
            'componentType' => Form\Field::NAME,
247
            'component' => 'Magento_Downloadable/js/components/upload-type-handler',
248
            'dataType' => Form\Element\DataType\Text::NAME,
249
            'dataScope' => Attachment::ATTACHMENT_TYPE,
250
            'options' => $this->typeUpload->toOptionArray(),
251
            'typeFile' => 'attachment_file',
252
            'typeUrl' => 'attachment_url',
253
        ];
254
255
        $attachmentUrl['arguments']['data']['config'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$attachmentUrl was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attachmentUrl = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
256
            'formElement' => Form\Element\Input::NAME,
257
            'componentType' => Form\Field::NAME,
258
            'dataType' => Form\Element\DataType\Text::NAME,
259
            'dataScope' => 'attachment_url',
260
            'placeholder' => 'URL',
261
            'validation' => [
262
                'required-entry' => true,
263
                'validate-url' => true,
264
            ],
265
        ];
266
267
        $attachmentUploader['arguments']['data']['config'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$attachmentUploader was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attachmentUploader = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
268
            'formElement' => 'fileUploader',
269
            'componentType' => 'fileUploader',
270
            'component' => 'LizardMedia_ProductAttachment/js/components/file-uploader',
271
            'elementTmpl' => 'Magento_Downloadable/components/file-uploader',
272
            'fileInputName' => 'attachments',
273
            'uploaderConfig' => [
274
                'url' => $this->urlBuilder->getUrl(
275
                    'downloadable/attachment_file/upload',
276
                    [Attachment::ATTACHMENT_TYPE => 'attachments', '_secure' => true]
277
                ),
278
            ],
279
            'dataScope' => 'file',
280
            'validation' => [
281
                'required-entry' => true,
282
            ],
283
        ];
284
285
        return $this->arrayManager->set(
286
            'children',
287
            $attachmentContainer,
288
            [
289
                'attachment_type' => $attachmentType,
290
                'attachment_url' => $attachmentUrl,
291
                'attachment_file' => $attachmentUploader,
292
            ]
293
        );
294
    }
295
}
296