AttachmentTab   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 2
dl 0
loc 85
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A getAttachments() 0 8 2
A getProduct() 0 4 1
A getDownloadUrl() 0 7 1
A getIsOpenInNewWindow() 0 4 1
A getIdentities() 0 4 1
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * File: Attachments.php
7
 *
8
 * @author Bartosz Kubicki [email protected]>
9
 * @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl)
10
 */
11
12
namespace LizardMedia\ProductAttachment\Block\Product;
13
14
use LizardMedia\ProductAttachment\Api\AttachmentRepositoryInterface;
15
use LizardMedia\ProductAttachment\Api\Data\AttachmentInterface;
16
use LizardMedia\ProductAttachment\Api\SettingsInterface;
17
use LizardMedia\ProductAttachment\Model\Attachment;
18
use Magento\Catalog\Api\Data\ProductInterface;
19
use Magento\Framework\DataObject\IdentityInterface;
20
use Magento\Framework\Registry;
21
use Magento\Framework\View\Element\Template;
22
use Magento\Framework\View\Element\Template\Context;
23
24
/**
25
 * Class Attachments
26
 * @package LizardMedia\ProductAttachment\Block\Product
27
 */
28
class AttachmentTab extends Template implements IdentityInterface
29
{
30
    /**
31
     * @var AttachmentRepositoryInterface
32
     */
33
    private $attachmentRepository;
34
35
    /**
36
     * @var SettingsInterface
37
     */
38
    private $settings;
39
40
    /**
41
     * @var Registry
42
     */
43
    private $registry;
44
45
    /**
46
     * @param AttachmentRepositoryInterface $attachmentRepository
47
     * @param SettingsInterface $settings
48
     * @param Context $context
49
     * @param Registry $registry
50
     * @param array $data
51
     */
52
    public function __construct(
53
        AttachmentRepositoryInterface $attachmentRepository,
54
        SettingsInterface $settings,
55
        Context $context,
56
        Registry $registry,
57
        array $data = []
58
    ) {
59
        parent::__construct($context, $data);
60
        $this->attachmentRepository = $attachmentRepository;
61
        $this->settings = $settings;
62
        $this->registry = $registry;
63
    }
64
65
    /**
66
     * @return AttachmentInterface[]
67
     */
68
    public function getAttachments() : array
69
    {
70
        if ($product = $this->getProduct()) {
71
            return $this->attachmentRepository->getAttachmentsByProduct($product);
72
        }
73
74
        return [];
75
    }
76
77
    /**
78
     * @return ProductInterface
79
     */
80
    public function getProduct(): ProductInterface
81
    {
82
        return $this->registry->registry('current_product');
83
    }
84
85
    /**
86
     * @param AttachmentInterface $attachment
87
     * @return string
88
     */
89
    public function getDownloadUrl(AttachmentInterface $attachment): string
90
    {
91
        return $this->getUrl(
92
            'downloadable/download/attachment',
93
            ['id' => $attachment->getId(), '_secure' => true]
94
        );
95
    }
96
97
    /**
98
     * @return bool
99
     */
100
    public function getIsOpenInNewWindow(): bool
101
    {
102
        return $this->settings->areLinksOpenedInNewWindow();
103
    }
104
105
    /**
106
     * @return array
107
     */
108
    public function getIdentities(): array
109
    {
110
        return [Attachment::CACHE_TAG];
111
    }
112
}
113