protected function initialize(TaskConfiguration $taskConfiguration): void
    {
        $this->taskConfiguration = $taskConfiguration;

        if ($taskConfiguration->getErrorStrategy() === TaskConfiguration::STRATEGY_STOP
            && \count($taskConfiguration->getErrorOutputs()) > 0) {
            $m = "Task configuration {$taskConfiguration->getCode()} has error outputs ";
            $m .= "but it's error strategy 'stop' implies they will never be reached.";
            $this->taskLogger->error($m);
        }
        // @todo Refactor this using a Registry with this feature:
        // https://symfony.com/doc/current/service_container/service_subscribers_locators.html
        $serviceReference = $taskConfiguration->getServiceReference();
        if (0 === strpos($serviceReference, '@')) {
            $task = $this->container->get(ltrim($serviceReference, '@'));
        } elseif ($this->container->has($serviceReference)) {
            $task = $this->container->get($serviceReference);
        } else {
            throw new \UnexpectedValueException(
                "Unable to resolve service reference for Task '{$taskConfiguration->getCode()}'"
            );
        }
        if (!$task instanceof TaskInterface) {
            throw new \UnexpectedValueException(
                "Service defined in Task '{$taskConfiguration->getCode()}' is not a TaskInterface"
            );
        }
        $taskConfiguration->setTask($task);

        if ($task instanceof InitializableTaskInterface) {
            $state = $taskConfiguration->getState();
            try {
                $task->initialize($state);
            } catch (\Throwable $e) {
                $logContext = ['exception' => $e];
                $this->taskLogger->critical($e->getMessage(), $logContext);
                $state->stop($e);
            }
        }
        $this->handleState($taskConfiguration->getState());

        $this->taskConfiguration = null;
    }