SendFriend.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SendFriend\Model\ResourceModel;
  7. /**
  8. * SendFriend Log Resource Model
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class SendFriend extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  16. {
  17. /**
  18. * Initialize connection and table
  19. *
  20. * @return void
  21. */
  22. protected function _construct()
  23. {
  24. $this->_init('sendfriend_log', 'log_id');
  25. }
  26. /**
  27. * Retrieve Sended Emails By Ip
  28. *
  29. * @param \Magento\SendFriend\Model\SendFriend $object
  30. * @param int $ip
  31. * @param int $startTime
  32. * @param int $websiteId
  33. * @return int
  34. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  35. */
  36. public function getSendCount($object, $ip, $startTime, $websiteId = null)
  37. {
  38. $connection = $this->getConnection();
  39. $select = $connection->select()->from(
  40. $this->getMainTable(),
  41. ['count' => new \Zend_Db_Expr('count(*)')]
  42. )->where(
  43. 'ip=:ip
  44. AND time>=:time
  45. AND website_id=:website_id'
  46. );
  47. $bind = ['ip' => $ip, 'time' => $startTime, 'website_id' => (int)$websiteId];
  48. $row = $connection->fetchRow($select, $bind);
  49. return $row['count'];
  50. }
  51. /**
  52. * Add sended email by ip item
  53. *
  54. * @param int $ip
  55. * @param int $startTime
  56. * @param int $websiteId
  57. * @return $this
  58. */
  59. public function addSendItem($ip, $startTime, $websiteId)
  60. {
  61. $this->getConnection()->insert(
  62. $this->getMainTable(),
  63. ['ip' => $ip, 'time' => $startTime, 'website_id' => $websiteId]
  64. );
  65. return $this;
  66. }
  67. /**
  68. * Delete Old logs
  69. *
  70. * @param int $time
  71. * @return $this
  72. */
  73. public function deleteLogsBefore($time)
  74. {
  75. $cond = $this->getConnection()->quoteInto('time<?', $time);
  76. $this->getConnection()->delete($this->getMainTable(), $cond);
  77. return $this;
  78. }
  79. }