<?php
namespace AWEdmBundle\Command;
use AWEdmBundle\Entity\File;
use AWEdmBundle\Exception\MigrationException;
use AWEdmBundle\Factory\FileSystemFactory;
use AWEdmBundle\Manager\DocumentManager;
use AWEdmBundle\Migration\CmsMigration;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Script de migration des fichiers du CMS vers le GED.
*/
class AwsS3MigrationCommand extends ContainerAwareCommand
{
/**
* @inheritdoc
*/
protected function configure()
{
$this
->setName('awedm:aws_s3:migration')
->setDescription('Lance la migration des fichiers de la GED vers S3 ou inversement')
->addArgument('action', InputArgument::OPTIONAL, 'upload (default) or download')
;
}
/**
* @inheritdoc
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var File[] $files */
$files = $this->getContainer()->get('doctrine')->getManager()->getRepository("AWEdmBundle:File")->findAll();
/** @var DocumentManager $documentManger */
$documentManger = $this->getContainer()->get('awedm.manager.document');
if($input->getArgument('action') == null or $input->getArgument('action') == 'upload'){
$progress = new ProgressBar($output, count($files));
foreach ($files as $file){
if($file->getStorage() !== FileSystemFactory::AMAZON_S3){
$saved = $documentManger->changeStorageTo($file, FileSystemFactory::AMAZON_S3);
if($saved){
$output->writeln($file->getStorage().' =>'. $file->getPath().' #'.$file->getId());
}else{
$output->writeln('<bg=yellow;fg=black>'.$file->getStorage().' =>'. $file->getPath().' #'.$file->getId().'</>');
}
}
$progress->advance();
}
$progress->finish();
}
elseif ($input->getArgument('action') == 'download'){
$progress = new ProgressBar($output, count($files));
$progress->start();
foreach ($files as $file){
if($file->getStorage() !== FileSystemFactory::LOCAL){
$saved = $documentManger->changeStorageTo($file, FileSystemFactory::LOCAL);
if($saved){
$output->writeln($file->getStorage().' => '. $file->getPath().' #'.$file->getId());
}else{
$output->writeln('<bg=yellow;fg=black>'.$file->getStorage().' =>'. $file->getPath().' #'.$file->getId().'</>');
}
}
$progress->advance();
}
$progress->finish();
}
$output->writeln('');
}
}