|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the class {@see \Mailcode\Commands\Command\ShowURL\AutoTrackingID}. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Mailcode |
|
6
|
|
|
* @subpackage Commands |
|
7
|
|
|
* @see \Mailcode\Commands\Command\ShowURL\AutoTrackingID |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Mailcode\Commands\Command\ShowURL; |
|
13
|
|
|
|
|
14
|
|
|
use Mailcode\Interfaces\Commands\Validation\TrackingIDInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Generates a tracking ID for a URL, used when a |
|
18
|
|
|
* `showurl` command's tracking is enabled, but no |
|
19
|
|
|
* tracking ID is specified. |
|
20
|
|
|
* |
|
21
|
|
|
* @package Mailcode |
|
22
|
|
|
* @subpackage Commands |
|
23
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class AutoTrackingID |
|
26
|
|
|
{ |
|
27
|
|
|
public const AUTO_ID_TEMPLATE = 'link-%03d'; |
|
28
|
|
|
|
|
29
|
|
|
private static int $linkCounter = 0; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array<int,string> |
|
33
|
|
|
*/ |
|
34
|
|
|
private static array $generated = array(); |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var callable|NULL |
|
38
|
|
|
*/ |
|
39
|
|
|
private static $customGenerator = null; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Generates an ID for the specified command. |
|
43
|
|
|
* Subsequent calls for the same command will |
|
44
|
|
|
* return the same ID. |
|
45
|
|
|
* |
|
46
|
|
|
* @param TrackingIDInterface $command |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function generate(TrackingIDInterface $command) : string |
|
50
|
|
|
{ |
|
51
|
|
|
$instanceID = $command->getInstanceID(); |
|
52
|
|
|
|
|
53
|
|
|
if(isset(self::$generated[$instanceID])) |
|
54
|
|
|
{ |
|
55
|
|
|
return self::$generated[$instanceID]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$trackingID = self::generateIDString($command); |
|
59
|
|
|
|
|
60
|
|
|
self::$generated[$instanceID] = $trackingID; |
|
61
|
|
|
|
|
62
|
|
|
return $trackingID; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Sets the callback used to auto-generate tracking IDs |
|
67
|
|
|
* for URLs. It gets the mailcode command as sole parameter, |
|
68
|
|
|
* and must return a tracking ID string. |
|
69
|
|
|
* |
|
70
|
|
|
* @param callable $generator |
|
71
|
|
|
* @return void |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function setGenerator(callable $generator) : void |
|
74
|
|
|
{ |
|
75
|
|
|
self::$customGenerator = $generator; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public static function resetGenerator() : void |
|
79
|
|
|
{ |
|
80
|
|
|
self::$customGenerator = null; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public static function hasCustomGenerator() : bool |
|
84
|
|
|
{ |
|
85
|
|
|
return isset(self::$customGenerator); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
private static function generateIDString(TrackingIDInterface $command) : string |
|
89
|
|
|
{ |
|
90
|
|
|
if(isset(self::$customGenerator)) |
|
91
|
|
|
{ |
|
92
|
|
|
$result = call_user_func(self::$customGenerator, $command); |
|
93
|
|
|
|
|
94
|
|
|
if(is_string($result)) |
|
95
|
|
|
{ |
|
96
|
|
|
return $result; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return self::generateDefault(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public static function resetLinkCounter() : void |
|
104
|
|
|
{ |
|
105
|
|
|
self::$linkCounter = 0; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public static function generateDefault() : string |
|
109
|
|
|
{ |
|
110
|
|
|
self::$linkCounter++; |
|
111
|
|
|
|
|
112
|
|
|
return sprintf(self::AUTO_ID_TEMPLATE, self::$linkCounter); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|