zend-blog-3-backend
68 строк · 1.7 Кб
1<?php
2
3namespace App\Command;
4
5use App\Repository\CommentatorRepository;
6use App\Utils\VerifyEmail;
7use Doctrine\ORM\EntityManagerInterface;
8use Symfony\Component\Console\Command\Command;
9use Symfony\Component\Console\Helper\Table;
10use Symfony\Component\Console\Input\InputInterface;
11use Symfony\Component\Console\Output\OutputInterface;
12
13class FakeEmailCheck extends Command
14{
15private CommentatorRepository $repository;
16
17private EntityManagerInterface $em;
18
19public function __construct(CommentatorRepository $repository, EntityManagerInterface $em)
20{
21parent::__construct();
22
23$this->repository = $repository;
24$this->em = $em;
25}
26
27protected function configure(): void
28{
29$this
30->setName('mtt:fake-email:check')
31->setDescription('Check commentators email')
32;
33}
34
35protected function execute(InputInterface $input, OutputInterface $output): int
36{
37$rows = [];
38
39$commentators = $this->repository->getWithUncheckedEmails();
40foreach ($commentators as $entity) {
41$result = VerifyEmail::isValid($entity->getEmail());
42
43$entity
44->setFakeEmail(!$result)
45->setEmailCheck(new \DateTime())
46;
47
48$rows[] = [
49$entity->getId(),
50$entity->getEmail(),
51$result ? '<fg=green>true</>' : '<fg=red>false</>',
52];
53}
54
55$this->em->flush();
56
57if (count($rows)) {
58$table = new Table($output);
59$table->setHeaders(['ID', 'email', 'result']);
60
61$table->setRows($rows)->render();
62} else {
63$output->writeln('Nothing to check');
64}
65
66return 0;
67}
68}
69