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