Skip to content

Commit 48797d0

Browse files
committed
Clean up dependencies and inits
1 parent 76fab5e commit 48797d0

3 files changed

Lines changed: 51 additions & 62 deletions

File tree

src/Audits.php

Lines changed: 45 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,7 @@
11
<?php namespace Tatter\Audits;
22

3-
/***
4-
* Name: Audits
5-
* Author: Matthew Gatner
6-
* Contact: mgatner@tattersoftware.com
7-
* Created: 2019-04-05
8-
*
9-
* Description: Lightweight object logging for CodeIgniter 4
10-
*
11-
* Requirements:
12-
* >= PHP 7.1
13-
* >= CodeIgniter 4.0
14-
* Preconfigured, autoloaded Database
15-
* `audits` table (run migrations)
16-
*
17-
* Configuration:
18-
* Use Config/Audits.php to override default behavior
19-
* Run migrations to update database tables:
20-
* > php spark migrate:latest -all
21-
*
22-
* @package CodeIgniter4-Audits
23-
* @author Matthew Gatner
24-
* @link https://github.com/tattersoftware/codeigniter4-audits
25-
*
26-
***/
27-
283
use CodeIgniter\Config\BaseConfig;
29-
use CodeIgniter\Config\Services;
304
use Tatter\Audits\Models\AuditModel;
31-
//use Tatter\Audits\Exceptions\AuditsException;
325

336
/*** CLASS ***/
347
class Audits
@@ -39,55 +12,71 @@ class Audits
3912
* @var \Tatter\Audits\Config\Audits
4013
*/
4114
protected $config;
15+
16+
/**
17+
* Audit rows waiting to add to the database.
18+
*
19+
* @var array
20+
*/
21+
protected $queue = [];
4222

4323
/**
44-
* The active user session.
24+
* Store the configuration
4525
*
46-
* @var \CodeIgniter\Session\Session
26+
* @param BaseConfig $config The Audits configuration to use
4727
*/
48-
protected $session;
49-
50-
// array of audit rows waiting to add to the database
51-
protected $queue = [ ];
52-
53-
// initiate library, check for existing session
5428
public function __construct(BaseConfig $config)
5529
{
56-
// save configuration
5730
$this->config = $config;
58-
59-
// initiate the Session library
60-
$this->session = Services::session();
6131
}
62-
63-
// checks for a logged in user based on config
64-
// returns user ID, 0 for "not logged in", -1 for CLI
32+
33+
/**
34+
* Checks the session for a logged in user based on config
35+
*
36+
* @return int The current user ID, 0 for "not logged in", -1 for CLI
37+
*/
6538
public function sessionUserId(): int
6639
{
6740
if (is_cli())
41+
{
6842
return -1;
69-
return $this->session->get($this->config->sessionUserId) ?? 0;
43+
}
44+
45+
return session($this->config->sessionUserId) ?? 0;
7046
}
71-
72-
// add an audit row to the queue
73-
public function add($audit)
47+
48+
/**
49+
* Add an audit row to the queue
50+
*
51+
* @param array|null The row to cache for insert
52+
*/
53+
public function add(array $audit = null)
7454
{
7555
if (empty($audit))
56+
{
7657
return false;
77-
78-
// add common data
79-
$audit['user_id'] = $this->sessionUserId();
58+
}
59+
60+
// Add common data
61+
$audit['user_id'] = $this->sessionUserId();
8062
$audit['created_at'] = date('Y-m-d H:i:s');
63+
8164
$this->queue[] = $audit;
8265
}
83-
84-
// batch insert all audits from the queue
85-
public function save()
66+
67+
/**
68+
* Batch insert all audits from the queue
69+
*
70+
* @return $this
71+
*/
72+
public function save(): self
8673
{
87-
if (empty($this->queue))
88-
return;
74+
if (! empty($this->queue))
75+
{
76+
$audits = new AuditModel();
77+
$audits->insertBatch($this->queue);
78+
}
8979

90-
$audits = new AuditModel();
91-
$audits->insertBatch($this->queue);
80+
return $this;
9281
}
9382
}

src/Config/Events.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?php namespace Tatter\Audits\Config;
22

33
use CodeIgniter\Events\Events;
4-
use CodeIgniter\Config\Services;
54

65
Events::on('post_system', function () {
7-
Services::audits()->save();
6+
services('audits')->save();
87
});

src/Config/Services.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
<?php namespace Tatter\Audits\Config;
22

33
use CodeIgniter\Config\BaseService;
4-
use CodeIgniter\Database\ConnectionInterface;
54

65
class Services extends BaseService
76
{
87
public static function audits(BaseConfig $config = null, bool $getShared = true)
98
{
10-
if ($getShared):
9+
if ($getShared)
10+
{
1111
return static::getSharedInstance('audits', $config);
12-
endif;
12+
}
1313

1414
// If no config was injected then load one
15-
// Prioritizes app/Config if found
1615
if (empty($config))
16+
{
1717
$config = config('Audits');
18+
}
1819

1920
return new \Tatter\Audits\Audits($config);
2021
}

0 commit comments

Comments
 (0)