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
6 namespace Magento\Paypal\Model;
7 
10 
15 {
19  const BASEPATH_PAYPAL_CERT = 'cert/paypal/';
20 
24  protected $varDirectory;
25 
29  protected $encryptor;
30 
40  public function __construct(
41  \Magento\Framework\Model\Context $context,
42  \Magento\Framework\Registry $registry,
43  \Magento\Framework\Filesystem $filesystem,
44  \Magento\Framework\Encryption\EncryptorInterface $encryptor,
45  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
46  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
47  array $data = []
48  ) {
49  $this->varDirectory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
50  $this->encryptor = $encryptor;
51  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
52  }
53 
59  protected function _construct()
60  {
61  $this->_init(\Magento\Paypal\Model\ResourceModel\Cert::class);
62  }
63 
71  public function loadByWebsite($websiteId, $strictLoad = true)
72  {
73  $this->setWebsiteId($websiteId);
74  $this->_getResource()->loadByWebsite($this, $strictLoad);
75  return $this;
76  }
77 
84  public function getCertPath()
85  {
86  if (!$this->getContent()) {
87  throw new \Magento\Framework\Exception\LocalizedException(__('The PayPal certificate does not exist.'));
88  }
89 
90  $certFileName = sprintf('cert_%s_%s.pem', $this->getWebsiteId(), strtotime($this->getUpdatedAt()));
91  $certFile = self::BASEPATH_PAYPAL_CERT . $certFileName;
92 
93  if (!$this->varDirectory->isExist($certFile)) {
94  $this->_createCertFile($certFile);
95  }
96  return $this->varDirectory->getAbsolutePath($certFile);
97  }
98 
105  protected function _createCertFile($file)
106  {
107  if ($this->varDirectory->isDirectory(self::BASEPATH_PAYPAL_CERT)) {
108  $this->_removeOutdatedCertFile();
109  }
110  $this->varDirectory->writeFile($file, $this->encryptor->decrypt($this->getContent()));
111  }
112 
118  protected function _removeOutdatedCertFile()
119  {
120  $pattern = sprintf('cert_%s*', $this->getWebsiteId());
121  $entries = $this->varDirectory->search($pattern, self::BASEPATH_PAYPAL_CERT);
122  foreach ($entries as $entry) {
123  $this->varDirectory->delete($entry);
124  }
125  }
126 
132  public function afterDelete()
133  {
134  $this->_removeOutdatedCertFile();
135  return $this;
136  }
137 }
loadByWebsite($websiteId, $strictLoad=true)
Definition: Cert.php:71
$pattern
Definition: website.php:22
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
const BASEPATH_PAYPAL_CERT
Definition: Cert.php:19
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Filesystem $filesystem, \Magento\Framework\Encryption\EncryptorInterface $encryptor, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Cert.php:40
$filesystem