11<?php namespace Tatter \Audits \Traits ;
22
3- use CodeIgniter \Config \Services ;
43use Tatter \Audits \Models \AuditModel ;
54
65/*** CLASS ***/
76trait AuditsTrait
87{
9- // takes an array of model $returnTypes and returns an array of Audits, arranged by object and event
10- // optionally filter by $events (string or array of strings)
8+ /**
9+ * Takes an array of model $returnTypes
10+ * and returns an array of Audits,
11+ * arranged by object and event.
12+ * Optionally filter by $events
13+ * (string or array of strings).
14+ *
15+ * @param array $objects
16+ * @param array|string|null $events
17+ *
18+ * @internal Due to a typo this function has never worked in a released version.
19+ * It will be refactored soon without announcing a new major release
20+ * so do not build on the signature or functionality.
21+ */
1122 public function getAudits (array $ objects , $ events = null ): array
1223 {
1324 if (empty ($ objects ))
14- return null ;
15-
16- // get the primary keys from the objects
25+ {
26+ return [];
27+ }
28+
29+ // Get the primary keys from the objects
1730 $ objectIds = array_column ($ objects , $ this ->primaryKey );
18-
19- $ audits = new AuditModel ();
20- $ query = $ query ->where ('source ' , $ this ->table )
21- -> whereIn ( ' source_id ' , $ objectIds );
31+
32+ // Start the query
33+ $ query = model (AuditModel::class) ->where ('source ' , $ this ->table )-> whereIn ( ' source_id ' , $ objectIds );
34+
2235 if (is_string ($ events ))
36+ {
2337 $ query = $ query ->where ('event ' , $ events );
38+ }
2439 elseif (is_array ($ events ))
40+ {
2541 $ query = $ query ->whereIn ('event ' , $ events );
42+ }
2643
27- // index by objectId, event
28- $ array = [ ];
29- while ($ audit = $ query ->getUnbufferedRow ()):
44+ // Index by objectId, event
45+ $ array = [];
46+ while ($ audit = $ query ->getUnbufferedRow ())
47+ {
3048 if (empty ($ array [$ audit ->{$ this ->primaryKey }]))
31- $ array [$ audit ->{$ this ->primaryKey }] = [ ];
32-
49+ {
50+ $ array [$ audit ->{$ this ->primaryKey }] = [];
51+ }
3352 if (empty ($ array [$ audit ->{$ this ->primaryKey }][$ audit ->event ]))
34- $ array [$ audit ->{$ this ->primaryKey }][$ audit ->event ] = [ ];
53+ {
54+ $ array [$ audit ->{$ this ->primaryKey }][$ audit ->event ] = [];
55+ }
3556
3657 $ array [$ audit ->{$ this ->primaryKey }][$ audit ->event ][] = $ audit ;
37- endwhile ;
58+ }
3859
3960 return $ array ;
4061 }
@@ -47,11 +68,11 @@ protected function auditInsert(array $data)
4768
4869 $ audit = [
4970 'source ' => $ this ->table ,
50- 'source_id ' => $ this ->db ->insertID (),
71+ 'source_id ' => $ this ->db ->insertID (), // @phpstan-ignore-line
5172 'event ' => 'insert ' ,
5273 'summary ' => count ($ data ['data ' ]) . ' fields ' ,
5374 ];
54- Services:: audits ( )->add ($ audit );
75+ service ( ' audits ' )->add ($ audit );
5576
5677 return $ data ;
5778 }
@@ -65,7 +86,7 @@ protected function auditUpdate(array $data)
6586 'event ' => 'update ' ,
6687 'summary ' => count ($ data ['data ' ]) . ' fields ' ,
6788 ];
68- Services:: audits ( )->add ($ audit );
89+ service ( ' audits ' )->add ($ audit );
6990
7091 return $ data ;
7192 }
@@ -85,7 +106,7 @@ protected function auditDelete(array $data)
85106 ];
86107
87108 // add an entry for each ID
88- $ audits = Services:: audits ( );
109+ $ audits = service ( ' audits ' );
89110 foreach ($ data ['id ' ] as $ id ):
90111 $ audit ['source_id ' ] = $ id ;
91112 $ audits ->add ($ audit );
0 commit comments