Passed
Push — master ( 77290b...f3fb78 )
by Marcel
09:09
created

RemoveOrphanedParentsCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace App\Command;
4
5
use App\Repository\UserRepositoryInterface;
6
use SchulIT\CommonBundle\Helper\DateHelper;
7
use Shapecode\Bundle\CronBundle\Annotation\CronJob;
8
use Shapecode\Bundle\CronBundle\Attribute\AsCronJob;
9
use Symfony\Component\Console\Attribute\AsCommand;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Style\SymfonyStyle;
14
15
#[AsCronJob(schedule: '0 1 * * * ')]
16
#[AsCommand(name: 'app:user:remove_orphaned', description: 'Cleanup all external ids to remove duplicates.')]
17
class RemoveOrphanedParentsCommand extends Command {
18
19
    private const InactiveModifier = '-14 days';
20
21
    public function __construct(private readonly DateHelper $dateHelper, private readonly UserRepositoryInterface $userRepository, string $name = null) {
22
        parent::__construct($name);
23
    }
24
25
26
    public function execute(InputInterface $input, OutputInterface $output): int {
27
        $style = new SymfonyStyle($input, $output);
28
29
        $users = $this->userRepository->findParentUsersWithoutStudents();
30
        $this->userRepository->beginTransaction();
31
32
        $count = 0;
33
        $threshold = $this->dateHelper->getToday()->modify(self::InactiveModifier);
34
35
        foreach($users as $user) {
36
            if($user->getLinkedStudents()->count() === 0 && $user->getCreatedAt() < $threshold) {
37
                $this->userRepository->remove($user);
38
                $count++;
39
            }
40
        }
41
42
        $this->userRepository->commit();
43
44
        $style->success(sprintf('Successfully removed %d user(s)', $count));
45
46
        return 0;
47
    }
48
}