Attachment::getUrl()   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 0
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * File: Attachment.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;
13
14
use LizardMedia\ProductAttachment\Api\Data\AttachmentExtensionInterface;
15
use LizardMedia\ProductAttachment\Api\Data\AttachmentInterface;
16
use LizardMedia\ProductAttachment\Model\ResourceModel\Attachment as AttachmentResource;
17
use Magento\Downloadable\Api\Data\File\ContentInterface;
18
use Magento\Downloadable\Model\ComponentInterface;
19
use Magento\Framework\DataObject\IdentityInterface;
20
use Magento\Framework\Exception\LocalizedException;
21
use Magento\Framework\Model\AbstractExtensibleModel;
22
23
/**
24
 * Class Attachment
25
 * @package LizardMedia\ProductAttachment\Model
26
 */
27
class Attachment extends AbstractExtensibleModel implements AttachmentInterface, ComponentInterface, IdentityInterface
28
{
29
    /**
30
     * @var string
31
     */
32
    const CACHE_TAG = 'lizardmedia_productattachment_attachment';
33
34
    /**
35
     * Tables
36
     *
37
     * @var string
38
     */
39
    const MAIN_TABLE = 'lizardmedia_product_attachment';
40
    const TITLE_TABLE = 'lizardmedia_product_attachment_title';
41
42
    /**
43
     * Main model field names
44
     *
45
     * @var string
46
     */
47
    const ID = 'id';
48
    const PRODUCT_ID = 'product_id';
49
    const SORT_ORDER = 'sort_order';
50
    const ATTACHMENT_TYPE = 'attachment_type';
51
    const ATTACHMENT_FILE = 'attachment_file';
52
    const ATTACHMENT_FILE_CONTENT = 'attachment_file_content';
53
    const ATTACHMENT_URL = 'attachment_url';
54
55
56
    /**
57
     * Title field names
58
     *
59
     * @var string
60
     */
61
    const TITLE_ID = 'id';
62
    const ATTACHMENT_ID = 'attachment_id';
63
    const TITLE = 'title';
64
    const STORE_ID = 'store_id';
65
66
    /**
67
     * @return void
68
     */
69
    protected function _construct()
70
    {
71
        $this->_init(AttachmentResource::class);
72
        parent::_construct();
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    public function getIdentities(): array
79
    {
80
        return [self::CACHE_TAG . '_' . $this->getId()];
81
    }
82
83
    /**
84
     * @return $this
85
     */
86
    public function afterSave()
87
    {
88
        $this->getResource()->saveItemTitle($this);
89
        return parent::afterSave();
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getUrl()
96
    {
97
        if ($this->getAttachmentUrl()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->getAttachmentUrl() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
98
            return $this->getAttachmentUrl();
99
        } else {
100
            return $this->getAttachmentFile();
101
        }
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function getBaseTmpPath(): string
108
    {
109
        return 'downloadable/tmp/attachment';
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public function getBasePath(): string
116
    {
117
        return 'downloadable/files/attachment';
118
    }
119
120
    /**
121
     * @param int $productId
122
     * @param int $storeId
123
     * @return array
124
     * @throws LocalizedException
125
     */
126
    public function getSearchableData($productId, $storeId)
127
    {
128
        return $this->_getResource()->getSearchableData($productId, $storeId);
129
    }
130
131
    /**
132
     * @return int
133
     */
134
    public function getProductId() : int
135
    {
136
        return (int) $this->getData(self::PRODUCT_ID);
137
    }
138
139
    /**
140
     * @param int $id
141
     * @return AttachmentInterface
142
     */
143
    public function setProductId(int $id) : AttachmentInterface
144
    {
145
        return $this->setData(self::PRODUCT_ID, $id);
146
    }
147
148
    /**
149
     * @return string
150
     */
151
    public function getTitle() : string
152
    {
153
        return (string) $this->getData(self::TITLE);
154
    }
155
156
    /**
157
     * @param string $title
158
     * @return AttachmentInterface
159
     */
160
    public function setTitle(string $title) : AttachmentInterface
161
    {
162
        return $this->setData(self::TITLE, $title);
163
    }
164
165
    /**
166
     * @return int
167
     */
168
    public function getSortOrder() : int
169
    {
170
        return (int) $this->getData(self::SORT_ORDER);
171
    }
172
173
    /**
174
     * @param int $sortOrder
175
     * @return AttachmentInterface
176
     */
177
    public function setSortOrder(int $sortOrder) : AttachmentInterface
178
    {
179
        return $this->setData(self::SORT_ORDER, $sortOrder);
180
    }
181
182
    /**
183
     * @return string
184
     */
185
    public function getAttachmentType() : string
186
    {
187
        return (string) $this->getData(self::ATTACHMENT_TYPE);
188
    }
189
190
    /**
191
     * @param string $attachmentType
192
     * @return AttachmentInterface
193
     */
194
    public function setAttachmentType(string $attachmentType) : AttachmentInterface
195
    {
196
        return $this->setData(self::ATTACHMENT_TYPE, $attachmentType);
197
    }
198
199
    /**
200
     * Relative file path
201
     * @return string|null
202
     */
203
    public function getAttachmentFile() : ?string
204
    {
205
        return $this->getData(self::ATTACHMENT_FILE);
206
    }
207
208
    /**
209
     * @param string $attachmentFile
210
     * @return AttachmentInterface
211
     */
212
    public function setAttachmentFile(string $attachmentFile) : AttachmentInterface
213
    {
214
        return $this->setData(self::ATTACHMENT_FILE, $attachmentFile);
215
    }
216
217
    /**
218
     * @return ContentInterface|null
219
     */
220
    public function getAttachmentFileContent() : ?ContentInterface
221
    {
222
        return $this->getData(self::ATTACHMENT_FILE_CONTENT);
223
    }
224
225
    /**
226
     * @param ContentInterface $attachmentFileContent
227
     * @return AttachmentInterface
228
     */
229
    public function setAttachmentFileContent(ContentInterface $attachmentFileContent = null) : AttachmentInterface
230
    {
231
        return $this->setData(self::ATTACHMENT_FILE_CONTENT, $attachmentFileContent);
232
    }
233
234
    /**
235
     * @return string|null
236
     */
237
    public function getAttachmentUrl() : ?string
238
    {
239
        return $this->getData(self::ATTACHMENT_URL);
240
    }
241
242
    /**
243
     * @param string $attachmentUrl
244
     * @return AttachmentInterface
245
     */
246
    public function setAttachmentUrl(string $attachmentUrl) : AttachmentInterface
247
    {
248
        return $this->setData(self::ATTACHMENT_URL, $attachmentUrl);
249
    }
250
251
    /**
252
     * @return AttachmentExtensionInterface | null
253
     */
254
    public function getExtensionAttributes() : ?AttachmentExtensionInterface
255
    {
256
        return $this->_getExtensionAttributes();
257
    }
258
259
    /**
260
     * @param AttachmentExtensionInterface $extensionAttributes
261
     * @return AttachmentInterface
262
     */
263
    public function setExtensionAttributes(AttachmentExtensionInterface $extensionAttributes) : AttachmentInterface
264
    {
265
        return $this->_setExtensionAttributes($extensionAttributes);
266
    }
267
}
268