Issues (13)

src/Command/DomainAddCommand.php (1 issue)

Labels
Severity
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\Domain;
14
use Doctrine\Persistence\ManagerRegistry;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Validator\ConstraintViolation;
20
use Symfony\Component\Validator\Validator\ValidatorInterface;
21
22
class DomainAddCommand extends Command
23
{
24
    private ManagerRegistry $manager;
25
26
    private ValidatorInterface $validator;
27
28
    public function __construct(ManagerRegistry $manager, ValidatorInterface $validator)
29
    {
30
        parent::__construct();
31
32
        $this->manager = $manager;
33
        $this->validator = $validator;
34
    }
35
36
    protected function configure(): void
37
    {
38
        $this
39
            ->setName('domain:add')
40
            ->setDescription('Add domains.')
41
            ->addArgument('domain', InputArgument::REQUIRED, 'Domain-part (after @)');
42
    }
43
44
    protected function execute(InputInterface $input, OutputInterface $output): int
45
    {
46
        $domain = new Domain();
47
        $domain->setName(\mb_strtolower($input->getArgument('domain')));
0 ignored issues
show
It seems like $input->getArgument('domain') can also be of type null and string[]; however, parameter $string of mb_strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
        $domain->setName(\mb_strtolower(/** @scrutinizer ignore-type */ $input->getArgument('domain')));
Loading history...
48
49
        $validationResult = $this->validator->validate($domain);
50
51
        if ($validationResult->count() > 0) {
52
            foreach ($validationResult as $item) {
53
                /* @var $item ConstraintViolation */
54
                $output->writeln(sprintf('<error>%s: %s</error>', $item->getPropertyPath(), $item->getMessage()));
55
            }
56
57
            return 1;
58
        }
59
60
        $this->manager->getManager()->persist($domain);
61
        $this->manager->getManager()->flush();
62
63
        return 0;
64
    }
65
}
66