Repository navigation
Expand file tree
/
Copy pathImportCommand.php
More file actions
112 lines (93 loc) · 3.07 KB
/
Copy pathImportCommand.php
File metadata and controls
112 lines (93 loc) · 3.07 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php declare(strict_types=1);
namespace Fop\Command;
use DateTimeInterface;
use Fop\Contract\MeetupImporterInterface;
use Fop\Entity\Meetup;
use Fop\MeetupCom\Command\Reporter\MeetupReporter;
use Fop\Repository\MeetupRepository;
use Nette\Utils\DateTime;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symplify\PackageBuilder\Console\Command\CommandNaming;
use Symplify\PackageBuilder\Console\ShellCode;
use function Safe\sprintf;
final class ImportCommand extends Command
{
/**
* @var MeetupImporterInterface[]
*/
private $meetupImporters = [];
/**
* @var SymfonyStyle
*/
private $symfonyStyle;
/**
* @var DateTimeInterface
*/
private $maxForecastDateTime;
/**
* @var MeetupRepository
*/
private $meetupRepository;
/**
* @var MeetupReporter
*/
private $meetupReporter;
/**
* @param MeetupImporterInterface[] $meetupImporters
*/
public function __construct(
array $meetupImporters,
int $maxForecastDays,
SymfonyStyle $symfonyStyle,
MeetupRepository $meetupRepository,
MeetupReporter $meetupReporter
) {
parent::__construct();
$this->meetupImporters = $meetupImporters;
$this->symfonyStyle = $symfonyStyle;
$this->meetupRepository = $meetupRepository;
$this->meetupReporter = $meetupReporter;
$this->maxForecastDateTime = DateTime::from('+' . $maxForecastDays . 'days');
}
protected function configure(): void
{
$this->setName(CommandNaming::classToName(self::class));
$this->setDescription('Import meetups from meetup providers');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
foreach ($this->meetupImporters as $meetupImporter) {
$this->symfonyStyle->note(sprintf('Importing meetups from "%s"', $meetupImporter->getKey()));
$meetups = $meetupImporter->getMeetups();
$meetups = $this->filterOutTooFarMeetups($meetups);
$this->saveAndReportMeetups($meetups, $meetupImporter->getKey());
}
$this->symfonyStyle->success('Import is done!');
return ShellCode::SUCCESS;
}
/**
* @param Meetup[] $meetups
* @return Meetup[]
*/
private function filterOutTooFarMeetups(array $meetups): array
{
return array_filter($meetups, function (Meetup $meetup) {
return $meetup->getStartDateTime() <= $this->maxForecastDateTime;
});
}
/**
* @param Meetup[] $meetups
*/
private function saveAndReportMeetups(array $meetups, string $key): void
{
if (count($meetups) === 0) {
return;
}
$this->meetupReporter->printMeetups($meetups);
$this->meetupRepository->saveImportsToFile($meetups, $key);
$this->symfonyStyle->success(sprintf('Loaded %d meetups from "%s"', count($meetups), $key));
}
}