|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* File: UpdateHandler.php |
|
7
|
|
|
* |
|
8
|
|
|
* @author Bartosz Kubicki [email protected]> |
|
9
|
|
|
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace LizardMedia\ProductAttachment\Model\Attachment; |
|
13
|
|
|
|
|
14
|
|
|
use LizardMedia\ProductAttachment\Api\AttachmentRepositoryInterface; |
|
15
|
|
|
use LizardMedia\ProductAttachment\Api\Data\AttachmentInterface; |
|
16
|
|
|
use Magento\Catalog\Api\Data\ProductInterface; |
|
17
|
|
|
use Magento\Framework\EntityManager\Operation\ExtensionInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class UpdateHandler |
|
21
|
|
|
* @package LizardMedia\ProductAttachment\Model\Attachment |
|
22
|
|
|
*/ |
|
23
|
|
|
class UpdateHandler implements ExtensionInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var AttachmentRepositoryInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $attachmentRepository; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param AttachmentRepositoryInterface $attachmentRepository |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(AttachmentRepositoryInterface $attachmentRepository) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->attachmentRepository = $attachmentRepository; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param object $entity |
|
40
|
|
|
* @param array $arguments |
|
41
|
|
|
* @return ProductInterface|object $entity |
|
42
|
|
|
*/ |
|
43
|
|
|
public function execute($entity, $arguments = []) |
|
44
|
|
|
{ |
|
45
|
|
|
$attachments = $entity->getExtensionAttributes()->getProductAttachments() ?: []; |
|
46
|
|
|
/** @var $attachments AttachmentInterface[] */ |
|
47
|
|
|
|
|
48
|
|
|
$updatedAttachments = []; |
|
49
|
|
|
$oldAttachments = $this->attachmentRepository->getAttachmentsByProduct($entity); |
|
50
|
|
|
|
|
51
|
|
|
foreach ($attachments as $attachment) { |
|
52
|
|
|
if ($attachment->getId()) { |
|
53
|
|
|
$updatedAttachments[(int) $attachment->getId()] = true; |
|
54
|
|
|
} |
|
55
|
|
|
$this->attachmentRepository->save($entity->getSku(), $attachment, !(bool) $entity->getStoreId()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** @var ProductInterface $entity */ |
|
59
|
|
|
foreach ($oldAttachments as $attachment) { |
|
60
|
|
|
if (!isset($updatedAttachments[(int) $attachment->getId()])) { |
|
61
|
|
|
$this->attachmentRepository->delete((int) $attachment->getId()); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $entity; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|