-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathSyncBirthdayCalendars.php
More file actions
69 lines (55 loc) · 2.11 KB
/
SyncBirthdayCalendars.php
File metadata and controls
69 lines (55 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace App\Command;
use App\Entity\User;
use App\Services\BirthdayService;
use Doctrine\Persistence\ManagerRegistry;
use Sabre\CalDAV\Backend\PDO as CalendarBackend;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class SyncBirthdayCalendars extends Command
{
public function __construct(
private ManagerRegistry $doctrine,
private BirthdayService $birthdayService,
) {
parent::__construct();
$em = $doctrine->getManager();
$pdo = $em->getConnection()->getNativeConnection();
$this->birthdayService->setBackend(new CalendarBackend($pdo));
}
protected function configure(): void
{
$this
->setName('dav:sync-birthday-calendar')
->setDescription('Synchronizes the birthday calendar')
->addArgument('username',
InputArgument::OPTIONAL,
'Username for whom the birthday calendar will be synchronized');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$username = $input->getArgument('username');
if (!is_null($username)) {
if (!$this->doctrine->getRepository(User::class)->findOneByUsername($username)) {
throw new \InvalidArgumentException("User <$username> is unknown.");
}
$output->writeln("Start birthday calendar sync for $username");
$this->birthdayService->syncUser($username);
return self::SUCCESS;
}
$output->writeln('Start birthday calendar sync for all users ...');
$p = new ProgressBar($output);
$p->start();
$users = $this->doctrine->getRepository(User::class)->findAll();
foreach ($users as $user) {
$p->advance();
$this->birthdayService->syncUser($user->getUsername());
}
$p->finish();
$output->writeln('');
return self::SUCCESS;
}
}