src/Suez/ReportImportBundle/Service/EventSubscriber/FileRenamer.php line 72

Open in your IDE?
  1. <?php
  2. /**
  3. * Copyright (C) SUEZ Smart Solutions - All Rights Reserved
  4. * On’Connect Gateway Management, 2018
  5. * Unauthorized copying of this file, via any medium is strictly prohibited
  6. * Proprietary and confidential
  7. * For the full copyright and license information, please report to the LICENSE CONTRACT
  8. */
  9. namespace Suez\ReportImportBundle\Service\EventSubscriber;
  10. use League\Flysystem\FileExistsException;
  11. use League\Flysystem\FileNotFoundException;
  12. use League\Flysystem\Filesystem;
  13. use Symfony\Component\Console\ConsoleEvents;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. /**
  16.  * Class FileRenamer.
  17.  *
  18.  * @author Ivanis KouamĂ© <ivanis.kouame@smile.fr>
  19.  */
  20. class FileRenamer implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * @var array
  24.      */
  25.     protected $files;
  26.     /**
  27.      * @var Filesystem
  28.      */
  29.     private $filesystem;
  30.     /**
  31.      * FileRenamer constructor.
  32.      * @param Filesystem $filesystem
  33.      */
  34.     public function __construct(Filesystem $filesystem)
  35.     {
  36.         $this->filesystem $filesystem;
  37.         $this->files = [];
  38.     }
  39.     /**
  40.      * @param $event
  41.      */
  42.     public function collectFile($event)
  43.     {
  44.         $actionName $event['actionName'];
  45.         $sourceName $event['sourceName'];
  46.         $this->files = [$event['file']];
  47.     }
  48.     /**
  49.      * @return array
  50.      */
  51.     public static function getSubscribedEvents()
  52.     {
  53.         return [
  54.             'read.end.filesystem' => 'collectFiles',
  55.             'read.start.file' => 'collectFile',
  56.             'process.end' => 'renameProcessedFiles',
  57.             ConsoleEvents::EXCEPTION => 'renameErrorFiles'
  58.         ];
  59.     }
  60.     /**
  61.      * @throws FileExistsException
  62.      * @throws FileNotFoundException
  63.      */
  64.     public function renameProcessedFiles()
  65.     {
  66.         $this->renameFiles('.pcs');
  67.     }
  68.     /**
  69.      * @throws FileExistsException
  70.      * @throws FileNotFoundException
  71.      */
  72.     public function renameErrorFiles()
  73.     {
  74.         $this->renameFiles('.err');
  75.     }
  76.     /**
  77.      * @param $event
  78.      */
  79.     public function collectFiles($event)
  80.     {
  81.         /*
  82.         $actionName = $event['actionName'];
  83.         $sourceName = $event['sourceName'];
  84.          */
  85.         $this->files $event['files'];
  86.     }
  87.     /**
  88.      * @param $extension
  89.      * @throws FileExistsException
  90.      * @throws FileNotFoundException
  91.      */
  92.     private function renameFiles($extension)
  93.     {
  94.         foreach ($this->files as $filePath) {
  95.             $this->filesystem->rename($filePath$filePath $extension);
  96.         }
  97.     }
  98. }