Passed
Push — master ( f93deb...6b3ae4 )
by Jeff
02:42
created

UserAddCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace App\Command;
4
5
use App\Entity\Domain;
6
use App\Entity\User;
7
use Doctrine\ORM\EntityManagerInterface;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Helper\QuestionHelper;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Question\Question;
15
use Symfony\Component\Validator\ConstraintViolation;
16
use Symfony\Component\Validator\Validator\ValidatorInterface;
17
18
class UserAddCommand extends Command
19
{
20
    private $manager;
21
22
    private $validator;
23
24
    public function __construct(string $name = null, EntityManagerInterface $manager, ValidatorInterface $validator)
25
    {
26
        parent::__construct($name);
27
28
        $this->manager = $manager;
29
        $this->validator = $validator;
30
    }
31
32
    protected function configure(): void
33
    {
34
        $this
35
            ->setName('user:add')
36
            ->setDescription('Add users.')
37
            ->addArgument('name', InputArgument::REQUIRED, 'Local-part (before @)')
38
            ->addArgument('domain', InputArgument::REQUIRED, 'Domain-part (after @), has to be created already')
39
            ->addOption('admin', null, InputOption::VALUE_NONE, 'Allow login to management interface')
40
            ->addOption('sendonly', null, InputOption::VALUE_NONE, 'Send only accounts cannot receive mails')
41
            ->addOption('quota', null, InputOption::VALUE_REQUIRED, 'Limit the disk usage of this account')
42
            ->addOption('password', null, InputOption::VALUE_REQUIRED, 'Sets the account password directly');
43
    }
44
45
    protected function execute(InputInterface $input, OutputInterface $output): int
46
    {
47
        $user = new User();
48
        $domain = $this->getDomain($input->getArgument('domain'));
49
50
        if (!$domain) {
51
            $output->writeln(sprintf('<error>Domain %s was not found.</error>', $input->getArgument('domain')));
52
53
            return 1;
54
        }
55
56
        $user->setDomain($domain);
57
        $user->setName(\mb_strtolower($input->getArgument('name')));
58
        $user->setAdmin((bool)$input->getOption('admin'));
59
        $user->setSendOnly((bool)$input->getOption('sendonly'));
60
61
        if ($input->hasOption('quota')) {
62
            $user->setQuota((int)$input->getOption('quota'));
63
        }
64
65
        if (!$input->getOption('password')) {
66
            /** @var QuestionHelper $helper */
67
            $helper = $this->getHelper('question');
68
            $question = new Question('Password? (hidden)');
69
            $question->setHidden(true);
70
71
            $password = $helper->ask($input, $output, $question);
72
73
            if (!$password) {
74
                $output->writeln('<error>Please set a valid password.</error>');
75
76
                return 1;
77
            }
78
79
            $user->setPlainPassword($password);
0 ignored issues
show
Bug introduced by
It seems like $password can also be of type true; however, parameter $plainPassword of App\Entity\User::setPlainPassword() does only seem to accept null|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

79
            $user->setPlainPassword(/** @scrutinizer ignore-type */ $password);
Loading history...
80
        } else {
81
            $user->setPlainPassword($input->getOption('password'));
82
        }
83
84
        $validationResult = $this->validator->validate($user);
85
86
        if ($validationResult->count() > 0) {
87
            foreach ($validationResult as $item) {
88
                /** @var $item ConstraintViolation */
89
                $output->writeln(sprintf('<error>%s: %s</error>', $item->getPropertyPath(), $item->getMessage()));
90
            }
91
92
            return 1;
93
        }
94
95
        $this->manager->persist($user);
96
        $this->manager->flush();
97
98
        return 0;
99
    }
100
101
    private function getDomain(string $domain): ?Domain
102
    {
103
        return $this->manager->getRepository(Domain::class)->findOneBy(['name' => \mb_strtolower($domain)]);
104
    }
105
}
106