Created
June 18, 2019 06:34
-
-
Save lasekmiroslaw/e033830a0a71ae4c396b72130ee21c27 to your computer and use it in GitHub Desktop.
Delete old files command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
namespace App\Command; | |
use Psr\Log\LoggerInterface; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
class DeleteOldFilesCommand extends Command | |
{ | |
public static $defaultName = 'drophub:delete:old:files'; | |
/** | |
* @var string | |
*/ | |
private $filesPath; | |
/** | |
* @var LoggerInterface | |
*/ | |
private $logger; | |
public function __construct(string $filesPath, LoggerInterface $logger, ?string $name = null) | |
{ | |
parent::__construct($name); | |
$this->filesPath = $filesPath; | |
$this->logger = $logger; | |
} | |
public function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$count = 0; | |
foreach (glob($this->filesPath. '/*') as $file) { | |
/*** if file is 24 hours (86400 seconds) old then delete it ***/ | |
if(time() - filemtime($file) > 86400){ | |
unlink($file); | |
++$count; | |
} | |
} | |
$this->logger->info('Deleted old files', ['count' => $count, 'path' => $this->filesPath]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment