<?php
namespace App\Command;
use App\Entity\User;
use App\Repository\AffectationRepository;
use App\Repository\CustomerRepository;
use App\Repository\SiteRepository;
use App\Services\Email\SendEmail;
use App\Services\Exception\WriterException;
use App\Services\SiteService;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
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\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
#[AsCommand(
name: 'app:delete:entity-site',
description: 'this command will delete sites',
)]
class DeleteSiteCommand extends Command
{
public function __construct(
private readonly SiteRepository $siteRepository,
private readonly SiteService $siteService,
private readonly AffectationRepository $affectationRepository,
private MailerInterface $mailer,
private ParameterBagInterface $params,
private SendEmail $sendEmail,
private ?EntityManagerInterface $em = null,
private readonly CustomerRepository $customerRepository,
)
{
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$io->info('START');
$sites = $this->siteRepository->getAllSitesByCustomer(411);
if (count($sites) > 0) {
$sites = array_column($sites, 'id');
foreach ($sites as $id) {
try {
$all_site = array_reverse(array_column($this->customerRepository->withRecursiveGetFilsOfEntity($id), 'id'));
foreach ($all_site as $site){
$this->siteRepository->deleteSite($site);
}
} catch (Exception $e) {
$from = new Address($this->params->get('mailer_from'), $this->params->get('mailer_from_name'));
$to = new Address('contact@novalia.online', 'contact_test');
$subject = 'Erreur de suppression du site';
$template = 'exception-delete-site.html.twig';
$context = [
'id_site' => $id,
'error' => $e->getMessage()
];
$this->sendEmail->sendExceptionEmail($from, $to, $subject, $template, $context);
}
}
}
$io->success('Done!');
return Command::SUCCESS;
}
}