1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* File: InitForSave.php |
7
|
|
|
* |
8
|
|
|
* @author Bartosz Kubicki [email protected]> |
9
|
|
|
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace LizardMedia\ProductAttachment\Plugin; |
13
|
|
|
|
14
|
|
|
use LizardMedia\ProductAttachment\Api\Data\AttachmentFactoryInterface; |
15
|
|
|
use LizardMedia\ProductAttachment\Model\Attachment\Builder as AttachmentBuilder; |
16
|
|
|
use Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper; |
17
|
|
|
use Magento\Catalog\Model\Product; |
18
|
|
|
use Magento\Framework\App\RequestInterface; |
19
|
|
|
use Magento\Framework\Exception\LocalizedException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class InitForSave |
23
|
|
|
* @package LizardMedia\ProductAttachment\Plugin |
24
|
|
|
*/ |
25
|
|
|
class InitForSave |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var AttachmentFactoryInterface |
29
|
|
|
*/ |
30
|
|
|
private $attachmentFactory; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var AttachmentBuilder |
34
|
|
|
*/ |
35
|
|
|
private $attachmentBuilder; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var RequestInterface |
39
|
|
|
*/ |
40
|
|
|
private $request; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param AttachmentFactoryInterface $attachmentFactory |
44
|
|
|
* @param AttachmentBuilder $attachmentBuilder |
45
|
|
|
* @param RequestInterface $request |
46
|
|
|
*/ |
47
|
|
|
public function __construct( |
48
|
|
|
AttachmentFactoryInterface $attachmentFactory, |
49
|
|
|
AttachmentBuilder $attachmentBuilder, |
50
|
|
|
RequestInterface $request |
51
|
|
|
) { |
52
|
|
|
$this->attachmentFactory = $attachmentFactory; |
53
|
|
|
$this->attachmentBuilder = $attachmentBuilder; |
54
|
|
|
$this->request = $request; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Helper $subject |
59
|
|
|
* @param Product $product |
60
|
|
|
* @return Product |
61
|
|
|
* @throws LocalizedException |
62
|
|
|
*/ |
63
|
|
|
public function afterInitialize(Helper $subject, Product $product) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$downloadable = $this->request->getPost('downloadable'); |
66
|
|
|
|
67
|
|
|
if (!empty($downloadable) && (isset($downloadable['attachment']) && is_array($downloadable['attachment']))) { |
68
|
|
|
$product->setDownloadableData($downloadable); |
69
|
|
|
|
70
|
|
|
$attachments = []; |
71
|
|
|
foreach ($downloadable['attachment'] as $attachmentData) { |
72
|
|
|
if (!$attachmentData || |
73
|
|
|
(isset($attachmentData['is_delete']) && (bool) $attachmentData['is_delete'])) { |
74
|
|
|
continue; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$attachments[] = $this->attachmentBuilder->setData($attachmentData) |
78
|
|
|
->build($this->attachmentFactory->create()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->setProductAttachments($product, $attachments); |
82
|
|
|
} else { |
83
|
|
|
$this->setProductAttachments($product, null); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $product; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param Product $product |
91
|
|
|
* @param array|null $attachments |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
|
|
private function setProductAttachments(Product $product, ?array $attachments): void |
95
|
|
|
{ |
96
|
|
|
$extension = $product->getExtensionAttributes(); |
97
|
|
|
$extension->setProductAttachments($attachments); |
98
|
|
|
$product->setExtensionAttributes($extension); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.