Skip to content

Commit

Permalink
Merge pull request #45 from crealoz/dev
Browse files Browse the repository at this point in the history
add logic for result procession
  • Loading branch information
ChristopheFerreboeuf authored Jan 23, 2025
2 parents b247446 + a5b69eb commit 548b5c5
Show file tree
Hide file tree
Showing 21 changed files with 972 additions and 417 deletions.
9 changes: 9 additions & 0 deletions Api/Result/SectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@ public function calculateSize(array $subresults): int;
*/
public function getLine(string $key, mixed $entry): string;

/**
* Get the PHP formated text
*
* @param string $key
* @param array $subResults
* @return string
*/
public function getPHPFormatedText(string $key, array $subResults): string;

}
6 changes: 4 additions & 2 deletions Service/FileSystem/ClassNameGetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class ClassNameGetter
public function __construct(
protected readonly DriverInterface $driver,
protected readonly File $io,
private readonly GetModuleConfig $getModuleConfig,
private readonly ModulePaths $modulePaths,
private readonly ModuleTools $moduleTools
)
Expand Down Expand Up @@ -52,6 +51,9 @@ public function getClassFullNameFromFile($filePathName): string
$namespace = trim($namespace, '\\');

$fileContent = $this->driver->fileGetContents($filePathName);
if ($fileContent == null) {
throw new FileSystemException(__('Could not read the file %1', $filePathName));
}
if (!str_contains($fileContent, 'namespace ' . $namespace)) {
throw new NotAClassException(__('The file %1 does not contain a namespace %2', $filePathName, $namespace));
}
Expand All @@ -73,7 +75,7 @@ private function getNamespaceForVendorModule(string $filePath): string
$moduleXmlPath = $this->modulePaths->getDeclarationXml($filePath, true);
$moduleName = $this->moduleTools->getModuleNameByModuleXml($moduleXmlPath);
$namespaceParts = explode('_', $moduleName);
$namespace = $namespaceParts[0] . DIRECTORY_SEPARATOR . $namespaceParts[1];
$namespace = DIRECTORY_SEPARATOR . $namespaceParts[0] . DIRECTORY_SEPARATOR . $namespaceParts[1];
if (isset($parts[3])) {
for ($i = 3; $i < count($parts); $i++) {
$namespace .= DIRECTORY_SEPARATOR . $parts[$i];
Expand Down
15 changes: 7 additions & 8 deletions Service/PDFWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Crealoz\EasyAudit\Api\Result\SectionInterface;
use Crealoz\EasyAudit\Service\FileSystem\ModulePaths;
use Crealoz\EasyAudit\Service\PDFWriter\SizeCalculation;
use Crealoz\EasyAudit\Service\PDFWriter\SpecificSectionGetter;
use Crealoz\EasyAudit\Service\PDFWriter\StyleManager;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\FileSystemException;
Expand Down Expand Up @@ -49,7 +50,7 @@ public function __construct(
private readonly Reader $moduleReader,
private readonly ModulePaths $modulePaths,
private readonly StyleManager $styleManager,
private readonly array $specificSections = [],
private readonly SpecificSectionGetter $specificSectionGetter,
public int $x = 50,
public int $columnCount = 1
)
Expand Down Expand Up @@ -250,20 +251,18 @@ private function displaySection(string $title, array $section): void
if (isset($entries['specificSections'])) {
$sectionName = $entries['specificSections'];
unset($entries['specificSections']);
if (!isset($this->specificSections[$sectionName]) || !$this->specificSections[$sectionName] instanceof SectionInterface) {
throw new \InvalidArgumentException("Specific section $sectionName is not valid");
}
$specificSection = $this->specificSectionGetter->getSpecificSection($sectionName);
if ($entries['files'] === []) {
continue;
}
$numberOfPages = $this->specificSections[$sectionName]->calculateSize($entries) / 800;
$numberOfPages = $specificSection->calculateSize($entries) / 800;
if ($numberOfPages > 10) {
if (!is_int($numberOfPages)) {
$numberOfPages = (int)ceil($numberOfPages);
}
$this->delegateToAnnex($numberOfPages, $entries, $title, $sectionName);
} else {
$this->specificSections[$sectionName]->writeSection($this, $entries);
$specificSection->writeSection($this, $entries);
}
} else {
$this->manageSubsection($entries);
Expand Down Expand Up @@ -363,9 +362,9 @@ private function writeAnnexes(): void
$this->addPage();
$this->writeTitle('Annex ' . $annexNumber);
$this->writeLine($annex['description']);
if (isset($annex['specificSection']) && isset($this->specificSections[$annex['specificSection']])) {
if (isset($annex['specificSection'])) {
/** @var SectionInterface $specificSection */
$specificSection = $this->specificSections[$annex['specificSection']];
$specificSection = $this->specificSectionGetter->getSpecificSection($annex['specificSection']);
$specificSection->writeSection($this, $annex['files'], true);
} else {
$this->manageFiles($annex['pages'], $annex['files']);
Expand Down
37 changes: 1 addition & 36 deletions Service/PDFWriter/SizeCalculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@ public function __construct(
* size of the title.
*
* @param $subResults
* @param bool $getFirstSection
* @return int
*/
public function calculateTitlePlusFirstSubsectionSize($subResults, $getFirstSection = false): int
public function calculateTitlePlusFirstSubsectionSize($subResults): int
{
if ($getFirstSection) {
$subResults = $this->getFirstSection($subResults);
}
$size = 0;
$size += 44;
$size += $this->calculateSectionIntroSize(reset($subResults));
Expand Down Expand Up @@ -111,37 +107,6 @@ public function getSizeForText($text, int $columnCount = 1): int
return $this->calculateNumberOfLines($text, $columnCount) * $this->lineHeight;
}

/**
* Get the first section of the subresults (errors, warnings or suggestions)
*
* @param $subResults
* @return array
*/
private function getFirstSection($subResults): array
{
if (isset($subResults['errors'])) {
return $subResults['errors'];
} elseif (isset($subResults['warnings'])) {
return $subResults['warnings'];
} elseif (isset($subResults['suggestions'])) {
return $subResults['suggestions'];
}
}

/**
* Calculate the size of the intro section of the PDF. It is not made to be used with the intro of the sections.
*
* @param $intro
* @return int
*/
public function calculateIntroSize($intro): int
{
$size = 0;
$size += $this->calculateNumberOfLines(preg_replace('/\s+/', ' ', $intro['summary'])) * $this->lineHeight;

return $size;
}

/**
* @param $files
* @return int if $sizeForTwoColumns is bigger than $size, return $sizeForTwoColumns
Expand Down
9 changes: 9 additions & 0 deletions Service/PDFWriter/SpecificSection/BlockVMRatio.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,13 @@ public function getLine($key, mixed $entry): string
{
return __('-%1(ratio : %2)', $key, $entry);
}

public function getPHPFormatedText(string $key, array $subResults): string
{
$text = __('Modules') . PHP_EOL;
foreach ($subResults as $module => $ratio) {
$text .= __('-%1(ratio : %2)', $module, $ratio) . PHP_EOL;
}
return $text;
}
}
17 changes: 16 additions & 1 deletion Service/PDFWriter/SpecificSection/SpecificClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(
/**
* @inheritDoc
*/
public function writeContent(PDFWriter $pdfWriter, array $subresults): void
protected function writeContent(PDFWriter $pdfWriter, array $subresults): void
{
$pdfWriter->writeLine('Files:');
foreach ($subresults['files'] as $file => $arguments) {
Expand All @@ -42,8 +42,23 @@ public function calculateSize(array $subresults): int
return $size;
}

/**
* @inheritdoc
*/
public function getLine($key, mixed $entry): string
{
return __('- %1 (potential issues count : %2)', $this->modulePaths->stripVendorOrApp($key), count($entry));
}

/**
* @inheritdoc
*/
public function getPHPFormatedText(string $key, array $subResults): string
{
$text = __('Files') . PHP_EOL;
foreach ($subResults as $file => $arguments) {
$text .= $this->getLine($file, $arguments) . PHP_EOL;
}
return $text;
}
}
11 changes: 10 additions & 1 deletion Service/PDFWriter/SpecificSection/SpecificModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(
/**
* @inheritDoc
*/
public function writeContent(PDFWriter $pdfWriter, array $subresults): void
protected function writeContent(PDFWriter $pdfWriter, array $subresults): void
{
$pdfWriter->writeLine('Files:');
foreach ($subresults['files'] as $file => $arguments) {
Expand Down Expand Up @@ -54,4 +54,13 @@ public function getLine($key, mixed $entry): string
}
return $text;
}

public function getPHPFormatedText(string $key, array $subResults): string
{
$text = __('Files') . PHP_EOL;
foreach ($subResults as $file => $arguments) {
$text .= $this->getLine($file, $arguments) . PHP_EOL;
}
return $text;
}
}
11 changes: 10 additions & 1 deletion Service/PDFWriter/SpecificSection/UnusedModules.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class UnusedModules extends AbstractSection implements SectionInterface
/**
* @inheritDoc
*/
public function writeContent(PDFWriter $pdfWriter, array $subresults): void
protected function writeContent(PDFWriter $pdfWriter, array $subresults): void
{
$pdfWriter->writeLine(__('Modules:'));
foreach ($subresults['files'] as $module) {
Expand All @@ -38,4 +38,13 @@ public function getLine($key, mixed $entry): string
unset($key);
return '-' . $entry;
}

public function getPHPFormatedText(string $key, array $subResults): string
{
$text = __('Modules') . PHP_EOL;
foreach ($subResults as $module) {
$text .= '-' . $module . PHP_EOL;
}
return $text;
}
}
27 changes: 27 additions & 0 deletions Service/PDFWriter/SpecificSectionGetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Crealoz\EasyAudit\Service\PDFWriter;

use Crealoz\EasyAudit\Api\Result\SectionInterface;

class SpecificSectionGetter
{
public function __construct(
private readonly array $specificSections = []
)
{
}

/**
* @param string $sectionName
* @return SectionInterface
* @throws \InvalidArgumentException
*/
public function getSpecificSection(string $sectionName): SectionInterface
{
if (!isset($this->specificSections[$sectionName]) || !$this->specificSections[$sectionName] instanceof SectionInterface) {
throw new \InvalidArgumentException("Section $sectionName not found");
}
return $this->specificSections[$sectionName];
}
}
19 changes: 8 additions & 11 deletions Test/Integration/Service/PDFWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ class PDFWriterTest extends TestCase
*/
private PDFWriter\StyleManager $styleManager;

private $mockedStyleManager;

protected function setUp(): void
{
$this->filesystem = $this->createMock(Filesystem::class);
Expand All @@ -48,11 +46,9 @@ protected function setUp(): void
$this->moduleReader = $this->createMock(Reader::class);
$this->modulePaths = new ModulePaths($this->filesystem);
$this->styleManager = new PDFWriter\StyleManager();
$this->BlockVsVMRatio = $this->createMock(PDFWriter\SpecificSection\BlockVMRatio::class);

$this->mockedPage = $this->createMock(\Zend_Pdf_Page::class);
$this->mockedStyleManager = $this->createMock(\Crealoz\EasyAudit\Service\PDFWriter\StyleManager::class);

$this->specificSectionGetter = $this->createMock(PDFWriter\SpecificSectionGetter::class);

$this->pdfWriter = new PDFWriter(
$this->filesystem,
Expand All @@ -61,9 +57,7 @@ protected function setUp(): void
$this->moduleReader,
$this->modulePaths,
$this->styleManager,
[
'manageBlockVMRatio' => $this->BlockVsVMRatio
],
$this->specificSectionGetter,
50,
5
);
Expand Down Expand Up @@ -127,6 +121,11 @@ public function testCreatedPDF(): void
];

$filename = 'test';
$this->specificSectionGetter
->expects($this->once())
->method('getSpecificSection')
->with('manageBlockVMRatio')
->willReturn($this->createMock(PDFWriter\SpecificSection\BlockVMRatio::class));

$tempDir = sys_get_temp_dir() . '/pdf_test';
if (!is_dir($tempDir)) {
Expand Down Expand Up @@ -228,10 +227,8 @@ protected function tearDown(): void
unset($this->moduleReader);
unset($this->modulePaths);
unset($this->styleManager);
unset($this->BlockVsVMRatio);
unset($this->mockedPage);
unset($this->mockedStyleManager);

unset($this->specificSectionGetter);
}

}
Loading

0 comments on commit 548b5c5

Please sign in to comment.