*/ namespace oat\taoProctoring\model; use oat\oatbox\service\ConfigurableService; use oat\oatbox\user\User; abstract class ServiceDelegator extends ConfigurableService implements ServiceDelegatorInterface { /** * @var ConfigurableService */ private $service; /** * Returns applicable service * * @throws \common_exception_NoImplementation * @param $user * @param $deliveryId * @return DelegatedServiceHandler */ public function getResponsibleService(User $user, $deliveryId = null) { if (!isset($this->service)) { /** @var DelegatedServiceHandler $handler */ foreach ($this->getOption(self::SERVICE_HANDLERS) as $handler) { if (!is_a($handler, DelegatedServiceHandler::class)) { throw new \common_exception_NoImplementation('Handler should be instance of DelegatorServiceHandler.'); } $handler->setServiceLocator($this->getServiceLocator()); if ($handler->isSuitable($user, $deliveryId)) { $this->service = $handler; break; } } } return $this->service; } /** * @param $handler */ public function registerHandler(DelegatedServiceHandler $handler) { $handlers = $this->getOption(self::SERVICE_HANDLERS); $exists = false; // change options on the existing handlers foreach ($handlers as $key => $_handler) { if ($_handler instanceof $handler) { $handlers[$key] = $handler; $exists = true; break; } } // new handler should be added to the top of the list if (!$exists) { $handlers = array_merge([$handler], $handlers); } $this->setOption(self::SERVICE_HANDLERS, $handlers); } }