_certFactory = $certFactory; $this->_encryptor = $encryptor; $this->_tmpDirectory = $filesystem->getDirectoryRead(DirectoryList::SYS_TMP); parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); } /** * Process additional data before save config * * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function beforeSave() { $value = $this->getValue(); if (!empty($value['value'])) { $this->setValue($value['value']); } if (is_array($value) && !empty($value['delete'])) { $this->setValue(''); $this->_certFactory->create()->loadByWebsite($this->getScopeId())->delete(); } if (empty($value['tmp_name'])) { return $this; } $tmpPath = $this->_tmpDirectory->getRelativePath($value['tmp_name']); if ($tmpPath && $this->_tmpDirectory->isExist($tmpPath)) { if (!$this->_tmpDirectory->stat($tmpPath)['size']) { throw new \Magento\Framework\Exception\LocalizedException(__('The PayPal certificate file is empty.')); } $this->setValue($value['name']); $content = $this->_encryptor->encrypt($this->_tmpDirectory->readFile($tmpPath)); $this->_certFactory->create()->loadByWebsite($this->getScopeId())->setContent($content)->save(); } return $this; } /** * Process object after delete data * * @return $this */ public function afterDelete() { $this->_certFactory->create()->loadByWebsite($this->getScopeId())->delete(); return $this; } }