Lines 100.00% 10 / 10
Methods 100.00% 2 / 2
Classes 100.00% 1 / 1
Covered by tests of size
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 getController 100.00% 9 / 9 100.00% 1 / 1 6
31class ContainerControllerResolver extends ControllerResolver
32{
33    public function __construct(
34        private readonly ContainerInterface $container,
35    ) {
36        parent::__construct();
37    }
38
39    #[Override]
40    public function getController(Request $request): callable|false
41    {
42        $controllerAttr = $request->attributes->get('_controller');
43
44        // If the controller is in ClassName::method format and registered in the container,
45        // resolve it from the container BEFORE the parent tries to instantiate with `new`.
46        if (is_string($controllerAttr) && str_contains($controllerAttr, '::')) {
47            [$class, $method] = explode('::', $controllerAttr, limit: 2);
48            if (class_exists($class) && $this->container->has($class)) {
49                $instance = $this->container->get($class);
50                $controllerCallable = [$instance, $method];
51                if (is_callable($controllerCallable)) {
52                    /* @mago-expect analysis:less-specific-nested-return-statement - is_callable proves the array is a valid callable */
53                    return $controllerCallable;
54                }
55            }
56        }
57
58        // Fall back to default resolution for unregistered controllers
59        return parent::getController($request);
60    }
61}