| Total Complexity | 16 |
| Total Lines | 107 |
| Duplicated Lines | 0 % |
| Coverage | 77.27% |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 14 | final class DependencyResolver |
||
| 15 | { |
||
| 16 | private GacelaConfigFileInterface $gacelaConfigFile; |
||
| 17 | |||
| 18 | 14 | public function __construct(GacelaConfigFileInterface $gacelaConfigFile) |
|
| 21 | 14 | } |
|
| 22 | |||
| 23 | /** |
||
| 24 | * @param class-string $resolvedClassName |
||
|
1 ignored issue
–
show
|
|||
| 25 | * |
||
| 26 | * @return list<mixed> |
||
| 27 | */ |
||
| 28 | 14 | public function resolveDependencies(string $resolvedClassName): array |
|
| 29 | { |
||
| 30 | 14 | $reflection = new ReflectionClass($resolvedClassName); |
|
| 31 | 14 | $constructor = $reflection->getConstructor(); |
|
| 32 | 14 | if (!$constructor) { |
|
| 33 | 13 | return []; |
|
|
1 ignored issue
–
show
|
|||
| 34 | } |
||
| 35 | |||
| 36 | /** @var list<mixed> $dependencies */ |
||
| 37 | 1 | $dependencies = []; |
|
| 38 | 1 | foreach ($constructor->getParameters() as $parameter) { |
|
| 39 | 1 | $paramType = $parameter->getType(); |
|
| 40 | 1 | if ($paramType) { |
|
| 41 | /** @psalm-suppress MixedAssignment */ |
||
| 42 | 1 | $dependencies[] = $this->resolveDependenciesRecursively($parameter); |
|
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | 1 | return $dependencies; |
|
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @return mixed |
||
| 51 | */ |
||
| 52 | 1 | private function resolveDependenciesRecursively(ReflectionParameter $parameter) |
|
| 53 | { |
||
| 54 | /** @var ReflectionNamedType $paramType */ |
||
| 55 | 1 | $paramType = $parameter->getType(); |
|
| 56 | 1 | $type = $paramType->getName(); |
|
| 57 | |||
| 58 | 1 | if (!class_exists($type) && !interface_exists($type)) { |
|
| 59 | return $parameter->getDefaultValue(); |
||
| 60 | } |
||
| 61 | |||
| 62 | 1 | $reflection = new ReflectionClass($type); |
|
| 63 | |||
| 64 | // If it's an interface we need to figure out which concrete class do we want to use |
||
| 65 | 1 | if ($reflection->isInterface()) { |
|
| 66 | 1 | $gacelaFileDependencies = $this->gacelaConfigFile->dependencies(); |
|
| 67 | 1 | $concreteClass = $gacelaFileDependencies[$reflection->getName()] ?? ''; |
|
| 68 | // a callable will be a way to bypass the instantiation and instead |
||
| 69 | // use the result from the callable that was defined in the gacela config file. |
||
| 70 | 1 | if (is_callable($concreteClass)) { |
|
| 71 | return $concreteClass(); |
||
| 72 | } |
||
| 73 | // if at this point there is no concrete class found for the interface we can |
||
| 74 | // try one more thing looking for 1 concrete class that implements this interface |
||
| 75 | 1 | if (empty($concreteClass)) { |
|
| 76 | $concreteClass = $this->findConcreteClassThatImplements($reflection); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** @var class-string $concreteClass */ |
||
| 80 | 1 | $reflection = new ReflectionClass($concreteClass); |
|
| 81 | } |
||
| 82 | |||
| 83 | 1 | $constructor = $reflection->getConstructor(); |
|
| 84 | 1 | if (!$constructor) { |
|
| 85 | 1 | return $reflection->newInstance(); |
|
| 86 | } |
||
| 87 | |||
| 88 | /** @var list<mixed> $innerDependencies */ |
||
| 89 | 1 | $innerDependencies = []; |
|
| 90 | |||
| 91 | 1 | foreach ($constructor->getParameters() as $constructorParameter) { |
|
| 92 | 1 | $paramType = $constructorParameter->getType(); |
|
| 93 | 1 | if ($paramType) { |
|
| 94 | /** @psalm-suppress MixedAssignment */ |
||
| 95 | 1 | $innerDependencies[] = $this->resolveDependenciesRecursively($constructorParameter); |
|
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | 1 | return $reflection->newInstanceArgs($innerDependencies); |
|
|
1 ignored issue
–
show
|
|||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return class-string|string |
||
|
1 ignored issue
–
show
|
|||
| 104 | */ |
||
| 105 | private function findConcreteClassThatImplements(ReflectionClass $interface): string |
||
| 121 | } |
||
| 122 | } |
||
| 123 |