-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLtiInstanceIngesterCommand.php
More file actions
171 lines (131 loc) · 5.5 KB
/
Copy pathLtiInstanceIngesterCommand.php
File metadata and controls
171 lines (131 loc) · 5.5 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
<?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\Entity\LtiInstance;
use OAT\SimpleRoster\Repository\LtiInstanceRepository;
use OAT\SimpleRoster\Storage\StorageRegistry;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
class LtiInstanceIngesterCommand extends AbstractCsvIngesterCommand
{
public const NAME = 'roster:ingest:lti-instance';
/** @var LtiInstanceRepository */
private LtiInstanceRepository $ltiInstanceRepository;
public function __construct(
CsvReaderBuilder $csvReaderBuilder,
StorageRegistry $storageRegistry,
LtiInstanceRepository $ltiInstanceRepository
) {
parent::__construct($csvReaderBuilder, $storageRegistry);
$this->ltiInstanceRepository = $ltiInstanceRepository;
}
protected function getIngesterCommandName(): string
{
return self::NAME;
}
protected function configure(): void
{
parent::configure();
$this->setDescription('Ingests LTI instances into the application');
$this->setHelp(<<<'EOF'
The <info>%command.name%</info> command ingests LTI instances 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 - LTI Instance Ingester');
}
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->symfonyStyle->comment('Executing ingestion...');
try {
$this->progressBar->start(1);
$numberOfProcessedRows = 0;
foreach ($this->csvReader->getRecords() as $rawLtiInstance) {
$this->validateRow($rawLtiInstance, 'label', 'ltiLink', 'ltiKey', 'ltiSecret');
$numberOfProcessedRows++;
$this->ltiInstanceRepository->persist($this->createLtiInstance($rawLtiInstance));
if ($this->batchProcessable($numberOfProcessedRows)) {
if (!$this->isDryRun) {
$this->ltiInstanceRepository->flush();
}
$this->progressBar->advance($this->batchSize);
}
}
if (!$this->isDryRun) {
$this->ltiInstanceRepository->flush();
}
$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 lti_instances"'
);
if ($this->isDryRun) {
$this->symfonyStyle->warning(
sprintf(
'[DRY RUN] %s LTI instances have been successfully ingested.',
number_format($numberOfProcessedRows)
)
);
$this->symfonyStyle->comment($verificationCommentMessage);
return 0;
}
$this->symfonyStyle->success(
sprintf(
'%s LTI instances have been successfully ingested.',
number_format($numberOfProcessedRows)
)
);
$this->symfonyStyle->comment($verificationCommentMessage);
} catch (Throwable $exception) {
$this->symfonyStyle->error($exception->getMessage());
return 1;
}
return 0;
}
private function createLtiInstance(array $rawLtiInstance): LtiInstance
{
return new LtiInstance(
0,
$rawLtiInstance['label'],
$rawLtiInstance['ltiLink'],
$rawLtiInstance['ltiKey'],
$rawLtiInstance['ltiSecret']
);
}
}