Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
33 / 33 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| ObjectProvider | |
100.00% |
33 / 33 |
|
100.00% |
6 / 6 |
19 | |
100.00% |
1 / 1 |
| getMapping | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
8 | |||
| getAvailableServices | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
7 | |||
| getAvailableValueObjects | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAvailableLists | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAvailableHashmaps | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAvailableDtos | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Apie\ApieCommonPlugin; |
| 4 | |
| 5 | use Apie\Core\Dto\DtoInterface; |
| 6 | use Apie\Core\Entities\EntityInterface; |
| 7 | use Apie\Core\Lists\ItemHashmap; |
| 8 | use Apie\Core\Lists\ItemList; |
| 9 | use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface; |
| 10 | use ReflectionClass; |
| 11 | use ReflectionException; |
| 12 | |
| 13 | abstract class ObjectProvider |
| 14 | { |
| 15 | private const DEFINED_GETTERS = [ |
| 16 | 'Entities' => EntityInterface::class, |
| 17 | 'ValueObjects' => ValueObjectInterface::class, |
| 18 | 'Lists' => ItemList::class, |
| 19 | 'Hashmaps' => ItemHashmap::class, |
| 20 | 'Dtos' => DtoInterface::class, |
| 21 | ]; |
| 22 | |
| 23 | protected const DEFINED_CLASSES = []; |
| 24 | |
| 25 | /** |
| 26 | * @var array<string, array<string, array<int, string>>> |
| 27 | */ |
| 28 | private static array $mapping = []; |
| 29 | |
| 30 | /** |
| 31 | * @var array<string, array<int, string>> |
| 32 | */ |
| 33 | private static array $mappedServices = []; |
| 34 | |
| 35 | private static function getMapping(): array |
| 36 | { |
| 37 | $key = static::class; |
| 38 | if (!isset(self::$mapping[$key])) { |
| 39 | self::$mapping = []; |
| 40 | foreach (self::DEFINED_GETTERS as $name => $interface) { |
| 41 | self::$mapping[$key][$name] = []; |
| 42 | } |
| 43 | foreach (static::DEFINED_CLASSES as $apieObject) { |
| 44 | try { |
| 45 | $refl = new ReflectionClass($apieObject); |
| 46 | $interfaceNames = [$refl->name, ...$refl->getInterfaceNames()]; |
| 47 | while ($refl = $refl->getParentClass()) { |
| 48 | $interfaceNames[] = $refl->name; |
| 49 | } |
| 50 | foreach (self::DEFINED_GETTERS as $name => $interfaceName) { |
| 51 | if (in_array($interfaceName, $interfaceNames)) { |
| 52 | self::$mapping[$key][$name][] = $apieObject; |
| 53 | } |
| 54 | } |
| 55 | } catch (ReflectionException) { |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | return self::$mapping[$key]; |
| 60 | } |
| 61 | |
| 62 | final public static function getAvailableServices(): array |
| 63 | { |
| 64 | $key = static::class; |
| 65 | if (!isset(self::$mappedServices[$key])) { |
| 66 | self::$mappedServices[$key] = []; |
| 67 | foreach (static::DEFINED_CLASSES as $apieObject) { |
| 68 | try { |
| 69 | $refl = new ReflectionClass($apieObject); |
| 70 | $interfaceNames = [$refl->name, ...$refl->getInterfaceNames()]; |
| 71 | while ($refl = $refl->getParentClass()) { |
| 72 | $interfaceNames[] = $refl->name; |
| 73 | } |
| 74 | foreach (self::DEFINED_GETTERS as $interfaceName) { |
| 75 | if (in_array($interfaceName, $interfaceNames)) { |
| 76 | continue(2); |
| 77 | } |
| 78 | } |
| 79 | self::$mappedServices[$key][] = $apieObject; |
| 80 | } catch (ReflectionException) { |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | return self::$mappedServices[$key]; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | final public static function getAvailableValueObjects(): array |
| 89 | { |
| 90 | return static::getMapping()['ValueObjects'] ?? []; |
| 91 | } |
| 92 | |
| 93 | final public static function getAvailableLists(): array |
| 94 | { |
| 95 | return static::getMapping()['Lists'] ?? []; |
| 96 | } |
| 97 | |
| 98 | final public static function getAvailableHashmaps(): array |
| 99 | { |
| 100 | return static::getMapping()['Hashmaps'] ?? []; |
| 101 | } |
| 102 | |
| 103 | final public static function getAvailableDtos(): array |
| 104 | { |
| 105 | return static::getMapping()['Dtos'] ?? []; |
| 106 | } |
| 107 | } |