-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAssignmentIngesterCommand.php
More file actions
195 lines (151 loc) · 6.51 KB
/
Copy pathAssignmentIngesterCommand.php
File metadata and controls
195 lines (151 loc) · 6.51 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2020 (original work) Open Assessment Technologies S.A.
*/
declare(strict_types=1);
namespace OAT\SimpleRoster\Command\Ingester;
use OAT\SimpleRoster\Csv\CsvReaderBuilder;
use OAT\SimpleRoster\DataTransferObject\AssignmentDto;
use OAT\SimpleRoster\DataTransferObject\AssignmentDtoCollection;
use OAT\SimpleRoster\Entity\Assignment;
use OAT\SimpleRoster\Exception\LineItemNotFoundException;
use OAT\SimpleRoster\Ingester\AssignmentIngester;
use OAT\SimpleRoster\Model\LineItemCollection;
use OAT\SimpleRoster\Repository\LineItemRepository;
use OAT\SimpleRoster\Storage\StorageRegistry;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
class AssignmentIngesterCommand extends AbstractCsvIngesterCommand
{
public const NAME = 'roster:ingest:assignment';
private AssignmentIngester $ingester;
private LineItemRepository $lineItemRepository;
public function __construct(
CsvReaderBuilder $csvReaderBuilder,
StorageRegistry $storageRegistry,
AssignmentIngester $assignmentIngester,
LineItemRepository $lineItemRepository
) {
$this->csvReaderBuilder = $csvReaderBuilder;
$this->ingester = $assignmentIngester;
$this->lineItemRepository = $lineItemRepository;
parent::__construct($csvReaderBuilder, $storageRegistry);
}
protected function getIngesterCommandName(): string
{
return self::NAME;
}
protected function configure(): void
{
parent::configure();
$this->setDescription('Ingests assignments into the application');
$this->setHelp(<<<'EOF'
The <info>%command.name%</info> command ingests assignments into the application.
<info>php %command.full_name% <path></info>
To ingest from a local csv file:
<info>php %command.full_name% relative/path/to/csv --force</info>
Use the --batch option to use custom batch size for ingestion:
<info>php %command.full_name% relative/path/to/csv --batch=10000 --force</info>
Use the --storage option to ingest from custom sources other than local filesystem (e.g. S3 bucket)):
<info>php %command.full_name% relative/path/to/csv --storage=customStorage --force</info>
<comment>(Documentation: https://github.com/oat-sa/simple-roster/blob/develop/docs/storage-registry.md)</comment>
Use the --delimiter option to define custom csv column delimiter:
<info>php %command.full_name% relative/path/to/csv --delimiter=| --force</info>
EOF
);
}
protected function initialize(InputInterface $input, OutputInterface $output): void
{
parent::initialize($input, $output);
$this->symfonyStyle->title('Simple Roster - Assignment Ingester');
}
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->symfonyStyle->comment('Executing ingestion...');
try {
$lineItems = $this->lineItemRepository->findAllAsCollection();
if ($lineItems->isEmpty()) {
throw new LineItemNotFoundException("No line items were found in database.");
}
$this->progressBar->start();
$assignmentDtoCollection = new AssignmentDtoCollection();
$numberOfProcessedRows = 0;
foreach ($this->csvReader->getRecords() as $rawAssignment) {
$this->validateRow($rawAssignment, 'username', 'lineItemSlug');
$numberOfProcessedRows++;
$assignmentDto = $this->createAssignmentDto(
$lineItems,
$rawAssignment['lineItemSlug'],
$rawAssignment['username']
);
$assignmentDtoCollection->add($assignmentDto);
if ($this->batchProcessable($numberOfProcessedRows)) {
if (!$this->isDryRun) {
$this->ingester->ingest($assignmentDtoCollection);
}
$assignmentDtoCollection->clear();
$this->progressBar->advance($this->batchSize);
}
}
if (!$this->isDryRun && !$assignmentDtoCollection->isEmpty()) {
$this->ingester->ingest($assignmentDtoCollection);
}
$this->progressBar->finish();
$this->symfonyStyle->newLine(2);
$verificationCommentMessage = sprintf(
'To verify you can run: <options=bold>%s</>',
'bin/console dbal:run-sql "SELECT COUNT(*) FROM assignments"'
);
if ($this->isDryRun) {
$this->symfonyStyle->warning(
sprintf(
'[DRY RUN] %s assignments have been successfully ingested.',
number_format($numberOfProcessedRows)
)
);
$this->symfonyStyle->comment($verificationCommentMessage);
return 0;
}
$this->symfonyStyle->success(
sprintf(
'%s assignments have been successfully ingested.',
number_format($numberOfProcessedRows)
)
);
$this->symfonyStyle->comment($verificationCommentMessage);
} catch (Throwable $exception) {
$this->symfonyStyle->error($exception->getMessage());
return 1;
}
return 0;
}
/**
* @throws LineItemNotFoundException
*/
private function createAssignmentDto(
LineItemCollection $lineItems,
string $lineItemSlug,
string $username
): AssignmentDto {
$lineItem = $lineItems->getBySlug($lineItemSlug);
return new AssignmentDto(Assignment::STATE_READY, (int)$lineItem->getId(), $username);
}
}