1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* File: DownloadResourceResolver.php |
7
|
|
|
* |
8
|
|
|
* @author Bartosz Kubicki [email protected]> |
9
|
|
|
* @copyright Copyright (C) 2020 Lizard Media (http://lizardmedia.pl) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace LizardMedia\ProductAttachment\Controller; |
13
|
|
|
|
14
|
|
|
use LizardMedia\ProductAttachment\Api\Data\AttachmentInterface; |
15
|
|
|
use Magento\Downloadable\Helper\Download as DownloadHelper; |
16
|
|
|
use Magento\Downloadable\Helper\File as FileHelper; |
17
|
|
|
use Magento\Framework\App\Filesystem\DirectoryList; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class DownloadResourceResolver |
21
|
|
|
* @package LizardMedia\ProductAttachment\Controller |
22
|
|
|
*/ |
23
|
|
|
class DownloadResourceResolver |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var FileHelper |
27
|
|
|
*/ |
28
|
|
|
private $fileHelper; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var DirectoryList |
32
|
|
|
*/ |
33
|
|
|
private $directoryList; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* DownloadResourceResolver constructor. |
37
|
|
|
* @param FileHelper $fileHelper |
38
|
|
|
* @param DirectoryList $directoryList |
39
|
|
|
*/ |
40
|
|
|
public function __construct(FileHelper $fileHelper, DirectoryList $directoryList) |
41
|
|
|
{ |
42
|
|
|
$this->fileHelper = $fileHelper; |
43
|
|
|
$this->directoryList = $directoryList; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param AttachmentInterface $attachment |
48
|
|
|
* @return string |
49
|
|
|
* @codeCoverageIgnore |
50
|
|
|
*/ |
51
|
|
|
public function resolveResourceType(AttachmentInterface $attachment): string |
52
|
|
|
{ |
53
|
|
|
return $attachment->getAttachmentType(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param AttachmentInterface $attachment |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function resolveResource(AttachmentInterface $attachment): string |
61
|
|
|
{ |
62
|
|
|
return $attachment->getAttachmentType() === DownloadHelper::LINK_TYPE_URL |
63
|
|
|
? $attachment->getAttachmentUrl() |
64
|
|
|
: $this->buildFullFilePath($attachment); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param AttachmentInterface $attachment |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
private function buildFullFilePath(AttachmentInterface $attachment): string |
72
|
|
|
{ |
73
|
|
|
return (string) $this->fileHelper->getFilePath( |
74
|
|
|
implode( |
75
|
|
|
DIRECTORY_SEPARATOR, |
76
|
|
|
[$this->directoryList->getPath(DirectoryList::MEDIA), $attachment->getBasePath()] |
77
|
|
|
), |
78
|
|
|
$attachment->getAttachmentFile() |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
} |