AttachmentList::getIsOpenInNewWindow()   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 0
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * File: AttachmentList.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\Customer\Account;
13
14
use LizardMedia\ProductAttachment\Api\Data\AttachmentInterface;
15
use LizardMedia\ProductAttachment\Api\PurchasedItemsAttachmentProviderInterface;
16
use LizardMedia\ProductAttachment\Api\SettingsInterface;
17
use LizardMedia\ProductAttachment\Model\ResourceModel\Attachment\Collection;
18
use Magento\Customer\Helper\Session\CurrentCustomer;
19
use Magento\Framework\Message\ManagerInterface;
20
use Magento\Framework\View\Element\Template;
21
use Magento\Framework\View\Element\Template\Context;
22
23
/**
24
 * Class Attachment
25
 * @package LizardMedia\ProductAttachment\Block\Customer\Account
26
 */
27
class AttachmentList extends Template
28
{
29
    /**
30
     * @var bool
31
     */
32
    private $isCollectionLoaded = false;
33
34
    /**
35
     * @var PurchasedItemsAttachmentProviderInterface
36
     */
37
    private $purchasedItemsAttachmentProvider;
38
39
    /**
40
     * @var SettingsInterface
41
     */
42
    private $settings;
43
44
    /**
45
     * @var Collection
46
     */
47
    private $collection;
48
49
    /**
50
     * @var CurrentCustomer
51
     */
52
    private $currentCustomer;
53
54
    /**
55
     * @var ManagerInterface
56
     */
57
    private $messageManager;
58
59
    /**
60
     * @param PurchasedItemsAttachmentProviderInterface $purchasedItemsAttachmentProvider
61
     * @param SettingsInterface $settings
62
     * @param CurrentCustomer $currentCustomer
63
     * @param Context $context
64
     * @param ManagerInterface $messageManager
65
     * @param array $data
66
     */
67
    public function __construct(
68
        PurchasedItemsAttachmentProviderInterface $purchasedItemsAttachmentProvider,
69
        SettingsInterface $settings,
70
        CurrentCustomer $currentCustomer,
71
        Context $context,
72
        ManagerInterface $messageManager,
73
        array $data = []
74
    ) {
75
        parent::__construct($context, $data);
76
        $this->purchasedItemsAttachmentProvider = $purchasedItemsAttachmentProvider;
77
        $this->settings = $settings;
78
        $this->currentCustomer = $currentCustomer;
79
        $this->messageManager = $messageManager;
80
    }
81
82
    /**
83
     * @return $this
84
     */
85
    protected function _prepareLayout()
86
    {
87
        parent::_prepareLayout();
88
89
        if ($attachments = $this->getAttachments()) {
90
            $this->getChildBlock('lizard_attachment_list_pager')
91
                ->setCollection($attachments)
92
                ->setPath('downloadable/customer/attachment_index');
93
        }
94
        return $this;
95
    }
96
97
    /**
98
     * @return Collection
99
     */
100
    public function getAttachments() : Collection
101
    {
102
        $customer = $this->currentCustomer->getCustomer();
103
104
        if (!$this->collection && $this->isCollectionLoaded === false) {
105
            try {
106
                $this->collection = $this->purchasedItemsAttachmentProvider->get($customer);
107
                $this->isCollectionLoaded = true;
108
            } catch (\Exception $e) {
109
                $this->messageManager->addErrorMessage(
110
                    __('Something went wrong, please contact the store support')
111
                );
112
            }
113
        }
114
115
        return $this->collection;
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    public function getBackUrl() : string
122
    {
123
        if ($this->getRefererUrl()) {
124
            return $this->getRefererUrl();
125
        }
126
127
        return $this->getUrl('customer/account/');
128
    }
129
130
    /**
131
     * @param AttachmentInterface $attachment
132
     * @return string
133
     */
134
    public function getDownloadUrl(AttachmentInterface $attachment) : string
135
    {
136
        return $this->getUrl(
137
            'downloadable/download/attachment',
138
            ['id' => $attachment->getId(), '_secure' => true]
139
        );
140
    }
141
142
    /**
143
     * @return bool
144
     */
145
    public function getIsOpenInNewWindow() : bool
146
    {
147
        return $this->settings->areLinksOpenedInNewWindow();
148
    }
149
}
150