← Back
Editing: AgencyExport.php
<?php namespace App\Import; use App\Entity\Agency; use App\Entity\User; use Doctrine\Common\Collections\Collection; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Exception; use Symfony\Component\HttpKernel\KernelInterface; class AgencyExport { private Spreadsheet $spreadsheet; private string $cacheDir; private Agency $agency; /** @var Collection<int, User> */ private Collection $managers; /** @var Collection<int, User> */ private Collection $agents; public function __construct(KernelInterface $kernel) { $this->spreadsheet = IOFactory::load($kernel->getProjectDir().'/data/import.xlsx'); $this->cacheDir = $kernel->getCacheDir(); } public function setAgency(Agency $agency): void { $this->agency = $agency; $this->fillManagers($this->agency->getUsers()); $this->fillAgents($this->agency->getUsers()); } /** * @param Collection<int, User> $users */ private function fillManagers(Collection $users): void { $this->managers = $users->filter( function ($user) { return in_array('ROLE_MANAGER', $user->getRoles()); } ); } /** * @param Collection<int, User> $users */ private function fillAgents(Collection $users): void { $this->agents = $users->filter( function ($user) { return in_array('ROLE_AGENT', $user->getRoles()); } ); } /** * @throws \PhpOffice\PhpSpreadsheet\Exception */ public function run(): void { $this->fillAgencySheet(); $this->fillManagers($this->agency->getUsers()); $this->fillAgents($this->agency->getUsers()); $this->fillAgentSheet(); $this->fillManagerSheet(); $this->fillAddressesSheet(); $this->fillPrestaSheet(); $this->fillAffectationsSheet(); } /** * @throws Exception */ public function export(): string { $writer = IOFactory::createWriter($this->spreadsheet, 'Xlsx'); $tmpName = uniqid().'.xlsx'; $fullPath = $this->cacheDir.$tmpName; $writer->save($fullPath); return $fullPath; } /** * @throws \PhpOffice\PhpSpreadsheet\Exception */ private function fillAgencySheet(): void { $this->spreadsheet->setActiveSheetIndex(0); $currentLine = 2; $colOffset = 2; for ($i = $colOffset; $i < (count(ExcelMapping::AGENCY) + $colOffset); ++$i) { $key = ExcelMapping::AGENCY[$i - $colOffset]; $val = $this->getValueFromObjectAndKey($this->agency, $key); $this->spreadsheet->getActiveSheet()->setCellValueByColumnAndRow($i, $currentLine, $val); } } /** * @throws \PhpOffice\PhpSpreadsheet\Exception */ private function fillAddressesSheet(): void { $this->spreadsheet->setActiveSheetIndex(2); $currentLine = 2; $colOffset = 2; foreach ($this->agency->getAdresses() as $adresse) { for ($i = $colOffset; $i < (count(ExcelMapping::ADDRESSES) + $colOffset); ++$i) { $key = ExcelMapping::ADDRESSES[$i - $colOffset]; $val = $this->getValueFromObjectAndKey($adresse, $key); $this->spreadsheet->getActiveSheet()->setCellValueByColumnAndRow($i, $currentLine, $val); } ++$currentLine; } } /** * @throws \PhpOffice\PhpSpreadsheet\Exception */ private function fillPrestaSheet(): void { $this->spreadsheet->setActiveSheetIndex(4); $currentLine = 2; $colOffset = 2; foreach ($this->agency->getPrestas() as $agencyPrestataire) { $presta = $agencyPrestataire->getPrestataire(); for ($i = $colOffset; $i < (count(ExcelMapping::PRESTAS) + $colOffset); ++$i) { $key = ExcelMapping::PRESTAS[$i - $colOffset]; $val = $this->getValueFromObjectAndKey($presta, $key); $this->spreadsheet->getActiveSheet()->setCellValueByColumnAndRow($i, $currentLine, $val); } ++$currentLine; } } /** * @throws \PhpOffice\PhpSpreadsheet\Exception */ private function fillManagerSheet(): void { $this->spreadsheet->setActiveSheetIndex(1); $lineOffset = 2; $colOffset = 2; foreach ($this->managers as $manager) { for ($i = $colOffset; $i < (count(ExcelMapping::MANAGERS) + $colOffset); ++$i) { $key = ExcelMapping::MANAGERS[$i - $colOffset]; $val = $this->getValueFromObjectAndKey($manager, $key); $this->spreadsheet->getActiveSheet()->setCellValueByColumnAndRow($i, $lineOffset, $val); } ++$lineOffset; } } /** * @throws \PhpOffice\PhpSpreadsheet\Exception */ private function fillAgentSheet(): void { $this->spreadsheet->setActiveSheetIndex(3); $lineOffset = 2; $colOffset = 2; foreach ($this->agents as $agent) { for ($i = $colOffset; $i < (count(ExcelMapping::AGENTS) + $colOffset); ++$i) { $key = ExcelMapping::AGENTS[$i - $colOffset]; $val = $this->getValueFromObjectAndKey($agent, $key); $this->spreadsheet->getActiveSheet()->setCellValueByColumnAndRow($i, $lineOffset, $val); } ++$lineOffset; } } /** * @throws \PhpOffice\PhpSpreadsheet\Exception */ private function fillAffectationsSheet(): void { $this->spreadsheet->setActiveSheetIndex(5); $lineOffset = 2; foreach ($this->agency->getAdresses() as $adresse) { $agentCursor = 1; foreach ($this->agents as $agent) { if ($agent->getId() === $adresse->getUserAddresses()->first()->getUser()->getId()) { $this->spreadsheet->getActiveSheet()->setCellValueByColumnAndRow( 3, $lineOffset, 'AGENT '.$agentCursor ); } ++$agentCursor; } $prestaCursor = 1; foreach ($this->agency->getPrestas() as $presta) { if ($presta->getId() === $adresse->getPrestataires()[0]->getId()) { $this->spreadsheet->getActiveSheet()->setCellValueByColumnAndRow( 2, $lineOffset, 'PRESTATAIRE '.$prestaCursor ); } ++$prestaCursor; } ++$lineOffset; } } private function getValueFromObjectAndKey(object $obj, string $key) { $method = 'get'.ucfirst($key); $value = $obj->$method(); if ($value instanceof \DateTimeInterface) { $value = $value->format('d/m/Y'); } return $value; } }
Save File
Cancel