Cert.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Model\ResourceModel;
  7. /**
  8. * PayPal resource model for certificate based authentication
  9. */
  10. class Cert extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  11. {
  12. /**
  13. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  14. */
  15. protected $_coreDate;
  16. /**
  17. * @var \Magento\Framework\Stdlib\DateTime
  18. */
  19. protected $dateTime;
  20. /**
  21. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  22. * @param \Magento\Framework\Stdlib\DateTime\DateTime $coreDate
  23. * @param \Magento\Framework\Stdlib\DateTime $dateTime
  24. * @param string $connectionName
  25. */
  26. public function __construct(
  27. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  28. \Magento\Framework\Stdlib\DateTime\DateTime $coreDate,
  29. \Magento\Framework\Stdlib\DateTime $dateTime,
  30. $connectionName = null
  31. ) {
  32. $this->_coreDate = $coreDate;
  33. $this->dateTime = $dateTime;
  34. parent::__construct($context, $connectionName);
  35. }
  36. /**
  37. * Initialize connection
  38. *
  39. * @return void
  40. */
  41. protected function _construct()
  42. {
  43. $this->_init('paypal_cert', 'cert_id');
  44. }
  45. /**
  46. * Set date of last update
  47. *
  48. * @param \Magento\Framework\Model\AbstractModel $object
  49. * @return \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  50. */
  51. protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
  52. {
  53. $object->setUpdatedAt($this->dateTime->formatDate($this->_coreDate->gmtDate()));
  54. return parent::_beforeSave($object);
  55. }
  56. /**
  57. * Load model by website id
  58. *
  59. * @param \Magento\Paypal\Model\Cert $object
  60. * @param bool $strictLoad
  61. * @return \Magento\Paypal\Model\Cert
  62. */
  63. public function loadByWebsite($object, $strictLoad = true)
  64. {
  65. $connection = $this->getConnection();
  66. $select = $connection->select()->from(['main_table' => $this->getMainTable()]);
  67. if ($strictLoad) {
  68. $select->where('main_table.website_id =?', $object->getWebsiteId());
  69. } else {
  70. $select->where(
  71. 'main_table.website_id IN(0, ?)',
  72. $object->getWebsiteId()
  73. )->order(
  74. 'main_table.website_id DESC'
  75. )->limit(
  76. 1
  77. );
  78. }
  79. $data = $connection->fetchRow($select);
  80. if ($data) {
  81. $object->setData($data);
  82. }
  83. return $object;
  84. }
  85. }