Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Cert.php
Go to the documentation of this file.
1 <?php
7 
10 
16 class Cert extends \Magento\Framework\App\Config\Value
17 {
21  protected $_certFactory;
22 
26  protected $_encryptor;
27 
31  protected $_tmpDirectory;
32 
45  public function __construct(
46  \Magento\Framework\Model\Context $context,
47  \Magento\Framework\Registry $registry,
49  \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
50  \Magento\Paypal\Model\CertFactory $certFactory,
51  \Magento\Framework\Encryption\EncryptorInterface $encryptor,
52  \Magento\Framework\Filesystem $filesystem,
53  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
54  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
55  array $data = []
56  ) {
57  $this->_certFactory = $certFactory;
58  $this->_encryptor = $encryptor;
59  $this->_tmpDirectory = $filesystem->getDirectoryRead(DirectoryList::SYS_TMP);
60  parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
61  }
62 
69  public function beforeSave()
70  {
71  $value = $this->getValue();
72 
73  if (!empty($value['value'])) {
74  $this->setValue($value['value']);
75  }
76 
77  if (is_array($value) && !empty($value['delete'])) {
78  $this->setValue('');
79  $this->_certFactory->create()->loadByWebsite($this->getScopeId())->delete();
80  }
81 
82  if (empty($value['tmp_name'])) {
83  return $this;
84  }
85 
86  $tmpPath = $this->_tmpDirectory->getRelativePath($value['tmp_name']);
87 
88  if ($tmpPath && $this->_tmpDirectory->isExist($tmpPath)) {
89  if (!$this->_tmpDirectory->stat($tmpPath)['size']) {
90  throw new \Magento\Framework\Exception\LocalizedException(__('The PayPal certificate file is empty.'));
91  }
92  $this->setValue($value['name']);
93  $content = $this->_encryptor->encrypt($this->_tmpDirectory->readFile($tmpPath));
94  $this->_certFactory->create()->loadByWebsite($this->getScopeId())->setContent($content)->save();
95  }
96  return $this;
97  }
98 
104  public function afterDelete()
105  {
106  $this->_certFactory->create()->loadByWebsite($this->getScopeId())->delete();
107  return $this;
108  }
109 }
$config
Definition: fraud_order.php:17
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$value
Definition: gender.phtml:16
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\App\Config\ScopeConfigInterface $config, \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList, \Magento\Paypal\Model\CertFactory $certFactory, \Magento\Framework\Encryption\EncryptorInterface $encryptor, \Magento\Framework\Filesystem $filesystem, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Cert.php:45
$filesystem