diff --git a/src/Command/CommandExtractService.php b/src/Command/CommandExtractService.php index a9ebbbc..9ab3533 100644 --- a/src/Command/CommandExtractService.php +++ b/src/Command/CommandExtractService.php @@ -3,10 +3,12 @@ namespace App\Command; use App\Imports\Themes\ExtractService; +use App\Imports\Themes\ThemeReader; use Doctrine\ORM\EntityManagerInterface; +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\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; @@ -19,6 +21,7 @@ class CommandExtractService extends Command { private $projectDir; private $entityManager; + private ?Worksheet $sheet = null; public function __construct(string $projectDir, EntityManagerInterface $entityManager) { @@ -27,42 +30,30 @@ 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'); - } - protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); - $extracthemes = $input->getArgument('extracthemes'); - $extract_service = new ExtractService($this->entityManager); - 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'); - if (!file_exists($excel_file)) { - $io->error('file does not exist'); - - return Command::FAILURE; - } + return Command::FAILURE; + } - 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); + $spreadsheet = IOFactory::load($excle_file); - $io->info("$saved_themes_count themes were upserted successfuly"); - } catch (\Exception $e) { - $io->error('File Excel failed to read : '.$e->getMessage()); + $this->sheet = $spreadsheet->getActiveSheet(); + $io->info('Sheet loaded ...'); + $readtheme = new ThemeReader($this->sheet); + $themes = $readtheme->extract(); - return Command::FAILURE; - } + $io->info(count($themes).' themes extracte successfully'); + $extract_service = new ExtractService($this->entityManager, $this->sheet); + $extracted_themes = $extract_service->PrepareThemesForDatabase($themes); - return Command::SUCCESS; - } + $saved_themes_count = $extract_service->SaveThemesOnDatabase($extracted_themes); + $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 55f040f..35bd282 100644 --- a/src/Imports/Themes/ExtractService.php +++ b/src/Imports/Themes/ExtractService.php @@ -4,49 +4,19 @@ use App\Entity\Theme; use Doctrine\ORM\EntityManagerInterface; -use PhpOffice\PhpSpreadsheet\IOFactory; -use Symfony\Component\Filesystem\Exception\FileNotFoundException; +use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; class ExtractService { private $entityManager; private $themeRepository; + private $worksheet; - public function __construct(EntityManagerInterface $entityManager) + public function __construct(EntityManagerInterface $entityManager, Worksheet $worksheet) { $this->entityManager = $entityManager; $this->themeRepository = $entityManager->getRepository(Theme::class); - } - - /** - * Get Themes From Excel File. - * - * @return array themes import from the excel file - */ - public function GetThemesFromExcelFile(string $excel_file): array - { - $themes = []; - if (!file_exists($excel_file)) { - throw new FileNotFoundException(sprintf('Excel file "%s" not found', $excel_file)); - } - $spreadsheet = IOFactory::load($excel_file); - $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; + $this->worksheet = $worksheet; } private function getParentExternalId(string $externalId): ?string diff --git a/src/Imports/Themes/ThemeReader.php b/src/Imports/Themes/ThemeReader.php new file mode 100644 index 0000000..0350396 --- /dev/null +++ b/src/Imports/Themes/ThemeReader.php @@ -0,0 +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; + } +} diff --git a/tests/Imports/Themes/ExtractServiceTest.php b/tests/Imports/Themes/ExtractServiceTest.php index 4364af9..71e4b0e 100644 --- a/tests/Imports/Themes/ExtractServiceTest.php +++ b/tests/Imports/Themes/ExtractServiceTest.php @@ -4,6 +4,9 @@ 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; class ExtractServiceTest extends KernelTestCase @@ -11,6 +14,8 @@ class ExtractServiceTest extends KernelTestCase private $entityManager; private $themeRepository; private $projectDir; + private ?Worksheet $sheet; + private $extract_service; protected function setUp(): void { @@ -20,6 +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'); + $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 @@ -34,12 +46,10 @@ protected function tearDown(): void public function testImportThemeSave(): void { - $ExtractServices = new ExtractService($this->entityManager); - // 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,