Coupon.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Dotdigitalgroup\Email\Block;
  3. use Dotdigitalgroup\Email\Helper\Config;
  4. use Dotdigitalgroup\Email\Helper\Data;
  5. use Dotdigitalgroup\Email\Model\DateIntervalFactory;
  6. use Dotdigitalgroup\Email\Model\SalesRule\DotmailerCouponGenerator;
  7. use Magento\Framework\View\Element\Template\Context;
  8. /**
  9. * Coupon block
  10. *
  11. * @api
  12. */
  13. class Coupon extends \Magento\Framework\View\Element\Template
  14. {
  15. /**
  16. * @var Data
  17. */
  18. public $helper;
  19. /**
  20. * @var DotmailerCouponGenerator
  21. */
  22. private $dotmailerCouponGenerator;
  23. /**
  24. * @var DateIntervalFactory
  25. */
  26. private $dateIntervalFactory;
  27. /**
  28. * Coupon constructor.
  29. *
  30. * @param Context $context
  31. * @param Data $helper
  32. * @param DotmailerCouponGenerator $dotmailerCouponGenerator
  33. * @param DateIntervalFactory $dateIntervalFactory
  34. * @param array $data
  35. */
  36. public function __construct(
  37. Context $context,
  38. Data $helper,
  39. DotmailerCouponGenerator $dotmailerCouponGenerator,
  40. DateIntervalFactory $dateIntervalFactory,
  41. array $data = []
  42. ) {
  43. $this->dateIntervalFactory = $dateIntervalFactory;
  44. $this->helper = $helper;
  45. $this->dotmailerCouponGenerator = $dotmailerCouponGenerator;
  46. parent::__construct($context, $data);
  47. }
  48. /**
  49. * Generates the coupon code based on the code id.
  50. *
  51. * @return bool
  52. * @throws \Magento\Framework\Exception\LocalizedException
  53. */
  54. public function generateCoupon()
  55. {
  56. $params = $this->getRequest()->getParams();
  57. //check for param code and id
  58. if (! isset($params['code']) ||
  59. ! $this->helper->isCodeValid($params['code'])
  60. ) {
  61. return false;
  62. }
  63. $priceRuleId = (int) $params['id'];
  64. $expireDate = false;
  65. if (isset($params['expire_days']) && is_numeric($params['expire_days']) && $params['expire_days'] > 0) {
  66. $days = (int) $params['expire_days'];
  67. $expireDate = $this->_localeDate->date()
  68. ->add($this->dateIntervalFactory->create(['interval_spec' => sprintf('P%sD', $days)]));
  69. }
  70. return $this->dotmailerCouponGenerator->generateCoupon($priceRuleId, $expireDate);
  71. }
  72. /**
  73. * @return array
  74. */
  75. public function getStyle()
  76. {
  77. return explode(
  78. ',',
  79. $this->helper->getWebsiteConfig(Config::XML_PATH_CONNECTOR_DYNAMIC_COUPON_STYLE)
  80. );
  81. }
  82. /**
  83. * Coupon color from config.
  84. *
  85. * @return mixed
  86. */
  87. public function getCouponColor()
  88. {
  89. return $this->helper->getWebsiteConfig(
  90. Config::XML_PATH_CONNECTOR_DYNAMIC_COUPON_COLOR
  91. );
  92. }
  93. /**
  94. * Coupon font size from config.
  95. *
  96. * @return int|boolean
  97. */
  98. public function getFontSize()
  99. {
  100. return $this->helper->getWebsiteConfig(
  101. Config::XML_PATH_CONNECTOR_DYNAMIC_COUPON_FONT_SIZE
  102. );
  103. }
  104. /**
  105. * Coupon Font from config.
  106. *
  107. * @return string|boolean
  108. */
  109. public function getFont()
  110. {
  111. return $this->helper->getWebsiteConfig(
  112. Config::XML_PATH_CONNECTOR_DYNAMIC_COUPON_FONT
  113. );
  114. }
  115. /**
  116. * Coupon background color from config.
  117. *
  118. * @return string|boolean
  119. */
  120. public function getBackgroundColor()
  121. {
  122. return $this->helper->getWebsiteConfig(
  123. Config::XML_PATH_CONNECTOR_DYNAMIC_COUPON_BG_COLOR
  124. );
  125. }
  126. }