getDisk()->exists($filename)) { $this->log( sprintf( 'File %s could not be moved to invalid (not found)', $filename ), LogType::warning() ); return; } if ($this->getDisk()->exists(sprintf('%s/%s', $this->invalidFolderName, $filename))) { $ext = \pathinfo($filename, \PATHINFO_EXTENSION); $newFileName = \sprintf( '%s_%d.%s', \substr($filename, 0, 0 - (strlen($ext) + 1)), \time(), $ext ); } try { if (!$this->getDisk()->move($filename, sprintf('%s/%s', $this->invalidFolderName, $newFileName))) { $this->log( sprintf( 'File %s could not be moved to invalid', $filename ), LogType::warning() ); } } catch (\Exception $e) { $this->log( sprintf( 'File %s could not be moved to invalid', $filename ), LogType::warning() ); } } /** * Suppression des fichiers dans * invalid qui ont plus de 6 mois * * @return void */ protected function clearOldInvalids(): void { $disk = $this->getDisk(); $invalids = $disk->files($this->invalidFolderName); // /** @var File */ foreach ($invalids as $invalid) { $pi = \pathinfo($invalid, \PATHINFO_BASENAME); if (in_array($pi, $this->ignoreFiles)) { continue; } if ($disk->lastModified($invalid) < strtotime(Carbon::now()->subMonth(6))) { if (!$disk->delete($invalid)) { Log::critical(sprintf('%s : Could not delete file %s', get_class($this), $invalid)); }; } } } /** * Move the file to archives * * @param string $filename * @return void */ protected function moveToArchives(string $filename): void { $newFileName = $filename; if (!$this->getDisk()->exists($filename)) { $this->log( sprintf( 'File %s could not be moved to archives (not found)', $filename ), LogType::warning() ); return; } if ($this->getDisk()->exists(sprintf('%s/%s', $this->archiveFolderName, $filename))) { $ext = \pathinfo($filename, \PATHINFO_EXTENSION); $newFileName = \sprintf( '%s_%d.%s', \substr($filename, 0, 0 - (strlen($ext) + 1)), \time(), $ext ); } try { if (!$this->getDisk()->move($filename, sprintf('%s/%s', $this->archiveFolderName, $newFileName))) { $this->log( sprintf( 'File %s could not be moved to archives', $filename ), LogType::warning() ); } } catch (\Exception $e) { $this->log( sprintf( 'File %s could not be moved to archives', $filename ), LogType::warning() ); } } /** * Suppression des fichiers dans * archives qui ont plus de 2j * * @return void */ protected function clearOldArchives(): void { $disk = $this->getDisk(); $archives = $disk->files($this->archiveFolderName); // /** @var File */ foreach ($archives as $archive) { $pi = \pathinfo($archive, \PATHINFO_BASENAME); if (in_array($pi, $this->ignoreFiles)) { continue; } if ($disk->lastModified($archive) < strtotime(Carbon::now()->subDays(2))) { if (!$disk->delete($archive)) { Log::critical(sprintf('%s : Could not delete file %s', get_class($this), $archive)); }; } } } /** * Get the concerned disk * * @return Filesystem */ protected function getDisk(): Filesystem { return Storage::disk($this->disk); } /** * Logs data * * @param string $message * @param LogType $logtype * @return void * @throws \ErrorException If logtype is not handled. */ protected function log(string $message, LogType $logtype = null): void { if (!$logtype) { $logtype = LogType::debug(); } $message = sprintf('%s : %s', get_class($this), $message); switch ($logtype->value) { case LogType::debug()->value: Log::debug($message); break; case LogType::info()->value: Log::info($message); break; case LogType::log()->value: Log::log($message); break; case LogType::warning()->value: Log::warning($message); break; case LogType::error()->value: Log::error($message); break; case LogType::critical()->value: Log::critical($message); break; default: throw new \ErrorException(sprintf('%s : unhandled log type %s', get_class($this), $logtype->label)); } //end switch $this->consolePrint($message, $logtype); } /** * Prints to the console * * @param string $message * @param LogType $logtype * @return void * @throws \ErrorException If logtype is not handled. */ protected function consolePrint(string $message, LogType $logtype): void { $this->clearProgressBarFromLine(); if ($this->console) { switch ($logtype->value) { case LogType::debug()->value: case LogType::info()->value: case LogType::log()->value: $this->console->info($message); break; case LogType::warning()->value: $this->console->comment($message); break; case LogType::error()->value: case LogType::critical()->value: $this->console->error($message); break; default: throw new \ErrorException(sprintf('%s : unhandled log type %s', get_class($this), $logtype->label)); } } } /** * Start ProgressBar * * @param integer $total * @return void */ protected function startProgressBar(int $total) { if ($this->console) { $this->progressBar = $this->console->getOutput() ->createProgressBar($total); $this->progressBar->start(); } } /** * Clear the line to write someting * * @return void */ protected function clearProgressBarFromLine() { if ($this->progressBar) { $this->progressBar->clear(); } } /** * Increment 1 on progress bar * * @return void */ protected function incrementProgressBar() { if ($this->progressBar) { $this->progressBar->advance(); } } /** * End the progress bar * * @return void */ protected function endProgressBar() { if ($this->progressBar) { $this->progressBar->finish(); } } }