Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ValidatorInfo.php
Go to the documentation of this file.
1 <?php
8 
12 class ValidatorInfo extends Validator
13 {
18 
22  protected $validateFactory;
23 
27  protected $useQuotePath;
28 
32  protected $fileFullPath;
33 
37  protected $fileRelativePath;
38 
48  public function __construct(
50  \Magento\Framework\Filesystem $filesystem,
51  \Magento\Framework\File\Size $fileSize,
52  \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDatabase,
54  ) {
55  $this->coreFileStorageDatabase = $coreFileStorageDatabase;
56  $this->validateFactory = $validateFactory;
57  parent::__construct($scopeConfig, $filesystem, $fileSize);
58  }
59 
66  public function setUseQuotePath($useQuotePath)
67  {
68  $this->useQuotePath = $useQuotePath;
69  return $this;
70  }
71 
80  public function validate($optionValue, $option)
81  {
82  if (!is_array($optionValue)) {
83  return false;
84  }
85 
86  $this->fileFullPath = null;
87  $this->fileRelativePath = null;
88  $this->initFilePath($optionValue);
89 
90  if ($this->fileFullPath === null) {
91  return false;
92  }
93 
94  $validatorChain = $this->validateFactory->create();
95  try {
96  $validatorChain = $this->buildImageValidator($validatorChain, $option, $this->fileFullPath);
97  } catch (\Magento\Framework\Exception\InputException $notImage) {
98  return false;
99  }
100 
101  $result = false;
102  if ($validatorChain->isValid($this->fileFullPath, $optionValue['title'])) {
103  $result = $this->rootDirectory->isReadable($this->fileRelativePath)
104  && isset($optionValue['secret_key'])
105  && $this->buildSecretKey($this->fileRelativePath) == $optionValue['secret_key'];
106  } elseif ($validatorChain->getErrors()) {
107  $errors = $this->getValidatorErrors($validatorChain->getErrors(), $optionValue, $option);
108 
109  if (count($errors) > 0) {
110  throw new \Magento\Framework\Exception\LocalizedException(__(implode("\n", $errors)));
111  }
112  } else {
113  throw new \Magento\Framework\Exception\LocalizedException(
114  __("The product's required option(s) weren't entered. Make sure the options are entered and try again.")
115  );
116  }
117  return $result;
118  }
119 
126  protected function buildSecretKey($fileRelativePath)
127  {
128  return substr(md5($this->rootDirectory->readFile($fileRelativePath)), 0, 20);
129  }
130 
137  protected function initFilePath($optionValue)
138  {
145  $checkPaths = [];
146  if (isset($optionValue['quote_path'])) {
147  $checkPaths[] = $optionValue['quote_path'];
148  }
149  if (isset($optionValue['order_path']) && !$this->useQuotePath) {
150  $checkPaths[] = $optionValue['order_path'];
151  }
152 
153  foreach ($checkPaths as $path) {
154  if (!$this->rootDirectory->isFile($path)) {
155  if (!$this->coreFileStorageDatabase->saveFileToFilesystem($path)) {
156  continue;
157  }
158  }
159  $this->fileFullPath = $this->rootDirectory->getAbsolutePath($path);
160  $this->fileRelativePath = $path;
161  break;
162  }
163  }
164 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
buildImageValidator($object, $option, $fileFullPath=null)
Definition: Validator.php:142
__construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Filesystem $filesystem, \Magento\Framework\File\Size $fileSize, \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDatabase, \Magento\Catalog\Model\Product\Option\Type\File\ValidateFactory $validateFactory)
$filesystem
$errors
Definition: overview.phtml:9