@@ -79,7 +79,7 @@ public function logs()
7979 }
8080
8181 /**
82- * Generates a list of the last $limit revisions made to any objects
82+ * Generates a list of the last $limit logs made to any objects
8383 * of the class it is being called from.
8484 *
8585 * @param int $limit
@@ -94,6 +94,8 @@ public static function classLogHistory($limit = 100, $order = 'desc')
9494 }
9595
9696 /**
97+ * Generates a list of the last $limit logs
98+ *
9799 * @param int $limit
98100 * @param string $order
99101 *
@@ -106,9 +108,12 @@ public function logHistory($limit = 100, $order = 'desc')
106108
107109 /**
108110 * Prepare audit model.
111+ *
112+ * @return void
109113 */
110114 public function prepareAudit ()
111115 {
116+ // If auditing is enabled
112117 if ($ this ->isAuditEnabled ()) {
113118 $ this ->originalData = $ this ->original ;
114119 $ this ->updatedData = $ this ->attributes ;
@@ -136,6 +141,7 @@ public function prepareAudit()
136141
137142 // Get changed data
138143 $ this ->dirtyData = $ this ->getDirty ();
144+
139145 // Tells whether the record exists in the database
140146 $ this ->updating = $ this ->exists ;
141147 }
@@ -148,18 +154,8 @@ public function prepareAudit()
148154 */
149155 public function auditCreation ()
150156 {
151- if (isset ($ this ->historyLimit ) && $ this ->logHistory ()->count () >= $ this ->historyLimit ) {
152- $ LimitReached = true ;
153- } else {
154- $ LimitReached = false ;
155- }
156- if (isset ($ this ->logCleanup )) {
157- $ LogCleanup = $ this ->LogCleanup ;
158- } else {
159- $ LogCleanup = false ;
160- }
161-
162- if ((($ this ->isAuditEnabled ())) && (!$ LimitReached || $ LogCleanup )) {
157+ // If auditing is enabled
158+ if ($ this ->isAuditEnabled ()) {
163159 $ log = ['old_value ' => null ];
164160 $ log ['new_value ' ] = [];
165161
@@ -180,18 +176,8 @@ public function auditCreation()
180176 */
181177 public function auditUpdate ()
182178 {
183- if (isset ($ this ->historyLimit ) && $ this ->logHistory ()->count () >= $ this ->historyLimit ) {
184- $ LimitReached = true ;
185- } else {
186- $ LimitReached = false ;
187- }
188- if (isset ($ this ->logCleanup )) {
189- $ LogCleanup = $ this ->LogCleanup ;
190- } else {
191- $ LogCleanup = false ;
192- }
193-
194- if ((($ this ->isAuditEnabled ()) && $ this ->updating ) && (!$ LimitReached || $ LogCleanup )) {
179+ // If auditing is enabled and object updated
180+ if ($ this ->isAuditEnabled () && $ this ->updating ) {
195181 $ changes_to_record = $ this ->changedAuditingFields ();
196182 if (count ($ changes_to_record )) {
197183 foreach ($ changes_to_record as $ key => $ change ) {
@@ -211,18 +197,8 @@ public function auditUpdate()
211197 */
212198 public function auditDeletion ()
213199 {
214- if (isset ($ this ->historyLimit ) && $ this ->logHistory ()->count () >= $ this ->historyLimit ) {
215- $ LimitReached = true ;
216- } else {
217- $ LimitReached = false ;
218- }
219- if (isset ($ this ->logCleanup )) {
220- $ LogCleanup = $ this ->LogCleanup ;
221- } else {
222- $ LogCleanup = false ;
223- }
224-
225- if ((($ this ->isAuditEnabled ()) && $ this ->isAuditing ('deleted_at ' )) && (!$ LimitReached || $ LogCleanup )) {
200+ // If auditing is enabled
201+ if ($ this ->isAuditEnabled () && $ this ->isAuditing ('deleted_at ' )) {
226202 $ log = ['new_value ' => null ];
227203
228204 foreach ($ this ->updatedData as $ key => $ value ) {
@@ -255,7 +231,26 @@ public function audit(array $log, $type)
255231 'updated_at ' => $ this ->freshTimestamp (),
256232 ];
257233
258- return $ this ->saveAudit ($ logAuditing );
234+ // Insert the log and clear the oldest logs if given a limit.
235+ if ($ this ->saveAudit ($ logAuditing )) $ this ->clearOlderLogs ();
236+ }
237+
238+ /**
239+ * Clear the oldest logs if given a limit.
240+ *
241+ * @return void
242+ */
243+ private function clearOlderLogs (){
244+
245+ $ logHistoryCount = $ this ->logHistory ()->count ();
246+ $ logHistoryOlder = $ logHistoryCount - $ this ->historyLimit ;
247+
248+ if (isset ($ this ->historyLimit ) && $ logHistoryOlder > 0 ) {
249+ $ logs = $ this ->logHistory ($ logHistoryOlder , 'asc ' );
250+ $ logs ->each (function ($ log ){
251+ $ log ->delete ();
252+ });
253+ }
259254 }
260255
261256 /**
@@ -321,7 +316,8 @@ private function changedAuditingFields()
321316 foreach ($ this ->dirtyData as $ key => $ value ) {
322317 if ($ this ->isAuditing ($ key ) && !is_array ($ value )) {
323318 // Check whether the current value is difetente the original value
324- if (!isset ($ this ->originalData [$ key ]) || $ this ->originalData [$ key ] != $ this ->updatedData [$ key ]) {
319+ if (!isset ($ this ->originalData [$ key ]) ||
320+ $ this ->originalData [$ key ] != $ this ->updatedData [$ key ]) {
325321 $ changes_to_record [$ key ] = $ value ;
326322 }
327323 } else {
@@ -363,8 +359,10 @@ private function isAuditing($key)
363359 */
364360 private function isAuditEnabled ()
365361 {
366- // Check that the model has audit enabled and also check that we aren't running in cosole or that we want to log console too.
367- if ((!isset ($ this ->auditEnabled ) || $ this ->auditEnabled ) && (!App::runningInConsole () || Config::get ('auditing.audit_console ' ))) {
362+ // Check that the model has audit enabled and also check that we aren't
363+ // running in cosole or that we want to log console too.
364+ if ((!isset ($ this ->auditEnabled ) || $ this ->auditEnabled )
365+ && (!App::runningInConsole () || Config::get ('auditing.audit_console ' ))) {
368366 return true ;
369367 }
370368
0 commit comments