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
|
|
|
private static int $linkCounter = 0; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array<int,string> |
31
|
|
|
*/ |
32
|
|
|
private static array $generated = array(); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var callable|NULL |
36
|
|
|
*/ |
37
|
|
|
private static $customGenerator = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Generates an ID for the specified command. |
41
|
|
|
* Subsequent calls for the same command will |
42
|
|
|
* return the same ID. |
43
|
|
|
* |
44
|
|
|
* @param TrackingIDInterface $command |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public static function generate(TrackingIDInterface $command) : string |
48
|
|
|
{ |
49
|
|
|
$instanceID = $command->getInstanceID(); |
50
|
|
|
|
51
|
|
|
if(isset(self::$generated[$instanceID])) |
52
|
|
|
{ |
53
|
|
|
return self::$generated[$instanceID]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$trackingID = self::generateIDString($command); |
57
|
|
|
|
58
|
|
|
self::$generated[$instanceID] = $trackingID; |
59
|
|
|
|
60
|
|
|
return $trackingID; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Sets the callback used to auto-generate tracking IDs |
65
|
|
|
* for URLs. It gets the mailcode command as sole parameter, |
66
|
|
|
* and must return a tracking ID string. |
67
|
|
|
* |
68
|
|
|
* @param callable $generator |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public static function setGenerator(callable $generator) : void |
72
|
|
|
{ |
73
|
|
|
self::$customGenerator = $generator; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public static function resetGenerator() : void |
77
|
|
|
{ |
78
|
|
|
self::$customGenerator = null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public static function hasCustomGenerator() : bool |
82
|
|
|
{ |
83
|
|
|
return isset(self::$customGenerator); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private static function generateIDString(TrackingIDInterface $command) : string |
87
|
|
|
{ |
88
|
|
|
if(isset(self::$customGenerator)) |
89
|
|
|
{ |
90
|
|
|
$result = call_user_func(self::$customGenerator, $command); |
91
|
|
|
|
92
|
|
|
if(is_string($result)) |
93
|
|
|
{ |
94
|
|
|
return $result; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return self::generateDefault(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public static function generateDefault() : string |
102
|
|
|
{ |
103
|
|
|
self::$linkCounter++; |
104
|
|
|
|
105
|
|
|
return 'link-'.self::$linkCounter; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|