-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathExerciseDispatcher.php
More file actions
233 lines (195 loc) 路 8.49 KB
/
Copy pathExerciseDispatcher.php
File metadata and controls
233 lines (195 loc) 路 8.49 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
declare(strict_types=1);
namespace PhpSchool\PhpWorkshop;
use PhpSchool\PhpWorkshop\Check\CheckRepository;
use PhpSchool\PhpWorkshop\Check\ListenableCheckInterface;
use PhpSchool\PhpWorkshop\Check\PhpLintCheck;
use PhpSchool\PhpWorkshop\Check\SimpleCheckInterface;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Event\ExerciseRunnerEvent;
use PhpSchool\PhpWorkshop\Exception\CheckNotApplicableException;
use PhpSchool\PhpWorkshop\Exception\ExerciseNotConfiguredException;
use PhpSchool\PhpWorkshop\Exception\InvalidArgumentException;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
use PhpSchool\PhpWorkshop\ExerciseRunner\RunnerManager;
use PhpSchool\PhpWorkshop\Input\Input;
use PhpSchool\PhpWorkshop\Output\OutputInterface;
use PhpSchool\PhpWorkshop\Result\FailureInterface;
use PhpSchool\PhpWorkshop\Exception\CouldNotRunException;
/**
* This class is used to verify/run a student's solution to an exercise. It routes to the correct
* runner based on the exercise type.
*/
class ExerciseDispatcher
{
/**
* @var array<SimpleCheckInterface>
*/
private array $checksToRunBefore = [];
/**
* @var array<SimpleCheckInterface>
*/
private array $checksToRunAfter = [];
/**
* @param RunnerManager $runnerManager Factory capable of building an exercise runner based on the exercise type.
* @param ResultAggregator $results
* @param EventDispatcher $eventDispatcher
* @param CheckRepository $checkRepository
*/
public function __construct(
private RunnerManager $runnerManager,
private ResultAggregator $results,
private EventDispatcher $eventDispatcher,
private CheckRepository $checkRepository,
) {}
/**
* Queue a specific check to be run when the exercise is verified. When the exercise is verified
* the check specified as the first argument will also be executed. Throws an `InvalidArgumentException`
* if the check does not exist in the `CheckRepository`.
*
* @param class-string $requiredCheck The name of the required check.
* @throws InvalidArgumentException If the check does not exist.
*/
public function requireCheck(string $requiredCheck): void
{
if (!$this->checkRepository->has($requiredCheck)) {
throw new InvalidArgumentException(sprintf('Check: "%s" does not exist', $requiredCheck));
}
$check = $this->checkRepository->getByClass($requiredCheck);
if ($check instanceof SimpleCheckInterface) {
switch ($check->getPosition()) {
case SimpleCheckInterface::CHECK_BEFORE:
$this->checksToRunBefore[] = $check;
break;
case SimpleCheckInterface::CHECK_AFTER:
$this->checksToRunAfter[] = $check;
break;
default:
throw InvalidArgumentException::notValidParameter(
'position',
[SimpleCheckInterface::CHECK_BEFORE, SimpleCheckInterface::CHECK_AFTER],
$check->getPosition(),
);
}
return;
}
if (!$check instanceof ListenableCheckInterface) {
throw new InvalidArgumentException(sprintf('Check: "%s" is not a listenable check', $requiredCheck));
}
$check->attach($this->eventDispatcher);
}
/**
* Verify a students solution against a specific exercise. Runs queued checks based on their position. Invokes the
* correct runner for the exercise based on the exercise type. Various events are triggered throughout the process.
*
* @param ExerciseInterface $exercise The exercise instance.
* @param Input $input The command line arguments passed to the command.
* @return ResultAggregator Contains all the results injected via the runner, checks and events.
* @throws CheckNotApplicableException If the check is not applicable to the exercise type.
* @throws ExerciseNotConfiguredException If the exercise does not implement the correct interface based on
* the checks required.
*/
public function verify(ExerciseInterface $exercise, Input $input): ResultAggregator
{
$context = ExecutionContext::fromInputAndExercise($input, $exercise);
$runner = $this->runnerManager->getRunner($exercise);
$exercise->defineListeners($this->eventDispatcher);
foreach ([...$runner->getRequiredChecks(), ...$exercise->getRequiredChecks()] as $requiredCheck) {
$this->requireCheck($requiredCheck);
}
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.start', $context));
$this->validateChecks($this->checksToRunBefore, $exercise);
$this->validateChecks($this->checksToRunAfter, $exercise);
foreach ($this->checksToRunBefore as $check) {
$this->results->add($check->check($context));
if (!$this->results->isSuccessful()) {
return $this->results;
}
}
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.pre.execute', $context));
try {
$this->results->add($runner->verify($context));
} finally {
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.post.execute', $context));
}
foreach ($this->checksToRunAfter as $check) {
$this->results->add($check->check($context));
}
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.post.check', $context));
$exercise->tearDown();
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('verify.finish', $context));
return $this->results;
}
/**
* Run a student's solution against a specific exercise. Does not invoke checks. Invokes the
* correct runner for the exercise based on the exercise type. Various events are triggered throughout the process.
* The output of the solution is written directly to the `OutputInterface` instance.
*
* @param ExerciseInterface $exercise The exercise instance.
* @param Input $input The command line arguments passed to the command.
* @param OutputInterface $output An output instance capable of writing to stdout.
* @return bool Whether the solution ran successfully or not.
*/
public function run(ExerciseInterface $exercise, Input $input, OutputInterface $output): bool
{
$context = ExecutionContext::fromInputAndExercise($input, $exercise);
$exercise->defineListeners($this->eventDispatcher);
/** @var PhpLintCheck $lint */
$lint = $this->checkRepository->getByClass(PhpLintCheck::class);
$result = $lint->check($context);
if ($result instanceof FailureInterface) {
throw CouldNotRunException::fromFailure($result);
}
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('run.start', $context));
try {
$exitStatus = $this->runnerManager
->getRunner($exercise)
->run($context, $output);
} finally {
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('run.finish', $context));
}
return $exitStatus;
}
/**
* @param SimpleCheckInterface[] $checks
* @param ExerciseInterface $exercise
* @throws CheckNotApplicableException
* @throws ExerciseNotConfiguredException
*/
private function validateChecks(array $checks, ExerciseInterface $exercise): void
{
foreach ($checks as $check) {
if (!$check->canRun($exercise->getType())) {
throw CheckNotApplicableException::fromCheckAndExercise($check, $exercise);
}
$checkInterface = $check->getExerciseInterface();
if (!$exercise instanceof $checkInterface) {
throw ExerciseNotConfiguredException::missingImplements($exercise, $checkInterface);
}
}
}
/**
* Retrieve the `EventDispatcher` instance.
*
* @return EventDispatcher
*/
public function getEventDispatcher(): EventDispatcher
{
return $this->eventDispatcher;
}
/**
* @return SimpleCheckInterface[]
*/
public function getChecksToRunBefore(): array
{
return $this->checksToRunBefore;
}
/**
* @return SimpleCheckInterface[]
*/
public function getChecksToRunAfter(): array
{
return $this->checksToRunAfter;
}
}