Repository navigation
Expand file tree
/
Copy pathSyncWindowCommandTrait.php
More file actions
80 lines (67 loc) · 2.44 KB
/
Copy pathSyncWindowCommandTrait.php
File metadata and controls
80 lines (67 loc) · 2.44 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
70
71
72
73
74
75
76
77
78
79
80
<?php
/*
* Copyright (c) 2026 Netresearch DTT GmbH
* SPDX-License-Identifier: AGPL-3.0-only
*/
declare(strict_types=1);
namespace App\Command;
use App\Entity\SyncRun;
use App\Enum\SyncRunStatus;
use App\Service\Sync\SyncRunConsoleRenderer;
use DateTimeImmutable;
use Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Style\SymfonyStyle;
use function sprintf;
use function trim;
/**
* Shared --from/--to handling and run reporting of the Personio cron commands
* (import and export take the same options and report the same way).
*/
trait SyncWindowCommandTrait
{
/**
* Resolves the day window from the raw --from/--to values, falling back to the given
* offsets from today; reports the problem and returns null when a date is unusable.
*
* @return array{DateTimeImmutable, DateTimeImmutable}|null
*/
private function resolveWindow(SymfonyStyle $symfonyStyle, ?string $from, ?string $to, string $defaultFromOffset, string $defaultToOffset): ?array
{
if ($this->isBlank($from) || $this->isBlank($to)) {
$symfonyStyle->error('Invalid date in --from/--to: must not be blank (expected Y-m-d)');
return null;
}
try {
$fromDate = new DateTimeImmutable($from ?? 'today');
$toDate = new DateTimeImmutable($to ?? 'today');
} catch (Exception) {
$symfonyStyle->error(sprintf('Invalid date in --from/--to (expected Y-m-d): %s / %s', $from ?? '-', $to ?? '-'));
return null;
}
return [
null !== $from ? $fromDate : $fromDate->modify($defaultFromOffset),
null !== $to ? $toDate : $toDate->modify($defaultToOffset),
];
}
/**
* Renders every run and returns the command exit code: failure when any run failed.
*
* @param list<SyncRun> $runs
*/
private function renderRuns(SymfonyStyle $symfonyStyle, SyncRunConsoleRenderer $syncRunConsoleRenderer, array $runs, string $label): int
{
$failed = false;
foreach ($runs as $syncRun) {
$syncRunConsoleRenderer->render($symfonyStyle, $syncRun, $label);
if (SyncRunStatus::FAILED === $syncRun->getStatus()) {
$failed = true;
}
}
return $failed ? Command::FAILURE : Command::SUCCESS;
}
private function isBlank(?string $value): bool
{
return null !== $value && '' === trim($value);
}
}