RemoveOrphanedParentsCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 2
ccs 0
cts 0
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\Attribute\AsCronJob;
8
use Symfony\Component\Console\Attribute\AsCommand;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
14
#[AsCronJob(schedule: '0 1 * * * ')]
15
#[AsCommand(name: 'app:user:remove_orphaned', description: 'Löscht Eltern ohne verknüpften Lernenden.')]
16
class RemoveOrphanedParentsCommand extends Command {
17
18
    private const InactiveModifier = '-14 days';
19
20
    public function __construct(private readonly DateHelper $dateHelper, private readonly UserRepositoryInterface $userRepository, string $name = null) {
21
        parent::__construct($name);
22
    }
23
24
25
    public function execute(InputInterface $input, OutputInterface $output): int {
26
        $style = new SymfonyStyle($input, $output);
27
28
        $users = $this->userRepository->findParentUsersWithoutStudents();
29
        $this->userRepository->beginTransaction();
30
31
        $count = 0;
32
        $threshold = $this->dateHelper->getToday()->modify(self::InactiveModifier);
33
34
        foreach($users as $user) {
35
            if($user->getLinkedStudents()->count() === 0 && $user->getCreatedAt() < $threshold) {
36
                $this->userRepository->remove($user);
37
                $count++;
38
            }
39
        }
40
41
        $this->userRepository->commit();
42
43
        $style->success(sprintf('%d Benutzer gelöscht', $count));
44
45
        return 0;
46
    }
47
}