ReadHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * File: ReadHandler.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 ReadHandler
19
 * @package LizardMedia\ProductAttachment\Model\Attachment
20
 */
21
class ReadHandler 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
        $entityExtension = $entity->getExtensionAttributes();
47
        $attachments = $this->attachmentRepository->getAttachmentsByProduct($entity);
48
49
        if ($attachments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $attachments of type LizardMedia\ProductAttac...a\AttachmentInterface[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
50
            $entityExtension->setProductAttachments($attachments);
51
        }
52
53
        $entity->setExtensionAttributes($entityExtension);
54
        return $entity;
55
    }
56
}
57