Cert.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Model\System\Config\Backend;
  7. use Magento\Framework\Filesystem;
  8. use Magento\Framework\Filesystem\DirectoryList;
  9. /**
  10. * Backend model for saving certificate file in case of using certificate based authentication
  11. *
  12. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  13. */
  14. class Cert extends \Magento\Framework\App\Config\Value
  15. {
  16. /**
  17. * @var \Magento\Paypal\Model\CertFactory
  18. */
  19. protected $_certFactory;
  20. /**
  21. * @var \Magento\Framework\Encryption\EncryptorInterface
  22. */
  23. protected $_encryptor;
  24. /**
  25. * @var \Magento\Framework\Filesystem\Directory\ReadInterface
  26. */
  27. protected $_tmpDirectory;
  28. /**
  29. * @param \Magento\Framework\Model\Context $context
  30. * @param \Magento\Framework\Registry $registry
  31. * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
  32. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  33. * @param \Magento\Paypal\Model\CertFactory $certFactory
  34. * @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
  35. * @param \Magento\Framework\Filesystem $filesystem
  36. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  37. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  38. * @param array $data
  39. */
  40. public function __construct(
  41. \Magento\Framework\Model\Context $context,
  42. \Magento\Framework\Registry $registry,
  43. \Magento\Framework\App\Config\ScopeConfigInterface $config,
  44. \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
  45. \Magento\Paypal\Model\CertFactory $certFactory,
  46. \Magento\Framework\Encryption\EncryptorInterface $encryptor,
  47. \Magento\Framework\Filesystem $filesystem,
  48. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  49. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  50. array $data = []
  51. ) {
  52. $this->_certFactory = $certFactory;
  53. $this->_encryptor = $encryptor;
  54. $this->_tmpDirectory = $filesystem->getDirectoryRead(DirectoryList::SYS_TMP);
  55. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  56. }
  57. /**
  58. * Process additional data before save config
  59. *
  60. * @return $this
  61. * @throws \Magento\Framework\Exception\LocalizedException
  62. */
  63. public function beforeSave()
  64. {
  65. $value = $this->getValue();
  66. if (!empty($value['value'])) {
  67. $this->setValue($value['value']);
  68. }
  69. if (is_array($value) && !empty($value['delete'])) {
  70. $this->setValue('');
  71. $this->_certFactory->create()->loadByWebsite($this->getScopeId())->delete();
  72. }
  73. if (empty($value['tmp_name'])) {
  74. return $this;
  75. }
  76. $tmpPath = $this->_tmpDirectory->getRelativePath($value['tmp_name']);
  77. if ($tmpPath && $this->_tmpDirectory->isExist($tmpPath)) {
  78. if (!$this->_tmpDirectory->stat($tmpPath)['size']) {
  79. throw new \Magento\Framework\Exception\LocalizedException(__('The PayPal certificate file is empty.'));
  80. }
  81. $this->setValue($value['name']);
  82. $content = $this->_encryptor->encrypt($this->_tmpDirectory->readFile($tmpPath));
  83. $this->_certFactory->create()->loadByWebsite($this->getScopeId())->setContent($content)->save();
  84. }
  85. return $this;
  86. }
  87. /**
  88. * Process object after delete data
  89. *
  90. * @return $this
  91. */
  92. public function afterDelete()
  93. {
  94. $this->_certFactory->create()->loadByWebsite($this->getScopeId())->delete();
  95. return $this;
  96. }
  97. }