Skip to content

ohmediaorg/cleanup-bundle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Overview

This bundle offers a consistent way for pieces of code to hook into a daily CRON job for the purposes of data cleanup.

Installation

Update composer.json by adding this to the repositories array:

{
    "type": "vcs",
    "url": "https://github.com/ohmediaorg/cleanup-bundle"
}

Then run composer require ohmediaorg/cleanup-bundle:dev-main.

Create the daily CRON job:

0 0 * * * /path/to/php /path/to/symfony/bin/console ohmedia:cleanup

Leverage the Daily CRON Job

Your service should implement CleanerInterface. All your dependancies can be injected as usual via the __construct() function. (You may need to explicitly provide arguments to your service definition.)

<?php

namespace App\Cleanup;

use App\Repository\BlogPostRepository;
use Doctrine\ORM\EntityManager;
use OHMedia\CleanupBundle\Interfaces\CleanerInterface;
use Symfony\Component\Console\Output\OutputInterface;

class BlogPostCleaner implements CleanerInterface
{
    public function __construct(
        private BlogPostRepository $blogPostRepository
    ) {
        $this->blogPostRepository = $blogPostRepository;
    }

    public function __invoke(OutputInterface $output): void
    {
        $blogPosts = $this->blogPostRepository->getOldBlogPosts();

        foreach ($blogPosts as $blogPost) {
            $this->blogPostRepository->remove($blogPost, true);
        }

        $this->em->flush();

        // (optionally) give some feeback via the output interface
        $output->writeln('Blog posts deleted');
    }
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages