a4sex/auto-clean-entity

Abstract Doctrine Entity Cleaner

This package's canonical repository appears to be gone and the package has been frozen as a result.

v1.0.3 2023-09-02 13:27 UTC

This package is auto-updated.

Last update: 2024-03-31 15:04:17 UTC


README

Create Cleaner for specific doctrine Entity

Entity MUST contain the id primary field and some date_time field

<?php

namespace App\Services\Cleaner;

use A4Sex\Services\AbstractCleaner; 
use App\Repository\MessageRepository;

class MessageCleaner extends AbstractCleaner
{  
    public $autocleanEnable = true;
    public $cleanDaysPeriod = 7;
    public $cleanDateField = 'date';
    // OR/AND Add batch cleaner params

    public function __construct(
        DialogRepository $repository
    ) {
        $this->repository = $repository;
    }

}

Some settings

    public $autocleanEnable = false; // default - disabled
    public $autocleanEvery = 10; // conditionally, each (divisible) - 10 - every tenth
    public $cleanDaysPeriod = 15; // more than a few days old
    public $cleanItemsCount = 100; // select no more items
    public $cleanDateField = 'updated'; // `db` datetime field 

Use auto cleaning


    // ...
    public function __construct(
        MessageCleaner $cleaner
    ) {
        $this->cleaner = $cleaner;
    }

    public function someFunction()
    {
        // ...
        $id = $this->user->getId(); // Some random/pseudorandom/regular number
        $this->cleaner->autoclean($id, 'updated');
        // OR
        $this->cleaner->autoclean($id); // 'updated' is default
        // or set const `$cleanDateField`
        // $id - required
        // if $id = 10 and `$autocleanEvery` = 10 - then cleanup will be called every time
        // if $id not divisible by 10 and `$autocleanEvery` = 10 - cleanup will not be called
    }
    // ...   

Batch Cleaner

Some settings

    public bool $batchCleanVerbose = false; // verbose output
    public int $cleanBatchPart = 1000; // count deleted items on iteration
    public int $batchItemsCount = 10000; // count all selected items
    public int $batchLoopDelay = 0; // delay between iterations

    // ...
    public function someFunction()
    {
        // ...
        $this->cleaner->batchclean('updated');
        // OR
        $this->cleaner->batchclean(); // 'updated' is default
        // or set const `$cleanDateField`
    }
    // ...