<?php
/**
* Copyright (C) SUEZ Smart Solutions - All Rights Reserved
* On’Connect Gateway Management, 2018
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* For the full copyright and license information, please report to the LICENSE CONTRACT
*/
namespace Suez\ReportImportBundle\Service\EventSubscriber;
use League\Flysystem\FileExistsException;
use League\Flysystem\FileNotFoundException;
use League\Flysystem\Filesystem;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class FileRenamer.
*
* @author Ivanis Kouamé <ivanis.kouame@smile.fr>
*/
class FileRenamer implements EventSubscriberInterface
{
/**
* @var array
*/
protected $files;
/**
* @var Filesystem
*/
private $filesystem;
/**
* FileRenamer constructor.
* @param Filesystem $filesystem
*/
public function __construct(Filesystem $filesystem)
{
$this->filesystem = $filesystem;
$this->files = [];
}
/**
* @param $event
*/
public function collectFile($event)
{
$actionName = $event['actionName'];
$sourceName = $event['sourceName'];
$this->files = [$event['file']];
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'read.end.filesystem' => 'collectFiles',
'read.start.file' => 'collectFile',
'process.end' => 'renameProcessedFiles',
ConsoleEvents::EXCEPTION => 'renameErrorFiles'
];
}
/**
* @throws FileExistsException
* @throws FileNotFoundException
*/
public function renameProcessedFiles()
{
$this->renameFiles('.pcs');
}
/**
* @throws FileExistsException
* @throws FileNotFoundException
*/
public function renameErrorFiles()
{
$this->renameFiles('.err');
}
/**
* @param $event
*/
public function collectFiles($event)
{
/*
$actionName = $event['actionName'];
$sourceName = $event['sourceName'];
*/
$this->files = $event['files'];
}
/**
* @param $extension
* @throws FileExistsException
* @throws FileNotFoundException
*/
private function renameFiles($extension)
{
foreach ($this->files as $filePath) {
$this->filesystem->rename($filePath, $filePath . $extension);
}
}
}