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-
283use CodeIgniter \Config \BaseConfig ;
29- use CodeIgniter \Config \Services ;
304use Tatter \Audits \Models \AuditModel ;
31- //use Tatter\Audits\Exceptions\AuditsException;
325
336/*** CLASS ***/
347class 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}
0 commit comments