Skip to content

Commit

Permalink
Add command to refresh database from real files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Warxcell committed May 12, 2020
1 parent bc2aefb commit 9d7b120
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
63 changes: 63 additions & 0 deletions Command/RefreshDatabaseCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);

namespace Arxy\FilesBundle\Command;

use Arxy\FilesBundle\Manager;
use Arxy\FilesBundle\Model\File;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class RefreshDatabaseCommand extends Command
{
protected static $defaultName = 'arxy:files:refresh-database';

/** @var Manager */
private $fileManager;

/** @var ManagerRegistry */
private $doctrine;

public function __construct(Manager $fileManager, ManagerRegistry $registry)
{
parent::__construct();
$this->fileManager = $fileManager;
$this->doctrine = $registry;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$progressBar = new ProgressBar($output);

$batchSize = 20;
$i = 1;

$em = $this->doctrine->getManagerForClass($this->fileManager->getClass());

$queryBuilder = $em->createQueryBuilder();
$queryBuilder->select('file')->from($this->fileManager->getClass(), 'file');

$iterableResult = $queryBuilder->getQuery()->iterate();
foreach ($iterableResult as $row) {
/** @var File $file */
$file = $row[0];

$this->fileManager->refresh($file);

if (($i % $batchSize) === 0) {
$em->flush();
$em->clear();
}
++$i;

$progressBar->advance();
}

$em->flush();

$progressBar->finish();
}
}
12 changes: 12 additions & 0 deletions Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,16 @@ public function readStream(File $file)
return $this->filesystem->readStream($pathname);
}
}

public function refresh(File $file)
{
$file->setFileSize($this->filesystem->getSize($this->getPathname($file)));
$file->setMd5Hash(md5($this->filesystem->read($this->getPathname($file))));
$file->setMimeType($this->filesystem->getMimetype($this->getPathname($file)));
}

public function getClass()
{
return $this->class;
}
}

0 comments on commit 9d7b120

Please sign in to comment.