Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
35 / 35 |
|
100.00% |
14 / 14 |
CRAP | |
100.00% |
1 / 1 |
| Auditor | |
100.00% |
35 / 35 |
|
100.00% |
14 / 14 |
24 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| adapter | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setModel | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| setUser | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| setDomain | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| setMetadata | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| addMetadata | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setStateData | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getStateData | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasStateData | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setDiff | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| resolveDiff | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| hasDiff | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| send | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <nick@popphp.org> |
| 8 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Audit; |
| 16 | |
| 17 | use Pop\Audit\Adapter\AdapterInterface; |
| 18 | |
| 19 | /** |
| 20 | * Auditor class |
| 21 | * |
| 22 | * @category Pop |
| 23 | * @package Pop\Audit |
| 24 | * @author Nick Sagona, III <nick@popphp.org> |
| 25 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 26 | * @license https://www.popphp.org/license New BSD License |
| 27 | * @version 3.0.0 |
| 28 | */ |
| 29 | class Auditor |
| 30 | { |
| 31 | |
| 32 | /** |
| 33 | * Auditor adapter |
| 34 | * @var ?AdapterInterface |
| 35 | */ |
| 36 | protected ?AdapterInterface $adapter = null; |
| 37 | |
| 38 | /** |
| 39 | * Constructor |
| 40 | * |
| 41 | * Instantiate the auditor object |
| 42 | * |
| 43 | * @param AdapterInterface $adapter |
| 44 | */ |
| 45 | public function __construct(AdapterInterface $adapter) |
| 46 | { |
| 47 | $this->adapter = $adapter; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get the adapter |
| 52 | * |
| 53 | * @return AdapterInterface |
| 54 | */ |
| 55 | public function adapter(): AdapterInterface |
| 56 | { |
| 57 | return $this->adapter; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Set user |
| 62 | * |
| 63 | * @param ?string $model |
| 64 | * @param int|string|null $modelId |
| 65 | * @return Auditor |
| 66 | */ |
| 67 | public function setModel(?string $model = null, int|string|null $modelId = null): Auditor |
| 68 | { |
| 69 | if ($model !== null) { |
| 70 | $this->adapter->setModel($model); |
| 71 | } |
| 72 | if ($modelId !== null) { |
| 73 | $this->adapter->setModelId($modelId); |
| 74 | } |
| 75 | |
| 76 | return $this; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Set user |
| 81 | * |
| 82 | * @param ?string $username |
| 83 | * @param ?int $userId |
| 84 | * @return Auditor |
| 85 | */ |
| 86 | public function setUser(?string $username = null, ?int $userId = null): Auditor |
| 87 | { |
| 88 | if ($username !== null) { |
| 89 | $this->adapter->setUsername($username); |
| 90 | } |
| 91 | if ($userId !== null) { |
| 92 | $this->adapter->setUserId($userId); |
| 93 | } |
| 94 | |
| 95 | return $this; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Set domain, route and method |
| 100 | * |
| 101 | * @param ?string $domain |
| 102 | * @param ?string $route |
| 103 | * @param ?string $method |
| 104 | * @return Auditor |
| 105 | */ |
| 106 | public function setDomain(?string $domain = null, ?string $route = null, ?string $method = null): Auditor |
| 107 | { |
| 108 | if ($domain !== null) { |
| 109 | $this->adapter->setDomain($domain); |
| 110 | } |
| 111 | if ($route !== null) { |
| 112 | $this->adapter->setRoute($route); |
| 113 | } |
| 114 | if ($method !== null) { |
| 115 | $this->adapter->setMethod($method); |
| 116 | } |
| 117 | |
| 118 | return $this; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Set the metadata |
| 123 | * |
| 124 | * @param array $metadata |
| 125 | * @return Auditor |
| 126 | */ |
| 127 | public function setMetadata(array $metadata): Auditor |
| 128 | { |
| 129 | $this->adapter->setMetadata($metadata); |
| 130 | return $this; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Add to the metadata |
| 135 | * |
| 136 | * @param string $name |
| 137 | * @param mixed $value |
| 138 | * @return Auditor |
| 139 | */ |
| 140 | public function addMetadata(string $name, mixed $value): Auditor |
| 141 | { |
| 142 | $this->adapter->addMetadata($name, $value); |
| 143 | return $this; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Set state data |
| 148 | * |
| 149 | * @param array $stateData |
| 150 | * @return Auditor |
| 151 | */ |
| 152 | public function setStateData(array $stateData): Auditor |
| 153 | { |
| 154 | $this->adapter->setStateData($stateData); |
| 155 | return $this; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Get state data |
| 160 | * |
| 161 | * @param ?string $name |
| 162 | * @return mixed |
| 163 | */ |
| 164 | public function getStateData(?string $name = null): mixed |
| 165 | { |
| 166 | return $this->adapter->getStateData($name); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Has state data |
| 171 | * |
| 172 | * @param ?string $name |
| 173 | * @return bool |
| 174 | */ |
| 175 | public function hasStateData(?string $name = null): bool |
| 176 | { |
| 177 | return $this->adapter->hasStateData($name); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Set the differences in values between the model states (that have already been processed) |
| 182 | * |
| 183 | * @param array $old |
| 184 | * @param array $new |
| 185 | * @return Auditor |
| 186 | */ |
| 187 | public function setDiff(array $old = [], array $new = []): Auditor |
| 188 | { |
| 189 | $this->adapter->setDiff($old, $new); |
| 190 | return $this; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Resolve the differences in values between the model states |
| 195 | * |
| 196 | * @param array $old |
| 197 | * @param array $new |
| 198 | * @param bool $state |
| 199 | * @return Auditor |
| 200 | */ |
| 201 | public function resolveDiff(array $old = [], array $new = [], bool $state = true): Auditor |
| 202 | { |
| 203 | $this->adapter->resolveDiff($old, $new, $state); |
| 204 | return $this; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Check if the model states are different |
| 209 | * |
| 210 | * @return bool |
| 211 | */ |
| 212 | public function hasDiff(): bool |
| 213 | { |
| 214 | return $this->adapter->hasDiff(); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Send the results of the audit |
| 219 | * |
| 220 | * @param ?array $old |
| 221 | * @param ?array $new |
| 222 | * @param bool $state |
| 223 | * @return mixed |
| 224 | */ |
| 225 | public function send(?array $old = null, ?array $new = null, bool $state = true): mixed |
| 226 | { |
| 227 | if (($old !== null) && ($new !== null)) { |
| 228 | $this->adapter->resolveDiff($old, $new, $state); |
| 229 | } |
| 230 | |
| 231 | return ($this->adapter->hasDiff()) ? $this->adapter->send() : false; |
| 232 | } |
| 233 | |
| 234 | } |