hanneskod /
readme-tester
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace hanneskod\readmetester\Runner; |
||
| 6 | |||
| 7 | use hanneskod\readmetester\Config\Configs; |
||
| 8 | use hanneskod\readmetester\Exception\InstantiatorException; |
||
| 9 | use hanneskod\readmetester\Exception\InvalidRunnerException; |
||
| 10 | use hanneskod\readmetester\Utils\Instantiator; |
||
| 11 | |||
| 12 | final class RunnerFactory |
||
| 13 | { |
||
| 14 | public function __construct( |
||
| 15 | private Instantiator $instantiator, |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 16 | ) {} |
||
| 17 | |||
| 18 | public function createRunner(string $id): RunnerInterface |
||
| 19 | { |
||
| 20 | try { |
||
| 21 | $runner = $this->instantiator->getNewObject( |
||
| 22 | Configs::expand(Configs::RUNNER_ID, $id), |
||
| 23 | ); |
||
| 24 | } catch (InstantiatorException $exception) { |
||
| 25 | throw new InvalidRunnerException("Invalid runner $id: {$exception->getMessage()}"); |
||
| 26 | } |
||
| 27 | |||
| 28 | if (!$runner instanceof RunnerInterface) { |
||
| 29 | throw new InvalidRunnerException( |
||
| 30 | "Invalid runner $id: Class does not implement RunnerInterface" |
||
| 31 | ); |
||
| 32 | } |
||
| 33 | |||
| 34 | return $runner; |
||
| 35 | } |
||
| 36 | } |
||
| 37 |