CreateHandler::execute()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 4
nop 2
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * File: CreateHandler.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 \Magento\Framework\EntityManager\Operation\ExtensionInterface;
16
17
/**
18
 * Class CreateHandler
19
 * @package LizardMedia\ProductAttachment\Model\Attachment
20
 */
21
class CreateHandler implements ExtensionInterface
22
{
23
    /**
24
     * @var \LizardMedia\ProductAttachment\Api\AttachmentRepositoryInterface
25
     */
26
    private $attachmentRepository;
27
28
29
    /**
30
     * @param \LizardMedia\ProductAttachment\Api\AttachmentRepositoryInterface $attachmentRepository
31
     */
32
    public function __construct(AttachmentRepositoryInterface $attachmentRepository)
33
    {
34
        $this->attachmentRepository = $attachmentRepository;
35
    }
36
37
38
    /**
39
     * @param object $entity
40
     * @param array $arguments
41
     *
42
     * @return \Magento\Catalog\Api\Data\ProductInterface|object $entity
43
     */
44
    public function execute($entity, $arguments = [])
45
    {
46
        /** @var $attachments \LizardMedia\ProductAttachment\Api\Data\AttachmentInterface[] */
47
        $attachments = $entity->getExtensionAttributes()->getProductAttachments() ?: [];
48
49
        foreach ($attachments as $attachment) {
50
            $attachment->setId(null);
51
            $this->attachmentRepository->save($entity->getSku(), $attachment, !$entity->getStoreId());
52
        }
53
54
        return $entity;
55
    }
56
}
57