Skip to content

Commit

Permalink
Add full state tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Sep 29, 2023
1 parent c80dc20 commit f4b9e85
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 25 deletions.
46 changes: 39 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,26 @@ $new = [
'email' => '[email protected]'
];

// Tracking the full state is optional
$state = [
"id" => 1,
"username" => "admin2",
"first_name" => "John",
"last_name" => "Doe",
"email" => "[email protected]"
];

$auditor = new Audit\Auditor(new Audit\Adapter\File('tmp')); // Folder passed to the File adapter
$auditor->setModel('MyApp\Model\User', 1001); // Model name and model ID
$auditor->setUser('testuser', 101); // Username and user ID (optional)
$auditor->setUser('testuser', 101); // Username and user ID that made the change (optional)
$auditor->setDomain('users.localhost'); // Domain (optional)
$logFile = $auditor->send($old, $new);
$logFile = $auditor->send($old, $new, $state);
```

In this case, the variable `$logFile` would contain the name of the audit log file,
for example `pop-audit-1535060625.log` in case it needs to be referenced again.
That file will contain the JSON-encoded data that tracks the difference between the
model states:
model states, as well as a snapshot of the full state (if provided):

```json
{
Expand All @@ -67,12 +76,17 @@ model states:
"new": {
"username": "admin2"
},
"state": {
"id": 1,
"username": "admin2",
"first_name": "John",
"last_name": "Doe",
"email": "[email protected]"
},
"timestamp": "2018-08-23 16:56:36"
}
```

Notice that only the difference is stored. In this case, only the `username` value changed.

### Using a database

Using a database connection requires the use of the `pop-db` component and a database table class
Expand Down Expand Up @@ -104,12 +118,20 @@ $new = [
'email' => '[email protected]'
];

// Tracking the full state is optional
$state = [
"id" => 1,
"username" => "admin2",
"first_name" => "John",
"last_name" => "Doe",
"email" => "[email protected]"
];

$auditor = new Audit\Auditor(new Audit\Adapter\Table('AuditLog'));
$auditor->setModel('MyApp\Model\User', 1001);
$auditor->setUser('testuser', 101);
$auditor->setDomain('users.localhost');
$row = $auditor->send($old, $new);
$row = $auditor->send($old, $new, $state);
```

If needed, the variable `$row` contains the newly created record in the audit table.
Expand All @@ -131,6 +153,15 @@ $new = [
'email' => '[email protected]'
];

// Tracking the full state is optional
$state = [
"id" => 1,
"username" => "admin2",
"first_name" => "John",
"last_name" => "Doe",
"email" => "[email protected]"
];

$stream = new \Pop\Http\Client\Stream('http://audit.localhost');
$stream->setContextOptions(['http' => [
'protocol_version' => '1.1',
Expand All @@ -142,7 +173,7 @@ $auditor = new Audit\Auditor(new Audit\Adapter\Http($stream));
$auditor->setModel('MyApp\Model\User', 1001);
$auditor->setUser('testuser', 101);
$auditor->setDomain('users.localhost');
$auditor->send($old, $new);
$auditor->send($old, $new, $state);
```

### Setting the Diff
Expand All @@ -169,3 +200,4 @@ $auditor->setUser('testuser', 101);
$auditor->setDomain('users.localhost');
$auditor->setDiff($old, $new);
$auditor->send();
```
21 changes: 11 additions & 10 deletions src/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,16 +357,6 @@ public function setStateData(array $state)
return $this;
}

/**
* Determine if there is a final state
*
* @return boolean
*/
public function hasStateData()
{
return !empty($this->stateData);
}

/**
* Get the final state
*
Expand All @@ -382,6 +372,17 @@ public function getStateData($name = null)
}
}

/**
* Determine if there is final state data
*
* @param string $name
* @return boolean
*/
public function hasStateData($name = null)
{
return (null !== $name) ? array_key_exists($name, $this->stateData) : !empty($this->stateData);
}

/**
* Set the differences in values between the model states (that have already been processed)
*
Expand Down
14 changes: 7 additions & 7 deletions src/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,6 @@ public function getMetadata($name = null);
*/
public function setStateData(array $state);

/**
* Determine if there is a final state
*
* @return boolean
*/
public function hasStateData();

/**
* Get the final state
*
Expand All @@ -208,6 +201,13 @@ public function hasStateData();
*/
public function getStateData($name = null);

/**
* Determine if there is a final state
*
* @return boolean
*/
public function hasStateData();

/**
* Set the differences in values between the model states (that have already been processed)
*
Expand Down
39 changes: 38 additions & 1 deletion src/Auditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,39 @@ public function addMetadata($name, $value)
return $this;
}

/**
* Set state data
*
* @param array $stateData
* @return Auditor
*/
public function setStateData(array $stateData)
{
$this->adapter->setStateData($stateData);
return $this;
}

/**
* Get state data
*
* @param string $name
* @return mixed
*/
public function getStateData($name = null)
{
return $this->adapter->getStateData($name);
}

/**
* Has state data
*
* @param string $name
* @return boolean
*/
public function hasStateData($name = null)
{
return $this->adapter->hasStateData($name);
}

/**
* Set the differences in values between the model states (that have already been processed)
Expand Down Expand Up @@ -184,13 +217,17 @@ public function hasDiff()
*
* @param array $old
* @param array $new
* @param array $state
* @return mixed
*/
public function send(array $old = null, array $new = null)
public function send(array $old = null, array $new = null, array $state = null)
{
if ((null !== $old) && (null !== $new)) {
$this->adapter->resolveDiff($old, $new);
}
if (null !== $state) {
$this->adapter->setStateData($state);
}

return ($this->adapter->hasDiff()) ? $this->adapter->send() : false;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/AuditorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ public function testMetadata()
$this->assertEquals('bar', $auditor->adapter()->getMetadata('foo'));
}

public function testStateData()
{
$auditor = new Audit\Auditor(new Audit\Adapter\File(__DIR__ . '/tmp'));
$auditor->setStateData(['foo' => 'bar']);
$this->assertTrue($auditor->adapter()->hasStateData());
$this->assertTrue($auditor->adapter()->hasStateData('foo'));
$this->assertFalse($auditor->adapter()->hasStateData('baz'));
$this->assertEquals('bar', $auditor->adapter()->getStateData('foo'));
$this->assertEquals(['foo' => 'bar'], $auditor->adapter()->getStateData());
}

public function testSetDiff()
{
$old = ['username' => 'admin'];
Expand Down

0 comments on commit f4b9e85

Please sign in to comment.