Completed
Push — master ( 8f2b16...c80f65 )
by Marcel
03:24
created

ProvisionUserCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 19
ccs 0
cts 11
cp 0
crap 6
rs 9.9332
1
<?php
2
3
namespace App\Command;
4
5
use App\Repository\UserRepositoryInterface;
6
use Shapecode\Bundle\CronBundle\Annotation\CronJob;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Style\SymfonyStyle;
11
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
12
13
/**
14
 * @CronJob("*\/2 * * * * ")
15
 */
16
class ProvisionUserCommand extends Command {
17
18
    private $numberOfUsers;
19
    private $userRepository;
20
    private $passwordEncoder;
21
22
    public function __construct(int $numberOfUsers, UserPasswordEncoderInterface $passwordEncoder, UserRepositoryInterface $userRepository, string $name = null) {
23
        parent::__construct($name);
24
25
        $this->numberOfUsers = $numberOfUsers;
26
        $this->userRepository = $userRepository;
27
        $this->passwordEncoder = $passwordEncoder;
28
    }
29
30
    public function configure() {
31
        $this
32
            ->setName('app:user:provision')
33
            ->setDescription('Provisions the next N users');
34
    }
35
36
    public function execute(InputInterface $input, OutputInterface $output) {
37
        $io = new SymfonyStyle($input, $output);
38
39
        $users = $this->userRepository->findNextNonProvisionedUsers($this->numberOfUsers);
40
41
        foreach($users as $user) {
42
            $user->setPassword(
43
                $this->passwordEncoder->encodePassword($user, $user->getPassword())
44
            );
45
46
            $user->setIsProvisioned(true);
47
            $this->userRepository->persist($user);
48
49
            $io->text(sprintf('User %s provisioned.', $user->getUsername()));
50
        }
51
52
        $io->success(sprintf('Successfully provisioned %d user(s).', count($users)));
53
54
        return 0;
55
    }
56
}