From 478659d6e29328dcb98b4b93112623f7ceb5529f Mon Sep 17 00:00:00 2001 From: fridev Date: Sun, 16 Mar 2025 20:56:40 +0100 Subject: [PATCH 01/11] add worksheet in extractService constructor --- src/Imports/Themes/ExtractService.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Imports/Themes/ExtractService.php b/src/Imports/Themes/ExtractService.php index 55f040f..c5f4f3a 100644 --- a/src/Imports/Themes/ExtractService.php +++ b/src/Imports/Themes/ExtractService.php @@ -3,19 +3,22 @@ namespace App\Imports\Themes; use App\Entity\Theme; -use Doctrine\ORM\EntityManagerInterface; use PhpOffice\PhpSpreadsheet\IOFactory; +use Doctrine\ORM\EntityManagerInterface; +use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use Symfony\Component\Filesystem\Exception\FileNotFoundException; class ExtractService { private $entityManager; private $themeRepository; - - public function __construct(EntityManagerInterface $entityManager) + private $worksheet; + + public function __construct(EntityManagerInterface $entityManager, Worksheet $worksheet) { $this->entityManager = $entityManager; $this->themeRepository = $entityManager->getRepository(Theme::class); + $this->worksheet = $worksheet; } /** From df6473f73ec7dfa55c8c7d83dba6051ffd44d390 Mon Sep 17 00:00:00 2001 From: fridev Date: Sun, 16 Mar 2025 21:08:04 +0100 Subject: [PATCH 02/11] add worksheet in ExtractServiceTest --- tests/Imports/Themes/ExtractServiceTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/Imports/Themes/ExtractServiceTest.php b/tests/Imports/Themes/ExtractServiceTest.php index 4364af9..26f695f 100644 --- a/tests/Imports/Themes/ExtractServiceTest.php +++ b/tests/Imports/Themes/ExtractServiceTest.php @@ -4,6 +4,7 @@ use App\Entity\Theme; use App\Imports\Themes\ExtractService; +use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; class ExtractServiceTest extends KernelTestCase @@ -11,6 +12,7 @@ class ExtractServiceTest extends KernelTestCase private $entityManager; private $themeRepository; private $projectDir; + private $worksheet; protected function setUp(): void { @@ -20,6 +22,7 @@ protected function setUp(): void $this->entityManager = $container->get('doctrine.orm.entity_manager'); $this->themeRepository = $this->entityManager->getRepository(Theme::class); $this->projectDir = $container->getParameter('kernel.project_dir'); + $this->worksheet = $this->createMock(Worksheet::class); } protected function tearDown(): void @@ -34,7 +37,7 @@ protected function tearDown(): void public function testImportThemeSave(): void { - $ExtractServices = new ExtractService($this->entityManager); + $ExtractServices = new ExtractService(entityManager: $this->entityManager, worksheet: $this->worksheet); // TODO: change this to inject the spreadsheet so that we can test the integration more easily. $excel_file = $this->projectDir.'/tests/Imports/Themes/test-themes.xlsx'; $themes = $ExtractServices->GetThemesFromExcelFile($excel_file); From fddbf34ecfca40409eead6d943ba61b57c7b2c4a Mon Sep 17 00:00:00 2001 From: fridev Date: Sun, 16 Mar 2025 21:56:58 +0100 Subject: [PATCH 03/11] add worksheek completly done --- src/Imports/Themes/ExtractService.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Imports/Themes/ExtractService.php b/src/Imports/Themes/ExtractService.php index c5f4f3a..3c0cce0 100644 --- a/src/Imports/Themes/ExtractService.php +++ b/src/Imports/Themes/ExtractService.php @@ -26,13 +26,18 @@ public function __construct(EntityManagerInterface $entityManager, Worksheet $wo * * @return array themes import from the excel file */ - public function GetThemesFromExcelFile(string $excel_file): array + public function GetThemesFromExcelFile(string $pathSheet): array { $themes = []; - if (!file_exists($excel_file)) { - throw new FileNotFoundException(sprintf('Excel file "%s" not found', $excel_file)); + + if(pathinfo(basename($pathSheet), PATHINFO_EXTENSION) !== 'xlsx') { + throw new \Exception('The file must be an Excel file'); } - $spreadsheet = IOFactory::load($excel_file); + if (!file_exists($pathSheet)) { + throw new FileNotFoundException(sprintf('Excel file "%s" not found', $pathSheet)); + } + + $spreadsheet = IOFactory::load($pathSheet); $sheet = $spreadsheet->getActiveSheet(); foreach ($sheet->getRowIterator() as $row) { From 7521fa247faaca51f6da6affab05804110074838 Mon Sep 17 00:00:00 2001 From: fridev Date: Sun, 16 Mar 2025 22:08:31 +0100 Subject: [PATCH 04/11] remove excel_file --- src/Command/CommandExtractService.php | 6 +++++- src/Imports/Themes/ExtractService.php | 8 ++++---- tests/Imports/Themes/ExtractServiceTest.php | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Command/CommandExtractService.php b/src/Command/CommandExtractService.php index a9ebbbc..57cc32f 100644 --- a/src/Command/CommandExtractService.php +++ b/src/Command/CommandExtractService.php @@ -4,6 +4,7 @@ use App\Imports\Themes\ExtractService; use Doctrine\ORM\EntityManagerInterface; +use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; @@ -19,11 +20,13 @@ class CommandExtractService extends Command { private $projectDir; private $entityManager; + private $worksheet; public function __construct(string $projectDir, EntityManagerInterface $entityManager) { $this->projectDir = $projectDir; $this->entityManager = $entityManager; + parent::__construct(); } @@ -37,7 +40,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); $extracthemes = $input->getArgument('extracthemes'); - $extract_service = new ExtractService($this->entityManager); + $worksheet = new Worksheet(); + $extract_service = new ExtractService($this->entityManager, $worksheet); if ($extracthemes) { $excel_file = $this->projectDir.'/var/import-data/emissions_GES_structure.xlsx'; diff --git a/src/Imports/Themes/ExtractService.php b/src/Imports/Themes/ExtractService.php index 3c0cce0..eef36d4 100644 --- a/src/Imports/Themes/ExtractService.php +++ b/src/Imports/Themes/ExtractService.php @@ -3,8 +3,8 @@ namespace App\Imports\Themes; use App\Entity\Theme; -use PhpOffice\PhpSpreadsheet\IOFactory; use Doctrine\ORM\EntityManagerInterface; +use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use Symfony\Component\Filesystem\Exception\FileNotFoundException; @@ -13,7 +13,7 @@ class ExtractService private $entityManager; private $themeRepository; private $worksheet; - + public function __construct(EntityManagerInterface $entityManager, Worksheet $worksheet) { $this->entityManager = $entityManager; @@ -29,8 +29,8 @@ public function __construct(EntityManagerInterface $entityManager, Worksheet $wo public function GetThemesFromExcelFile(string $pathSheet): array { $themes = []; - - if(pathinfo(basename($pathSheet), PATHINFO_EXTENSION) !== 'xlsx') { + + if ('xlsx' !== pathinfo(basename($pathSheet), PATHINFO_EXTENSION)) { throw new \Exception('The file must be an Excel file'); } if (!file_exists($pathSheet)) { diff --git a/tests/Imports/Themes/ExtractServiceTest.php b/tests/Imports/Themes/ExtractServiceTest.php index 26f695f..68df65a 100644 --- a/tests/Imports/Themes/ExtractServiceTest.php +++ b/tests/Imports/Themes/ExtractServiceTest.php @@ -12,7 +12,7 @@ class ExtractServiceTest extends KernelTestCase private $entityManager; private $themeRepository; private $projectDir; - private $worksheet; + private $worksheet; protected function setUp(): void { From 7c5f5b239de263b423d3f53efacfdc5e5f7cd01e Mon Sep 17 00:00:00 2001 From: fridev Date: Wed, 19 Mar 2025 01:51:37 +0100 Subject: [PATCH 05/11] add class ThemeReader --- src/Command/CommandExtractService.php | 96 +++++++++++++++++++-------- src/Imports/Themes/ThemeReader.php | 37 +++++++++++ 2 files changed, 104 insertions(+), 29 deletions(-) create mode 100644 src/Imports/Themes/ThemeReader.php diff --git a/src/Command/CommandExtractService.php b/src/Command/CommandExtractService.php index 57cc32f..332b560 100644 --- a/src/Command/CommandExtractService.php +++ b/src/Command/CommandExtractService.php @@ -3,8 +3,10 @@ namespace App\Command; use App\Imports\Themes\ExtractService; +use App\ThemeReader; use Doctrine\ORM\EntityManagerInterface; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; +use PhpOffice\PhpSpreadsheet\IOFactory; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; @@ -18,56 +20,92 @@ )] class CommandExtractService extends Command { - private $projectDir; + private $projectDir; private $entityManager; - private $worksheet; + private ?Worksheet $sheet = null; public function __construct(string $projectDir, EntityManagerInterface $entityManager) { $this->projectDir = $projectDir; $this->entityManager = $entityManager; - parent::__construct(); } - protected function configure(): void - { - $this - ->addArgument('extracthemes', InputArgument::OPTIONAL, 'extract and save themes into database themes'); - } + // protected function configure(): void + // { + // $this + // ->addArgument('extracthemes', InputArgument::OPTIONAL, 'extract and save themes into database themes'); + // $this ->addArgument('extract1', InputArgument::OPTIONAL, 'extract and save themes into database themes'); + // $this ->addArgument('extract2', InputArgument::OPTIONAL, 'extract and save themes into database themes'); + + + // } protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); - $extracthemes = $input->getArgument('extracthemes'); - $worksheet = new Worksheet(); - $extract_service = new ExtractService($this->entityManager, $worksheet); - if ($extracthemes) { - $excel_file = $this->projectDir.'/var/import-data/emissions_GES_structure.xlsx'; + $excle_file = $this->projectDir.'/var/import-data/emissions_GES_structure.xlsx'; + if (!file_exists($excle_file)) { + $io->error('file does not exist'); + return Command::FAILURE; + } + $spreadsheet = IOFactory::load($excle_file); + $this->sheet = $spreadsheet->getActiveSheet(); + $readtheme = new ThemeReader($this->sheet); + $themes = $readtheme->ingest(); + $io->info(count($themes).' themes extracted'); + $extract_service = new ExtractService($this->entityManager, $this->sheet); - if (!file_exists($excel_file)) { - $io->error('file does not exist'); + $prepared_themes = $extract_service->PrepareThemesForDatabase($themes); + $saved_themes_count = $extract_service->SaveThemesOnDatabase($prepared_themes); + $io->info("$saved_themes_count themes were upserted successfuly"); + + - return Command::FAILURE; - } + // $spreadsheet = IOFactory::load($excle_file); + // $this->sheet = $spreadsheet->getActiveSheet(); + // $themes = $this->readTheme(); + // $io->info(count($themes).' themes extracted'); + // $this->sheet = $spreadsheet->getActiveSheet(); + // $extracthemes = $input->getArgument('extracthemes'); + // $extract_service = new ExtractService($this->entityManager, $this->sheet); - try { - $extracted_themes = $extract_service->GetThemesFromExcelFile($excel_file); - $io->info(count($extracted_themes).' themes extracted'); - $prepared_themes = $extract_service->PrepareThemesForDatabase($extracted_themes); - $saved_themes_count = $extract_service->SaveThemesOnDatabase($prepared_themes); + // $io->info('sheet'); + // $readTheme = new ThemeReader($worksheet); + - $io->info("$saved_themes_count themes were upserted successfuly"); - } catch (\Exception $e) { - $io->error('File Excel failed to read : '.$e->getMessage()); + // if ($extracthemes) { + // $excel_file = $this->projectDir.'/var/import-data/emissions_GES_structure.xlsx'; - return Command::FAILURE; - } + // if (!file_exists($excel_file)) { + // $io->error('file does not exist'); - return Command::SUCCESS; - } + // return Command::FAILURE; + // } + // $spreadsheet = IOFactory::load($excel_file); + // $sheet = $spreadsheet->getActiveSheet(); + + + // try { + // $themes = $readTheme->ingest(); + // // $extracted_themes = $extract_service->GetThemesFromExcelFile($excel_file); + // $io->info(count($themes).' themes extracted'); + // $prepared_themes = $extract_service->PrepareThemesForDatabase($themes); + // $saved_themes_count = $extract_service->SaveThemesOnDatabase($prepared_themes); + + // $io->info("$saved_themes_count themes were upserted successfuly"); + // } catch (\Exception $e) { + // $io->error('File Excel failed to read : '.$e->getMessage()); + + // return Command::FAILURE; + // } + + // return Command::SUCCESS; + // } return Command::SUCCESS; } + + } diff --git a/src/Imports/Themes/ThemeReader.php b/src/Imports/Themes/ThemeReader.php new file mode 100644 index 0000000..9a63370 --- /dev/null +++ b/src/Imports/Themes/ThemeReader.php @@ -0,0 +1,37 @@ +sheet->getRowIterator() as $row) { + $rowIndex = $row->getRowIndex(); + $name = $this->sheet->getCell('A'.$rowIndex)->getValue(); + $id = $this->sheet->getCell('B'.$rowIndex)->getValue(); + + if (null !== $id && null !== $name) { + if ($rowIndex > 2) { + $themes[] = [ + 'id' => $id, + 'name' => $name, + ]; + } + } + } + + return $themes; + } + + +} From 66e2450eb41fb2de7d06e3d1b917f8775f05a3dc Mon Sep 17 00:00:00 2001 From: fridev Date: Wed, 19 Mar 2025 02:18:18 +0100 Subject: [PATCH 06/11] ExtractService command works --- src/Command/CommandExtractService.php | 2 +- src/Imports/Themes/ThemeReader.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Command/CommandExtractService.php b/src/Command/CommandExtractService.php index 332b560..f00d210 100644 --- a/src/Command/CommandExtractService.php +++ b/src/Command/CommandExtractService.php @@ -56,7 +56,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $themes = $readtheme->ingest(); $io->info(count($themes).' themes extracted'); $extract_service = new ExtractService($this->entityManager, $this->sheet); - + //dd($themes); $prepared_themes = $extract_service->PrepareThemesForDatabase($themes); $saved_themes_count = $extract_service->SaveThemesOnDatabase($prepared_themes); $io->info("$saved_themes_count themes were upserted successfuly"); diff --git a/src/Imports/Themes/ThemeReader.php b/src/Imports/Themes/ThemeReader.php index 9a63370..3307854 100644 --- a/src/Imports/Themes/ThemeReader.php +++ b/src/Imports/Themes/ThemeReader.php @@ -18,12 +18,12 @@ public function ingest(): array foreach ($this->sheet->getRowIterator() as $row) { $rowIndex = $row->getRowIndex(); $name = $this->sheet->getCell('A'.$rowIndex)->getValue(); - $id = $this->sheet->getCell('B'.$rowIndex)->getValue(); + $external_id = $this->sheet->getCell('B'.$rowIndex)->getValue(); - if (null !== $id && null !== $name) { + if (null !== $external_id && null !== $name) { if ($rowIndex > 2) { $themes[] = [ - 'id' => $id, + 'externalId' => $external_id, 'name' => $name, ]; } From cfd5ceb2680cca4c5710844329c9d97b0c6b5389 Mon Sep 17 00:00:00 2001 From: fridev Date: Thu, 20 Mar 2025 10:56:49 +0100 Subject: [PATCH 07/11] remove argument from ExtractService command --- src/Command/CommandExtractService.php | 65 +++-------------------- src/Imports/Themes/ThemeReader.php | 74 +++++++++++++-------------- 2 files changed, 45 insertions(+), 94 deletions(-) diff --git a/src/Command/CommandExtractService.php b/src/Command/CommandExtractService.php index f00d210..cece79d 100644 --- a/src/Command/CommandExtractService.php +++ b/src/Command/CommandExtractService.php @@ -9,7 +9,6 @@ use PhpOffice\PhpSpreadsheet\IOFactory; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; @@ -31,15 +30,6 @@ public function __construct(string $projectDir, EntityManagerInterface $entityMa parent::__construct(); } - // protected function configure(): void - // { - // $this - // ->addArgument('extracthemes', InputArgument::OPTIONAL, 'extract and save themes into database themes'); - // $this ->addArgument('extract1', InputArgument::OPTIONAL, 'extract and save themes into database themes'); - // $this ->addArgument('extract2', InputArgument::OPTIONAL, 'extract and save themes into database themes'); - - - // } protected function execute(InputInterface $input, OutputInterface $output): int { @@ -53,56 +43,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int $spreadsheet = IOFactory::load($excle_file); $this->sheet = $spreadsheet->getActiveSheet(); $readtheme = new ThemeReader($this->sheet); - $themes = $readtheme->ingest(); + $themes = $readtheme->extract(); $io->info(count($themes).' themes extracted'); - $extract_service = new ExtractService($this->entityManager, $this->sheet); - //dd($themes); - $prepared_themes = $extract_service->PrepareThemesForDatabase($themes); - $saved_themes_count = $extract_service->SaveThemesOnDatabase($prepared_themes); - $io->info("$saved_themes_count themes were upserted successfuly"); - - - - // $spreadsheet = IOFactory::load($excle_file); - // $this->sheet = $spreadsheet->getActiveSheet(); - // $themes = $this->readTheme(); - // $io->info(count($themes).' themes extracted'); - // $this->sheet = $spreadsheet->getActiveSheet(); - // $extracthemes = $input->getArgument('extracthemes'); - // $extract_service = new ExtractService($this->entityManager, $this->sheet); - - // $io->info('sheet'); - // $readTheme = new ThemeReader($worksheet); - - - // if ($extracthemes) { - // $excel_file = $this->projectDir.'/var/import-data/emissions_GES_structure.xlsx'; - // if (!file_exists($excel_file)) { - // $io->error('file does not exist'); - - // return Command::FAILURE; - // } - // $spreadsheet = IOFactory::load($excel_file); - // $sheet = $spreadsheet->getActiveSheet(); - - - // try { - // $themes = $readTheme->ingest(); - // // $extracted_themes = $extract_service->GetThemesFromExcelFile($excel_file); - // $io->info(count($themes).' themes extracted'); - // $prepared_themes = $extract_service->PrepareThemesForDatabase($themes); - // $saved_themes_count = $extract_service->SaveThemesOnDatabase($prepared_themes); - - // $io->info("$saved_themes_count themes were upserted successfuly"); - // } catch (\Exception $e) { - // $io->error('File Excel failed to read : '.$e->getMessage()); - - // return Command::FAILURE; - // } + $extract_service = new ExtractService($this->entityManager, $this->sheet); + + $extracted_themes = $extract_service->PrepareThemesForDatabase($themes); + $saved_themes_count = $extract_service->SaveThemesOnDatabase($extracted_themes); - // return Command::SUCCESS; - // } + $io->info("$saved_themes_count themes were upserted successfuly"); + $io->warning('This command is not implemented yet!'); + dd($themes); return Command::SUCCESS; } diff --git a/src/Imports/Themes/ThemeReader.php b/src/Imports/Themes/ThemeReader.php index 3307854..a275f2a 100644 --- a/src/Imports/Themes/ThemeReader.php +++ b/src/Imports/Themes/ThemeReader.php @@ -1,37 +1,37 @@ -sheet->getRowIterator() as $row) { - $rowIndex = $row->getRowIndex(); - $name = $this->sheet->getCell('A'.$rowIndex)->getValue(); - $external_id = $this->sheet->getCell('B'.$rowIndex)->getValue(); - - if (null !== $external_id && null !== $name) { - if ($rowIndex > 2) { - $themes[] = [ - 'externalId' => $external_id, - 'name' => $name, - ]; - } - } - } - - return $themes; - } - - -} +sheet->getRowIterator() as $row) { + $rowIndex = $row->getRowIndex(); + $name = $this->sheet->getCell('A'.$rowIndex)->getValue(); + $external_id = $this->sheet->getCell('B'.$rowIndex)->getValue(); + + if (null !== $external_id && null !== $name) { + if ($rowIndex > 2) { + $themes[] = [ + 'externalId' => $external_id, + 'name' => $name, + ]; + } + } + } + + return $themes; + } + + +} From 1f68d2df03e8f24ffebf4ba7489a94cede0c349f Mon Sep 17 00:00:00 2001 From: fridev Date: Thu, 20 Mar 2025 22:59:18 +0100 Subject: [PATCH 08/11] add checking themes database --- src/Command/CommandExtractService.php | 23 +++++++++++++---------- src/Imports/Themes/ExtractService.php | 5 +++++ src/Repository/ThemeRepository.php | 15 +++++++++++++++ 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/Command/CommandExtractService.php b/src/Command/CommandExtractService.php index cece79d..a20d730 100644 --- a/src/Command/CommandExtractService.php +++ b/src/Command/CommandExtractService.php @@ -40,23 +40,26 @@ protected function execute(InputInterface $input, OutputInterface $output): int $io->error('file does not exist'); return Command::FAILURE; } + $spreadsheet = IOFactory::load($excle_file); + $this->sheet = $spreadsheet->getActiveSheet(); + $io->info('Sheet loaded ...'); $readtheme = new ThemeReader($this->sheet); $themes = $readtheme->extract(); - $io->info(count($themes).' themes extracted'); + $io->info(count($themes).' themes extracte successfully'); $extract_service = new ExtractService($this->entityManager, $this->sheet); - $extracted_themes = $extract_service->PrepareThemesForDatabase($themes); - $saved_themes_count = $extract_service->SaveThemesOnDatabase($extracted_themes); - - $io->info("$saved_themes_count themes were upserted successfuly"); - $io->warning('This command is not implemented yet!'); - dd($themes); - return Command::SUCCESS; + if($extract_service->checkThemesIsAlreadySaved($extracted_themes)){ + $io->info('All themes already exist in the database'); + return Command::SUCCESS; + } + else { + $saved_themes_count = $extract_service->SaveThemesOnDatabase($extracted_themes); + $io->info("$saved_themes_count themes were upserted successfuly"); + return Command::SUCCESS; + } } - - } diff --git a/src/Imports/Themes/ExtractService.php b/src/Imports/Themes/ExtractService.php index eef36d4..f59cfb0 100644 --- a/src/Imports/Themes/ExtractService.php +++ b/src/Imports/Themes/ExtractService.php @@ -102,4 +102,9 @@ public function SaveThemesOnDatabase(array $arrayThemes): int return count($arrayThemes); } + + public function checkThemesIsAlreadySaved(array $themes): bool + { + return $this->themeRepository->checkThemesExiste($themes); + } } diff --git a/src/Repository/ThemeRepository.php b/src/Repository/ThemeRepository.php index 9f2bed5..e0b1b88 100644 --- a/src/Repository/ThemeRepository.php +++ b/src/Repository/ThemeRepository.php @@ -25,4 +25,19 @@ public function findAllHierarchical(): array return $themesByParentId; } + + public function checkThemesExiste(array $themes): bool + { + $sizeThemes = count($themes); + $themesCount = count($this->findAll()); + if ($sizeThemes === $themesCount) { + return true; + } + return false; + } + + public function saveThemes(array $themes):bool + { + return false; + } } From 027c0688b68a2ab7470750c6dd004ec1348fbf84 Mon Sep 17 00:00:00 2001 From: fridev Date: Fri, 21 Mar 2025 08:09:28 +0100 Subject: [PATCH 09/11] code fix-style --- src/Command/CommandExtractService.php | 17 ++--- src/Imports/Themes/ExtractService.php | 40 +----------- src/Imports/Themes/ThemeReader.php | 71 ++++++++++----------- src/Repository/ThemeRepository.php | 8 +-- tests/Imports/Themes/ExtractServiceTest.php | 23 ++++--- 5 files changed, 61 insertions(+), 98 deletions(-) diff --git a/src/Command/CommandExtractService.php b/src/Command/CommandExtractService.php index a20d730..8321e2f 100644 --- a/src/Command/CommandExtractService.php +++ b/src/Command/CommandExtractService.php @@ -3,10 +3,10 @@ namespace App\Command; use App\Imports\Themes\ExtractService; -use App\ThemeReader; +use App\Imports\Themes\ThemeReader; use Doctrine\ORM\EntityManagerInterface; -use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PhpOffice\PhpSpreadsheet\IOFactory; +use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; @@ -19,7 +19,7 @@ )] class CommandExtractService extends Command { - private $projectDir; + private $projectDir; private $entityManager; private ?Worksheet $sheet = null; @@ -30,7 +30,6 @@ public function __construct(string $projectDir, EntityManagerInterface $entityMa parent::__construct(); } - protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); @@ -38,9 +37,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int $excle_file = $this->projectDir.'/var/import-data/emissions_GES_structure.xlsx'; if (!file_exists($excle_file)) { $io->error('file does not exist'); + return Command::FAILURE; } - + $spreadsheet = IOFactory::load($excle_file); $this->sheet = $spreadsheet->getActiveSheet(); @@ -52,13 +52,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int $extract_service = new ExtractService($this->entityManager, $this->sheet); $extracted_themes = $extract_service->PrepareThemesForDatabase($themes); - if($extract_service->checkThemesIsAlreadySaved($extracted_themes)){ + if ($extract_service->checkThemesIsAlreadySaved($extracted_themes)) { $io->info('All themes already exist in the database'); + return Command::SUCCESS; - } - else { + } else { $saved_themes_count = $extract_service->SaveThemesOnDatabase($extracted_themes); $io->info("$saved_themes_count themes were upserted successfuly"); + return Command::SUCCESS; } } diff --git a/src/Imports/Themes/ExtractService.php b/src/Imports/Themes/ExtractService.php index f59cfb0..2acfb0e 100644 --- a/src/Imports/Themes/ExtractService.php +++ b/src/Imports/Themes/ExtractService.php @@ -4,9 +4,7 @@ use App\Entity\Theme; use Doctrine\ORM\EntityManagerInterface; -use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; -use Symfony\Component\Filesystem\Exception\FileNotFoundException; class ExtractService { @@ -21,42 +19,6 @@ public function __construct(EntityManagerInterface $entityManager, Worksheet $wo $this->worksheet = $worksheet; } - /** - * Get Themes From Excel File. - * - * @return array themes import from the excel file - */ - public function GetThemesFromExcelFile(string $pathSheet): array - { - $themes = []; - - if ('xlsx' !== pathinfo(basename($pathSheet), PATHINFO_EXTENSION)) { - throw new \Exception('The file must be an Excel file'); - } - if (!file_exists($pathSheet)) { - throw new FileNotFoundException(sprintf('Excel file "%s" not found', $pathSheet)); - } - - $spreadsheet = IOFactory::load($pathSheet); - $sheet = $spreadsheet->getActiveSheet(); - - foreach ($sheet->getRowIterator() as $row) { - $rowIndex = $row->getRowIndex(); - $name = $sheet->getCell('A'.$rowIndex)->getValue(); - $external_id = $sheet->getCell('B'.$rowIndex)->getValue(); - if (null !== $name && null !== $external_id) { - if ($rowIndex > 2) { - $themes[] = [ - 'externalId' => $external_id, - 'name' => $name, - ]; - } - } - } - - return $themes; - } - private function getParentExternalId(string $externalId): ?string { $check_dot = strpos($externalId, '.'); @@ -105,6 +67,6 @@ public function SaveThemesOnDatabase(array $arrayThemes): int public function checkThemesIsAlreadySaved(array $themes): bool { - return $this->themeRepository->checkThemesExiste($themes); + return $this->themeRepository->checkThemesExiste($themes); } } diff --git a/src/Imports/Themes/ThemeReader.php b/src/Imports/Themes/ThemeReader.php index a275f2a..0350396 100644 --- a/src/Imports/Themes/ThemeReader.php +++ b/src/Imports/Themes/ThemeReader.php @@ -1,37 +1,34 @@ -sheet->getRowIterator() as $row) { - $rowIndex = $row->getRowIndex(); - $name = $this->sheet->getCell('A'.$rowIndex)->getValue(); - $external_id = $this->sheet->getCell('B'.$rowIndex)->getValue(); - - if (null !== $external_id && null !== $name) { - if ($rowIndex > 2) { - $themes[] = [ - 'externalId' => $external_id, - 'name' => $name, - ]; - } - } - } - - return $themes; - } - - -} +sheet->getRowIterator() as $row) { + $rowIndex = $row->getRowIndex(); + $name = $this->sheet->getCell('A'.$rowIndex)->getValue(); + $external_id = $this->sheet->getCell('B'.$rowIndex)->getValue(); + + if (null !== $external_id && null !== $name) { + if ($rowIndex > 2) { + $themes[] = [ + 'externalId' => $external_id, + 'name' => $name, + ]; + } + } + } + + return $themes; + } +} diff --git a/src/Repository/ThemeRepository.php b/src/Repository/ThemeRepository.php index e0b1b88..8fc414d 100644 --- a/src/Repository/ThemeRepository.php +++ b/src/Repository/ThemeRepository.php @@ -32,12 +32,8 @@ public function checkThemesExiste(array $themes): bool $themesCount = count($this->findAll()); if ($sizeThemes === $themesCount) { return true; - } - return false; - } + } - public function saveThemes(array $themes):bool - { - return false; + return false; } } diff --git a/tests/Imports/Themes/ExtractServiceTest.php b/tests/Imports/Themes/ExtractServiceTest.php index 68df65a..71e4b0e 100644 --- a/tests/Imports/Themes/ExtractServiceTest.php +++ b/tests/Imports/Themes/ExtractServiceTest.php @@ -4,6 +4,8 @@ use App\Entity\Theme; use App\Imports\Themes\ExtractService; +use App\Imports\Themes\ThemeReader; +use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; @@ -12,7 +14,8 @@ class ExtractServiceTest extends KernelTestCase private $entityManager; private $themeRepository; private $projectDir; - private $worksheet; + private ?Worksheet $sheet; + private $extract_service; protected function setUp(): void { @@ -22,7 +25,13 @@ protected function setUp(): void $this->entityManager = $container->get('doctrine.orm.entity_manager'); $this->themeRepository = $this->entityManager->getRepository(Theme::class); $this->projectDir = $container->getParameter('kernel.project_dir'); - $this->worksheet = $this->createMock(Worksheet::class); + $excel_file = $this->projectDir.'/tests/Imports/Themes/test-themes.xlsx'; + if (!file_exists($excel_file)) { + throw new \Exception('file does not exist'); + } + $spreadsheet = IOFactory::load($excel_file); + $this->sheet = $spreadsheet->getActiveSheet(); + $this->extract_service = new ExtractService($this->entityManager, $this->sheet); } protected function tearDown(): void @@ -37,12 +46,10 @@ protected function tearDown(): void public function testImportThemeSave(): void { - $ExtractServices = new ExtractService(entityManager: $this->entityManager, worksheet: $this->worksheet); - // TODO: change this to inject the spreadsheet so that we can test the integration more easily. - $excel_file = $this->projectDir.'/tests/Imports/Themes/test-themes.xlsx'; - $themes = $ExtractServices->GetThemesFromExcelFile($excel_file); - $preparedThemes = $ExtractServices->PrepareThemesForDatabase($themes); - $savedThemesCount = $ExtractServices->SaveThemesOnDatabase($preparedThemes); + $read_themes = new ThemeReader($this->sheet); + $themes = $read_themes->extract(); + $preparedThemes = $this->extract_service->PrepareThemesForDatabase($themes); + $savedThemesCount = $this->extract_service->SaveThemesOnDatabase($preparedThemes); $this->assertEquals( $savedThemesCount, From 6b3b35d883fd80922e61452c8fd417c268f1f251 Mon Sep 17 00:00:00 2001 From: fridev Date: Fri, 21 Mar 2025 22:53:11 +0100 Subject: [PATCH 10/11] remove checkThemes function --- src/Command/CommandExtractService.php | 13 ++++--------- src/Imports/Themes/ExtractService.php | 5 +---- src/Repository/ThemeRepository.php | 11 +---------- 3 files changed, 6 insertions(+), 23 deletions(-) diff --git a/src/Command/CommandExtractService.php b/src/Command/CommandExtractService.php index 8321e2f..d0fa991 100644 --- a/src/Command/CommandExtractService.php +++ b/src/Command/CommandExtractService.php @@ -52,15 +52,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int $extract_service = new ExtractService($this->entityManager, $this->sheet); $extracted_themes = $extract_service->PrepareThemesForDatabase($themes); - if ($extract_service->checkThemesIsAlreadySaved($extracted_themes)) { - $io->info('All themes already exist in the database'); + $saved_themes_count = $extract_service->SaveThemesOnDatabase($extracted_themes); + $io->info("$saved_themes_count themes were saved successfuly"); - return Command::SUCCESS; - } else { - $saved_themes_count = $extract_service->SaveThemesOnDatabase($extracted_themes); - $io->info("$saved_themes_count themes were upserted successfuly"); - - return Command::SUCCESS; - } + return Command::SUCCESS; + } } diff --git a/src/Imports/Themes/ExtractService.php b/src/Imports/Themes/ExtractService.php index 2acfb0e..aaa967f 100644 --- a/src/Imports/Themes/ExtractService.php +++ b/src/Imports/Themes/ExtractService.php @@ -65,8 +65,5 @@ public function SaveThemesOnDatabase(array $arrayThemes): int return count($arrayThemes); } - public function checkThemesIsAlreadySaved(array $themes): bool - { - return $this->themeRepository->checkThemesExiste($themes); - } + } diff --git a/src/Repository/ThemeRepository.php b/src/Repository/ThemeRepository.php index 8fc414d..4d4e5a1 100644 --- a/src/Repository/ThemeRepository.php +++ b/src/Repository/ThemeRepository.php @@ -26,14 +26,5 @@ public function findAllHierarchical(): array return $themesByParentId; } - public function checkThemesExiste(array $themes): bool - { - $sizeThemes = count($themes); - $themesCount = count($this->findAll()); - if ($sizeThemes === $themesCount) { - return true; - } - - return false; - } + } From 27169a04421f3fa0cb398b98414acdd2d279984d Mon Sep 17 00:00:00 2001 From: fridev Date: Fri, 21 Mar 2025 22:56:49 +0100 Subject: [PATCH 11/11] all tests completed --- src/Command/CommandExtractService.php | 1 - src/Imports/Themes/ExtractService.php | 2 -- src/Repository/ThemeRepository.php | 2 -- 3 files changed, 5 deletions(-) diff --git a/src/Command/CommandExtractService.php b/src/Command/CommandExtractService.php index d0fa991..9ab3533 100644 --- a/src/Command/CommandExtractService.php +++ b/src/Command/CommandExtractService.php @@ -56,6 +56,5 @@ protected function execute(InputInterface $input, OutputInterface $output): int $io->info("$saved_themes_count themes were saved successfuly"); return Command::SUCCESS; - } } diff --git a/src/Imports/Themes/ExtractService.php b/src/Imports/Themes/ExtractService.php index aaa967f..35bd282 100644 --- a/src/Imports/Themes/ExtractService.php +++ b/src/Imports/Themes/ExtractService.php @@ -64,6 +64,4 @@ public function SaveThemesOnDatabase(array $arrayThemes): int return count($arrayThemes); } - - } diff --git a/src/Repository/ThemeRepository.php b/src/Repository/ThemeRepository.php index 4d4e5a1..9f2bed5 100644 --- a/src/Repository/ThemeRepository.php +++ b/src/Repository/ThemeRepository.php @@ -25,6 +25,4 @@ public function findAllHierarchical(): array return $themesByParentId; } - - }