Code Coverage  | 
      ||||||||||
Lines  | 
       Functions and Methods  | 
       Classes and Traits  | 
      ||||||||
| Total |         | 
       100.00%  | 
       10 / 10  | 
               | 
       100.00%  | 
       2 / 2  | 
       CRAP |         | 
       100.00%  | 
       1 / 1  | 
      
| FileFakerFactory |         | 
       100.00%  | 
       10 / 10  | 
               | 
       100.00%  | 
       2 / 2  | 
       7 |         | 
       100.00%  | 
       1 / 1  | 
      
| __construct | n/a  | 
       0 / 0  | 
       n/a  | 
       0 / 0  | 
       1 | |||||
| getOrCreate |         | 
       100.00%  | 
       3 / 3  | 
               | 
       100.00%  | 
       1 / 1  | 
       2 | |||
| getSupportedFileFakers |         | 
       100.00%  | 
       7 / 7  | 
               | 
       100.00%  | 
       1 / 1  | 
       4 | |||
| 1 | <?php | 
| 2 | namespace Apie\Faker; | 
| 3 | |
| 4 | use Apie\Common\ValueObjects\EntityNamespace; | 
| 5 | use Apie\Faker\Interfaces\ApieFileFaker; | 
| 6 | use ReflectionClass; | 
| 7 | |
| 8 | final class FileFakerFactory | 
| 9 | { | 
| 10 | /** @var array<class-string<ApieFileFaker>, ApieFileFaker> $instantiated */ | 
| 11 | private static array $instantiated = []; | 
| 12 | |
| 13 | /** | 
| 14 | * @codeCoverageIgnore | 
| 15 | */ | 
| 16 | private function __construct() | 
| 17 | { | 
| 18 | } | 
| 19 | |
| 20 | /** | 
| 21 | * @param ReflectionClass<ApieFileFaker> $class | 
| 22 | */ | 
| 23 | private static function getOrCreate(ReflectionClass $class): ApieFileFaker | 
| 24 | { | 
| 25 | if (!isset(self::$instantiated[$class->name])) { | 
| 26 | self::$instantiated[$class->name] = $class->newInstance(); | 
| 27 | } | 
| 28 | return self::$instantiated[$class->name]; | 
| 29 | } | 
| 30 | |
| 31 | /** | 
| 32 | * @template T of ApieFileFaker | 
| 33 | * @param class-string<T> $interface | 
| 34 | * @return array<int, T> | 
| 35 | */ | 
| 36 | public static function getSupportedFileFakers(string $interface = ApieFileFaker::class): array | 
| 37 | { | 
| 38 | $ns = new EntityNamespace('Apie\Faker\FileFakers'); | 
| 39 | $supportedFileFakers = []; | 
| 40 | foreach ($ns->getClasses(__DIR__ . '/FileFakers') as $class) { | 
| 41 | if (in_array($interface, $class->getInterfaceNames()) | 
| 42 | && $class->getMethod('isSupported')->invoke(null)) { | 
| 43 | $supportedFileFakers[] = self::getOrCreate($class); | 
| 44 | } | 
| 45 | } | 
| 46 | return $supportedFileFakers; | 
| 47 | } | 
| 48 | } |