self::RECORD_TREE_SERVICE_NAME, ]; private $data = []; private $serviceFactory; private $metadata; public function __construct(ServiceFactory $serviceFactory, Metadata $metadata) { $this->serviceFactory = $serviceFactory; $this->metadata = $metadata; } public function get(string $entityType): Service { if (!array_key_exists($entityType, $this->data)) { $this->load($entityType); } return $this->data[$entityType]; } private function load(string $entityType): void { if (!$this->metadata->get(['scopes', $entityType, 'entity'])) { throw new Error("Can't create record service '{$entityType}', there's no such entity type."); } if ($this->serviceFactory->checkExists($entityType)) { $service = $this->serviceFactory->create($entityType); if (!$service instanceof Service) { $this->loadDefault($entityType); return; } $this->data[$entityType] = $service; return; } $this->loadDefault($entityType); } private function loadDefault(string $entityType): void { $default = self::RECORD_SERVICE_NAME; $type = $this->metadata->get(['scopes', $entityType, 'type']); if ($type) { $default = $this->defaultTypeMap[$type] ?? $default; } $obj = $this->serviceFactory->create($default); $obj->setEntityType($entityType); $this->data[$entityType] = $obj; } }