Completed
Push — master ( 5429ef...771e5d )
by Jeff
04:44 queued 02:38
created

DKIMDisableCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
A execute() 0 35 3
A __construct() 0 5 1
1
<?php
2
3
namespace App\Command;
4
5
use App\Entity\Domain;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Helper\QuestionHelper;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Question\ConfirmationQuestion;
13
14
class DKIMDisableCommand extends Command
15
{
16
    private $manager;
17
18
    public function __construct(string $name = null, EntityManagerInterface $manager)
19
    {
20
        parent::__construct($name);
21
22
        $this->manager = $manager;
23
    }
24
25
    protected function configure(): void
26
    {
27
        $this
28
            ->setName('dkim:disable')
29
            ->setDescription('Disables DKIM for a specific domain and deletes its private key.')
30
            ->addArgument('domain', InputArgument::REQUIRED, 'Domain-part (after @)');
31
    }
32
33
    protected function execute(InputInterface $input, OutputInterface $output): int
34
    {
35
        $name = $input->getArgument('domain');
36
        /** @var Domain $domain */
37
        $domain = $this->manager->getRepository(Domain::class)->findOneBy(['name' => $name]);
38
39
        if (!$domain) {
0 ignored issues
show
introduced by
$domain is of type App\Entity\Domain, thus it always evaluated to true.
Loading history...
40
            $output->writeln(sprintf('<error>Domain "%s" was not found.</error>', $name));
41
42
            return 1;
43
        }
44
45
        /** @var QuestionHelper $questionHelper */
46
        $questionHelper = $this->getHelper('question');
47
        $result = $questionHelper->ask(
48
            $input,
49
            $output,
50
            new ConfirmationQuestion(sprintf('Do you want to disable DKIM for domain "%s"?', $domain->getName()))
51
        );
52
53
        if (!$result) {
54
            $output->writeln('Aborting.');
55
56
            return 1;
57
        }
58
59
        $domain->setDkimPrivateKey('');
60
        $domain->setDkimSelector('');
61
        $domain->setDkimEnabled(false);
62
63
        $this->manager->flush();
64
65
        $output->writeln('Done.');
66
67
        return 0;
68
    }
69
}
70