1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* File: ContentValidator.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\Data\AttachmentInterface; |
15
|
|
|
use \Magento\Downloadable\Helper\Download; |
16
|
|
|
use \Magento\Downloadable\Model\File\ContentValidator as FileContentValidator; |
17
|
|
|
use \Magento\Framework\Exception\InputException; |
18
|
|
|
use \Magento\Framework\Url\Validator as UrlValidator; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class ContentValidator |
22
|
|
|
* @package LizardMedia\ProductAttachment\Model\Attachment |
23
|
|
|
*/ |
24
|
|
|
class ContentValidator |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var \Magento\Downloadable\Model\File\ContentValidator |
28
|
|
|
*/ |
29
|
|
|
private $fileContentValidator; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var UrlValidator |
34
|
|
|
*/ |
35
|
|
|
private $urlValidator; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param \Magento\Downloadable\Model\File\ContentValidator $fileContentValidator |
40
|
|
|
* @param \Magento\Framework\Url\Validator $urlValidator |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
FileContentValidator $fileContentValidator, |
44
|
|
|
UrlValidator $urlValidator |
45
|
|
|
) { |
46
|
|
|
$this->fileContentValidator = $fileContentValidator; |
47
|
|
|
$this->urlValidator = $urlValidator; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param \LizardMedia\ProductAttachment\Api\Data\AttachmentInterface $attachment |
53
|
|
|
* @param bool $validateAttachmentContent |
54
|
|
|
* |
55
|
|
|
* @throws \Magento\Framework\Exception\InputException |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function isValid(AttachmentInterface $attachment, bool $validateAttachmentContent = true) : bool |
60
|
|
|
{ |
61
|
|
|
if (filter_var($attachment->getSortOrder(), FILTER_VALIDATE_INT) === false |
62
|
|
|
|| $attachment->getSortOrder() < 0) { |
63
|
|
|
throw new InputException(__('Sort order must be a positive integer.')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($validateAttachmentContent) { |
67
|
|
|
$this->validateAttachmentResource($attachment); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param \LizardMedia\ProductAttachment\Api\Data\AttachmentInterface $attachment |
76
|
|
|
* |
77
|
|
|
* @throws \Magento\Framework\Exception\InputException |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
protected function validateAttachmentResource(AttachmentInterface $attachment) : void |
82
|
|
|
{ |
83
|
|
|
$attachmentFile = $attachment->getAttachmentFileContent(); |
84
|
|
|
if ($attachment->getAttachmentType() == Download::LINK_TYPE_FILE |
85
|
|
|
&& (!$attachmentFile || !$this->fileContentValidator->isValid($attachmentFile)) |
86
|
|
|
) { |
87
|
|
|
throw new InputException(__('Provided file content must be valid base64 encoded data.')); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($attachment->getAttachmentType() == Download::LINK_TYPE_URL |
91
|
|
|
&& !$this->urlValidator->isValid($attachment->getAttachmentUrl()) |
92
|
|
|
) { |
93
|
|
|
throw new InputException(__('Attachment URL must have valid format.')); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|