|
7 | 7 | use PhpSchool\PhpWorkshop\Check\CheckInterface; |
8 | 8 | use PhpSchool\PhpWorkshop\Check\CheckRepository; |
9 | 9 | use PhpSchool\PhpWorkshop\Check\ListenableCheckInterface; |
| 10 | +use PhpSchool\PhpWorkshop\Check\SimpleCheckInterface; |
10 | 11 | use PhpSchool\PhpWorkshop\Event\Event; |
11 | 12 | use PhpSchool\PhpWorkshop\Event\EventDispatcher; |
12 | 13 | use PhpSchool\PhpWorkshop\Exception\CheckNotApplicableException; |
|
24 | 25 | */ |
25 | 26 | class ExerciseDispatcher |
26 | 27 | { |
27 | | - const CHECK_BEFORE = 'before'; |
28 | | - const CHECK_AFTER = 'after'; |
29 | | - |
30 | 28 | /** |
31 | 29 | * @var CheckInterface[] |
32 | 30 | */ |
@@ -77,42 +75,34 @@ public function __construct( |
77 | 75 |
|
78 | 76 | /** |
79 | 77 | * @param string $requiredCheck |
80 | | - * @param string $position |
81 | 78 | * @throws InvalidArgumentException |
82 | 79 | */ |
83 | | - public function requireCheck($requiredCheck, $position) |
| 80 | + public function requireCheck($requiredCheck) |
84 | 81 | { |
85 | 82 | if (!$this->checkRepository->has($requiredCheck)) { |
86 | 83 | throw new InvalidArgumentException(sprintf('Check: "%s" does not exist', $requiredCheck)); |
87 | 84 | } |
88 | 85 |
|
89 | | - switch ($position) { |
90 | | - case static::CHECK_BEFORE: |
91 | | - $this->checksToRunBefore[] = $this->checkRepository->getByClass($requiredCheck); |
92 | | - break; |
93 | | - case static::CHECK_AFTER: |
94 | | - $this->checksToRunAfter[] = $this->checkRepository->getByClass($requiredCheck); |
95 | | - break; |
96 | | - default: |
97 | | - throw InvalidArgumentException::notValidParameter( |
98 | | - 'position', |
99 | | - [static::CHECK_BEFORE, static::CHECK_AFTER], |
100 | | - $position |
101 | | - ); |
102 | | - } |
103 | | - } |
| 86 | + $check = $this->checkRepository->getByClass($requiredCheck); |
104 | 87 |
|
105 | | - /** |
106 | | - * @param string $requiredCheck |
107 | | - * @throws InvalidArgumentException |
108 | | - */ |
109 | | - public function requireListenableCheck($requiredCheck) |
110 | | - { |
111 | | - if (!$this->checkRepository->has($requiredCheck)) { |
112 | | - throw new InvalidArgumentException(sprintf('Check: "%s" does not exist', $requiredCheck)); |
113 | | - } |
| 88 | + if ($check instanceof SimpleCheckInterface) { |
| 89 | + switch ($check->getPosition()) { |
| 90 | + case SimpleCheckInterface::CHECK_BEFORE: |
| 91 | + $this->checksToRunBefore[] = $check; |
| 92 | + break; |
| 93 | + case SimpleCheckInterface::CHECK_AFTER: |
| 94 | + $this->checksToRunAfter[] = $check; |
| 95 | + break; |
| 96 | + default: |
| 97 | + throw InvalidArgumentException::notValidParameter( |
| 98 | + 'position', |
| 99 | + [SimpleCheckInterface::CHECK_BEFORE, SimpleCheckInterface::CHECK_AFTER], |
| 100 | + $check->getPosition() |
| 101 | + ); |
| 102 | + } |
114 | 103 |
|
115 | | - $check = $this->checkRepository->getByClass($requiredCheck); |
| 104 | + return; |
| 105 | + } |
116 | 106 |
|
117 | 107 | if (!$check instanceof ListenableCheckInterface) { |
118 | 108 | throw new InvalidArgumentException(sprintf('Check: "%s" is not a listenable check', $requiredCheck)); |
|
0 commit comments