Cert.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Model;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Filesystem\Directory\WriteInterface;
  9. /**
  10. * PayPal specific model for certificate based authentication
  11. */
  12. class Cert extends \Magento\Framework\Model\AbstractModel
  13. {
  14. /**
  15. * Certificate base path
  16. */
  17. const BASEPATH_PAYPAL_CERT = 'cert/paypal/';
  18. /**
  19. * @var WriteInterface
  20. */
  21. protected $varDirectory;
  22. /**
  23. * @var \Magento\Framework\Encryption\EncryptorInterface
  24. */
  25. protected $encryptor;
  26. /**
  27. * @param \Magento\Framework\Model\Context $context
  28. * @param \Magento\Framework\Registry $registry
  29. * @param \Magento\Framework\Filesystem $filesystem
  30. * @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
  31. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  32. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  33. * @param array $data
  34. */
  35. public function __construct(
  36. \Magento\Framework\Model\Context $context,
  37. \Magento\Framework\Registry $registry,
  38. \Magento\Framework\Filesystem $filesystem,
  39. \Magento\Framework\Encryption\EncryptorInterface $encryptor,
  40. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  41. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  42. array $data = []
  43. ) {
  44. $this->varDirectory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
  45. $this->encryptor = $encryptor;
  46. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  47. }
  48. /**
  49. * Initialize resource model
  50. *
  51. * @return void
  52. */
  53. protected function _construct()
  54. {
  55. $this->_init(\Magento\Paypal\Model\ResourceModel\Cert::class);
  56. }
  57. /**
  58. * Load model by website id
  59. *
  60. * @param int $websiteId
  61. * @param bool $strictLoad
  62. * @return $this
  63. */
  64. public function loadByWebsite($websiteId, $strictLoad = true)
  65. {
  66. $this->setWebsiteId($websiteId);
  67. $this->_getResource()->loadByWebsite($this, $strictLoad);
  68. return $this;
  69. }
  70. /**
  71. * Get path to PayPal certificate file, if file does not exist try to create it
  72. *
  73. * @return string
  74. * @throws \Magento\Framework\Exception\LocalizedException
  75. */
  76. public function getCertPath()
  77. {
  78. if (!$this->getContent()) {
  79. throw new \Magento\Framework\Exception\LocalizedException(__('The PayPal certificate does not exist.'));
  80. }
  81. $certFileName = sprintf('cert_%s_%s.pem', $this->getWebsiteId(), strtotime($this->getUpdatedAt()));
  82. $certFile = self::BASEPATH_PAYPAL_CERT . $certFileName;
  83. if (!$this->varDirectory->isExist($certFile)) {
  84. $this->_createCertFile($certFile);
  85. }
  86. return $this->varDirectory->getAbsolutePath($certFile);
  87. }
  88. /**
  89. * Create physical certificate file based on DB data
  90. *
  91. * @param string $file
  92. * @return void
  93. */
  94. protected function _createCertFile($file)
  95. {
  96. if ($this->varDirectory->isDirectory(self::BASEPATH_PAYPAL_CERT)) {
  97. $this->_removeOutdatedCertFile();
  98. }
  99. $this->varDirectory->writeFile($file, $this->encryptor->decrypt($this->getContent()));
  100. }
  101. /**
  102. * Check and remove outdated certificate file by website
  103. *
  104. * @return void
  105. */
  106. protected function _removeOutdatedCertFile()
  107. {
  108. $pattern = sprintf('cert_%s*', $this->getWebsiteId());
  109. $entries = $this->varDirectory->search($pattern, self::BASEPATH_PAYPAL_CERT);
  110. foreach ($entries as $entry) {
  111. $this->varDirectory->delete($entry);
  112. }
  113. }
  114. /**
  115. * Delete assigned certificate file after delete object
  116. *
  117. * @return $this
  118. */
  119. public function afterDelete()
  120. {
  121. $this->_removeOutdatedCertFile();
  122. return $this;
  123. }
  124. }