Completed
Push — master ( 3d4598...ee29b8 )
by Jeff
03:15
created

AliasAddCommand::getDomain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * This file is part of the mailserver-admin package.
6
 * (c) Jeffrey Boehm <https://github.com/jeboehm/mailserver-admin>
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace App\Command;
12
13
use App\Entity\Alias;
14
use App\Entity\Domain;
15
use Doctrine\ORM\EntityManagerInterface;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\Validator\ConstraintViolation;
21
use Symfony\Component\Validator\Validator\ValidatorInterface;
22
23
class AliasAddCommand extends Command
24
{
25
    private $manager;
26
27
    private $validator;
28
29
    public function __construct(
30
        string $name = null,
31
        EntityManagerInterface $manager,
32
        ValidatorInterface $validator
33
    ) {
34
        parent::__construct($name);
35
36
        $this->manager = $manager;
37
        $this->validator = $validator;
38
    }
39
40
    protected function configure(): void
41
    {
42
        $this
43
            ->setName('alias:add')
44
            ->setDescription('Add aliases.')
45
            ->addArgument('from', InputArgument::REQUIRED, 'Address of the new alias.')
46
            ->addArgument('to', InputArgument::REQUIRED, 'Where mails to the new alias go to.');
47
    }
48
49
    protected function execute(InputInterface $input, OutputInterface $output): int
50
    {
51
        $from = \filter_var($input->getArgument('from'), \FILTER_VALIDATE_EMAIL);
52
        $to = \filter_var($input->getArgument('to'), \FILTER_VALIDATE_EMAIL);
53
54
        if (!$from) {
55
            $output->writeln(sprintf('<error>%s is not a valid email address.</error>', $input->getArgument('from')));
56
57
            return 1;
58
        }
59
60
        if (!$to) {
61
            $output->writeln(sprintf('<error>%s is not a valid email address.</error>', $input->getArgument('to')));
62
63
            return 1;
64
        }
65
66
        $alias = new Alias();
67
        $alias->setDestination($to);
68
69
        $fromParts = \explode('@', $from, 2);
70
        $domain = $this->getDomain($fromParts[1]);
71
72
        if (!$domain) {
73
            $output->writeln(sprintf('<error>Domain %s has to be created before.</error>', $fromParts[1]));
74
75
            return 1;
76
        }
77
78
        $alias->setDomain($domain);
79
        $alias->setName(\mb_strtolower($fromParts[0]));
80
81
        $validationResult = $this->validator->validate($alias);
82
83
        if ($validationResult->count() > 0) {
84
            foreach ($validationResult as $item) {
85
                /* @var $item ConstraintViolation */
86
                $output->writeln(sprintf('<error>%s: %s</error>', $item->getPropertyPath(), $item->getMessage()));
87
            }
88
89
            return 1;
90
        }
91
92
        $this->manager->persist($alias);
93
        $this->manager->flush();
94
95
        return 0;
96
    }
97
98
    private function getDomain(string $domain): ?Domain
99
    {
100
        return $this->manager->getRepository(Domain::class)->findOneBy(['name' => \mb_strtolower($domain)]);
101
    }
102
}
103