forked from ems-project/elasticms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '5.x' into test/image_processor
- Loading branch information
Showing
9 changed files
with
254 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\CLI\Client\File; | ||
|
||
use App\CLI\Client\Report\AbstractReport; | ||
|
||
class Report extends AbstractReport | ||
{ | ||
/** @var string[][] */ | ||
private array $warnings = [['type', 'file', 'message']]; | ||
|
||
public function addWarning(string $type, string $filename, string $message): void | ||
{ | ||
$this->warnings[] = [ | ||
$type, | ||
$filename, | ||
$message, | ||
]; | ||
} | ||
|
||
/** | ||
* @return string[][] | ||
*/ | ||
public function getWarnings(): array | ||
{ | ||
return $this->warnings; | ||
} | ||
|
||
/** | ||
* @return array{array{name: string, rows: string[][]}} | ||
*/ | ||
protected function getSheets(): array | ||
{ | ||
return [ | ||
[ | ||
'name' => 'Warnings', | ||
'rows' => \array_values($this->warnings), | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\CLI\Client\Report; | ||
|
||
use EMS\CommonBundle\Common\SpreadsheetGeneratorService; | ||
use EMS\CommonBundle\Contracts\SpreadsheetGeneratorServiceInterface; | ||
use Symfony\Component\HttpFoundation\HeaderUtils; | ||
|
||
abstract class AbstractReport | ||
{ | ||
private readonly SpreadsheetGeneratorService $spreadsheetGeneratorService; | ||
|
||
public function __construct() | ||
{ | ||
$this->spreadsheetGeneratorService = new SpreadsheetGeneratorService(); | ||
} | ||
|
||
public function generateXslxReport(): string | ||
{ | ||
$config = [ | ||
SpreadsheetGeneratorServiceInterface::CONTENT_DISPOSITION => HeaderUtils::DISPOSITION_ATTACHMENT, | ||
SpreadsheetGeneratorServiceInterface::WRITER => SpreadsheetGeneratorServiceInterface::XLSX_WRITER, | ||
SpreadsheetGeneratorServiceInterface::CONTENT_FILENAME => 'Audit-Report.xlsx', | ||
SpreadsheetGeneratorServiceInterface::SHEETS => $this->getSheets(), | ||
]; | ||
$tmpFilename = \tempnam(\sys_get_temp_dir(), 'Audit-Report-').'.xlsx'; | ||
if (!\is_string($tmpFilename)) { | ||
throw new \RuntimeException('Not able to generate a temporary filename'); | ||
} | ||
$this->spreadsheetGeneratorService->generateSpreadsheetFile($config, $tmpFilename); | ||
|
||
return $tmpFilename; | ||
} | ||
|
||
/** | ||
* @return array{array{name: string, rows: string[][]}} | ||
*/ | ||
abstract protected function getSheets(): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\CLI\Command\File; | ||
|
||
use App\CLI\Client\File\Report; | ||
use App\CLI\Commands; | ||
use EMS\CommonBundle\Common\Command\AbstractCommand; | ||
use EMS\Helpers\File\File; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Logger\ConsoleLogger; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Finder\Finder; | ||
use Symfony\Component\Mime\MimeTypes; | ||
|
||
class AuditFileCommand extends AbstractCommand | ||
{ | ||
protected static $defaultName = Commands::FILE_AUDIT; | ||
final public const UPPERCASE_EXTENSION = 'ExtensionWithUppercase'; | ||
final public const EXTENSION_MISMATCH = 'ExtensionMismatch'; | ||
|
||
private const ARG_FOLDER = 'folder'; | ||
private ConsoleLogger $logger; | ||
private string $folder; | ||
private MimeTypes $mimeTypes; | ||
private Report $report; | ||
|
||
public function __construct() | ||
{ | ||
$this->mimeTypes = new MimeTypes(); | ||
$this->report = new Report(); | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setDescription('Audit files in a folder structure') | ||
->addArgument( | ||
self::ARG_FOLDER, | ||
InputArgument::REQUIRED, | ||
'Path of the folder structure' | ||
); | ||
} | ||
|
||
protected function initialize(InputInterface $input, OutputInterface $output): void | ||
{ | ||
parent::initialize($input, $output); | ||
$this->logger = new ConsoleLogger($output); | ||
$this->folder = $this->getArgumentString(self::ARG_FOLDER); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$this->io->title(\sprintf('Audit files in %s', $this->folder)); | ||
$finder = new Finder(); | ||
$finder->files()->in($this->folder); | ||
|
||
if (!$finder->hasResults()) { | ||
throw new \RuntimeException('No files found!'); | ||
} | ||
$this->io->comment(\sprintf('%d files located', $finder->count())); | ||
$progressBar = $this->io->createProgressBar($finder->count()); | ||
foreach ($finder as $file) { | ||
$pathInStructure = \substr($file->getPathname(), \strlen($this->folder)); | ||
$info = new File($file); | ||
$extension = \strtolower($info->extension); | ||
if ($extension !== $info->extension) { | ||
$this->log(self::UPPERCASE_EXTENSION, $pathInStructure, \sprintf('The extension %s contains uppercase', $info->extension)); | ||
} | ||
if (!\in_array($extension, $this->mimeTypes->getExtensions($info->mimeType))) { | ||
$this->log(self::EXTENSION_MISMATCH, $pathInStructure, \sprintf('The extension %s mismatch with the mime type %s', $info->extension, $info->mimeType)); | ||
} | ||
$progressBar->advance(); | ||
} | ||
|
||
$progressBar->finish(); | ||
$this->io->newLine(); | ||
$this->io->writeln(\sprintf('Audit report: %s', $this->report->generateXslxReport())); | ||
|
||
return self::EXECUTE_SUCCESS; | ||
} | ||
|
||
private function log(string $type, string $filename, string $message): void | ||
{ | ||
$this->report->addWarning($type, $filename, $message); | ||
$this->logger->warning(\sprintf('%s: %s', $filename, $message)); | ||
} | ||
} |
Oops, something went wrong.