DeleteHandler::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * File: DeleteHandler.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 DeleteHandler
19
 * @package LizardMedia\ProductAttachment\Model\Attachment
20
 */
21
class DeleteHandler 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
        foreach ($this->attachmentRepository->getAttachmentsByProduct($entity) as $attachment) {
47
            $this->attachmentRepository->delete((int) $attachment->getId());
48
        }
49
50
        return $entity;
51
    }
52
}
53