-
Notifications
You must be signed in to change notification settings - Fork 295
/
DigestsPool.php
117 lines (103 loc) · 3.36 KB
/
DigestsPool.php
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
declare(strict_types=1);
/**
* Passbolt ~ Open source password manager for teams
* Copyright (c) Passbolt SA (https://www.passbolt.com)
*
* Licensed under GNU Affero General Public License version 3 of the or any later version.
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Passbolt SA (https://www.passbolt.com)
* @license https://opensource.org/licenses/AGPL-3.0 AGPL License
* @link https://www.passbolt.com Passbolt(tm)
* @since 3.0.0
*/
namespace Passbolt\EmailDigest\Utility\Digest;
/**
* DigestsPool handle a collection of Digest instances which are ordered by priority.
*
* @see DigestInterface
*
* It is used by the DigestsCollection and other components to retrieve and add digests digests dynamically.
*
* @see DigestsCollection
* Lowest priority is equal to DigestsPool::LOWEST_PRIORITY
*/
class DigestsPool
{
public const LOWEST_PRIORITY = -1;
/**
* @var static|null
*/
private static $instance = null;
/**
* @var array
*/
private $digests = [];
/**
* Access to constructor is restricted because it is a singleton.
*/
final private function __construct()
{
}
/**
* Return a singleton of the DigestsPool
*
* @return \Passbolt\EmailDigest\Utility\Digest\DigestsPool
*/
public static function getInstance()
{
if (!isset(static::$instance)) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Clear the singleton of the DigestsPool
*
* @return void
*/
public static function clearInstance(): void
{
static::$instance = null;
}
/**
* Add a digest instance to the pool of digests with an optional priority.
*
* @param \Passbolt\EmailDigest\Utility\Digest\DigestInterface $digest Digest digest instance
* @param int $priority An integer equals to the priority level of the digest. Higher number is more prior.
* @return $this
*/
public function addDigest(DigestInterface $digest, int $priority = self::LOWEST_PRIORITY)
{
$this->digests[] = [
'digest' => $digest,
'priority' => $priority,
];
return $this;
}
/**
* Return a collection of email digests ordered by priority.
*
* @return \Passbolt\EmailDigest\Utility\Digest\DigestInterface[]
*/
public function getDigests()
{
$digests = $this->digests;
// Sort the digests by priority in ascendant order.
usort($digests, function ($digestA, $digestB) {
if ($digestA['priority'] == $digestB['priority']) {
return 0;
}
// -1 if priority of digest A is lower than the priority of digest B
// 0 if priority of digest A is equal to priority of digest B
// 1 if priority of digest A is greater than the priority of digest B
return $digestA['priority'] < $digestB['priority'] ? -1 : 1;
});
// Then we reverse the array to make it in descendant order
// since we want to start with the digest with the highest priority, not the lowest.
$digests = array_reverse($digests);
return array_column($digests, 'digest');
}
}