1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace Dotdigitalgroup\Email\Model\SalesRule;
use Magento\Framework\Math\Random;
use Magento\SalesRule\Helper\Coupon;
use Magento\SalesRule\Model\Coupon\CodegeneratorInterface;
class DotmailerCouponCodeGenerator implements CodegeneratorInterface
{
/**
* @var Coupon
*/
private $salesRuleCoupon;
/**
* @param Coupon $salesRuleCoupon
*/
public function __construct(
Coupon $salesRuleCoupon
) {
$this->salesRuleCoupon = $salesRuleCoupon;
}
/**
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function generateCode()
{
$format = Coupon::COUPON_FORMAT_ALPHANUMERIC;
$splitChar = $this->getDelimiter();
$charset = $this->salesRuleCoupon->getCharset($format);
$code = '';
$charsetSize = count($charset);
$split = 3;
$length = 9;
for ($i = 0; $i < $length; ++$i) {
$char = $charset[Random::getRandomNumber(0, $charsetSize - 1)];
if (($split > 0) && (($i % $split) === 0) && ($i !== 0)) {
$char = $splitChar . $char;
}
$code .= $char;
}
return 'DOT-' . $code;
}
/**
* Retrieve delimiter
*
* @return string
*/
public function getDelimiter()
{
return $this->salesRuleCoupon->getCodeSeparator();
}
}