| Total Complexity | 5 |
| Total Lines | 39 |
| Duplicated Lines | 0 % |
| Coverage | 92.31% |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 15 | final class FormatterFactory |
||
| 16 | { |
||
| 17 | /** @var string[] */ |
||
| 18 | private array $formatters; |
||
| 19 | |||
| 20 | 2 | public function __construct(FileLocatorInterface $fileLocator) |
|
| 21 | { |
||
| 22 | 2 | $this->formatters = $this->parseFormatters($fileLocator->locate('formatters.yaml')); |
|
|
|
|||
| 23 | 2 | } |
|
| 24 | |||
| 25 | /** |
||
| 26 | * Creates formatter for first acceptable format |
||
| 27 | * |
||
| 28 | * @param string ...$acceptableFormats |
||
| 29 | * @return FormatterInterface |
||
| 30 | * @throws NotAcceptableHttpException if proper formatter does not exist |
||
| 31 | */ |
||
| 32 | 2 | public function create(string ...$acceptableFormats): FormatterInterface |
|
| 33 | { |
||
| 34 | 2 | foreach ($acceptableFormats as $format) { |
|
| 35 | 2 | if (true === array_key_exists($format, $this->formatters)) { |
|
| 36 | 1 | return new $this->formatters[$format]; |
|
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | 1 | throw new NotAcceptableHttpException( |
|
| 41 | sprintf( |
||
| 42 | 1 | 'Unable to provide an acceptable representation. Available alternatives: %s.', |
|
| 43 | 1 | implode(', ', array_keys($this->formatters)) |
|
| 44 | ) |
||
| 45 | ); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @return string[] |
||
| 50 | */ |
||
| 51 | 2 | private function parseFormatters(string $file): array |
|
| 54 | } |
||
| 55 | } |
||
| 56 |