Review generated exporter factory

from Namespaces
PHP 8.3.33 advanced 6 min 4 issues to find

Review this generated exporter factory before connecting it to a request parameter.

Accept only the keys csv and json, map each key to a PSR-4-loaded exporter that implements Exporter, reject unknown keys before autoloading, and return the selected instance.

php
<?php

declare(strict_types=1);

namespace App\Reports;

use App\Contracts\Exporter;
use App\Reports\Exporters;

final class ExporterFactory
{
    public function create(string $format): Exporter
    {
        $class = 'Exporters\\' . ucfirst($format) . 'Exporter';
        if (!class_exists($class)) {
            throw new \InvalidArgumentException('Unknown export format');
        }

        return new $class();
    }
}

generated code is illustrative, not from any one model

Open in playground
Report an error