From 0ff769f2c737a649f28996e440c3f5f53e05fc96 Mon Sep 17 00:00:00 2001 From: mscherer Date: Sat, 9 Mar 2024 17:23:12 +0100 Subject: [PATCH] Backport #415 --- src/Queue/ServicesTrait.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Queue/ServicesTrait.php b/src/Queue/ServicesTrait.php index 2e7ca99d..d005d003 100644 --- a/src/Queue/ServicesTrait.php +++ b/src/Queue/ServicesTrait.php @@ -4,23 +4,32 @@ namespace Queue\Queue; use Cake\Core\ContainerInterface; +use LogicException; trait ServicesTrait { /** - * @var \Cake\Core\ContainerInterface + * @var \Cake\Core\ContainerInterface|null */ - protected ContainerInterface $container; + protected ?ContainerInterface $container = null; /** - * @param string $id Classname or identifier of the service you want to retrieve + * @template T + * + * @param class-string $id Classname or identifier of the service you want to retrieve * - * @throws \Psr\Container\NotFoundExceptionInterface * @throws \Psr\Container\ContainerExceptionInterface + * @throws \Psr\Container\NotFoundExceptionInterface * - * @return mixed + * @return T */ - protected function getService(string $id): mixed { + protected function getService(string $id) { + if ($this->container === null) { + throw new LogicException( + "The Container has not been set. Hint: getService() must not be called in the Task's constructor.", + ); + } + return $this->container->get($id); }