Passed
Push — master ( af11e6...9ff364 )
by Sebastian
03:46
created

AutoTrackingID::generateIDString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
rs 10
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