Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
11 / 11
CRAP
100.00% covered (success)
100.00%
1 / 1
Auditor
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
11 / 11
21
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
 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-2023 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-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
25 * @license    http://www.popphp.org/license     New BSD License
26 * @version    1.3.0
27 */
28class Auditor
29{
30
31    /**
32     * Auditor adapter
33     * @var AdapterInterface
34     */
35    protected $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 mixed
53     */
54    public function adapter()
55    {
56        return $this->adapter;
57    }
58
59    /**
60     * Set user
61     *
62     * @param  string  $model
63     * @param  int     $modelId
64     * @return Auditor
65     */
66    public function setModel($model = null, $modelId = null)
67    {
68        if (null !== $model) {
69            $this->adapter->setModel($model);
70        }
71        if (null !== $modelId) {
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($username = null, $userId = null)
86    {
87        if (null !== $username) {
88            $this->adapter->setUsername($username);
89        }
90        if (null !== $userId) {
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($domain = null, $route = null, $method = null)
106    {
107        if (null !== $domain) {
108            $this->adapter->setDomain($domain);
109        }
110        if (null !== $route) {
111            $this->adapter->setRoute($route);
112        }
113        if (null !== $method) {
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)
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($name, $value)
140    {
141        $this->adapter->addMetadata($name, $value);
142        return $this;
143    }
144
145
146    /**
147     * Set the differences in values between the model states (that have already been processed)
148     *
149     * @param  array $old
150     * @param  array $new
151     * @return self
152     */
153    public function setDiff(array $old = [], array $new = [])
154    {
155        $this->adapter->setDiff($old, $new);
156        return $this;
157    }
158
159    /**
160     * Resolve the differences in values between the model states
161     *
162     * @param  array $old
163     * @param  array $new
164     * @return self
165     */
166    public function resolveDiff(array $old = [], array $new = [])
167    {
168        $this->adapter->resolveDiff($old, $new);
169        return $this;
170    }
171
172    /**
173     * Check if the model states are different
174     *
175     * @return boolean
176     */
177    public function hasDiff()
178    {
179        return $this->adapter->hasDiff();
180    }
181
182    /**
183     * Send the results of the audit
184     *
185     * @param  array $old
186     * @param  array $new
187     * @return mixed
188     */
189    public function send(array $old = null, array $new = null)
190    {
191        if ((null !== $old) && (null !== $new)) {
192            $this->adapter->resolveDiff($old, $new);
193        }
194
195        return ($this->adapter->hasDiff()) ? $this->adapter->send() : false;
196    }
197
198}