/** * Do the initialization logic * * @return void * @throws RpcExceptionInterface */ protected function doInitialize() { $persister = $this->manager->getUnitOfWork()->getEntityPersister($this->metadata->getName()); /** @var Collection $collection */ $collection = call_user_func_array([$persister, 'loadAll'], $this->searchArgs); if ($collection instanceof AbstractLazyCollection && $this->owner) { $collection = $persister->loadOneToManyCollection($this->association, $this->owner, $collection); } $this->collection = $collection; }
/** * Stores entity to cache * * @param mixed $data Entity source data * @param ApiMetadata $metadata * @param array $identifiers */ public function set($data, ApiMetadata $metadata, array $identifiers) { $item = $this->cache->getItem($this->getEntityKey($metadata, $identifiers)); $configuration = $this->manager->getConfiguration()->getCacheConfiguration($metadata->getName()); if (null === $configuration || $configuration['enabled'] === false) { $this->logger->debug(sprintf('Skipping entity cache for %s: not configured', $metadata->getName())); return; } $item->set($data); $item->expiresAfter($configuration['ttl']); $this->cache->save($item); $this->logger->debug(sprintf('Storing entity %s', $item->isHit() ? 'HIT' : 'MISS'), ['class' => $metadata->getName(), 'identifiers' => $identifiers]); }
/** * convert foreign identifiers into scalar foreign key values to avoid object to string conversion failures. * * @param ApiMetadata $class * @param array $id * * @return array */ public function flattenIdentifier(ApiMetadata $class, array $id) { $flatId = []; foreach ($class->getIdentifierFieldNames() as $field) { if ($class->hasAssociation($field) && array_key_exists($field, $id) && is_object($id[$field])) { /* @var EntityMetadata $targetClassMetadata */ $targetClassMetadata = $this->manager->getClassMetadata($class->getAssociationMapping($field)['target']); if ($this->unitOfWork->isInIdentityMap($id[$field])) { $associatedId = $this->flattenIdentifier($targetClassMetadata, $this->unitOfWork->getEntityIdentifier($id[$field])); } else { $associatedId = $this->flattenIdentifier($targetClassMetadata, $targetClassMetadata->getIdentifierValues($id[$field])); } $flatId[$field] = implode(' ', $associatedId); } else { $flatId[$field] = $id[$field]; } } return $flatId; }
/** {@inheritdoc} */ public function find(RpcClientInterface $client, ApiMetadata $metadata, array $identifiers) { $request = new RpcRequest($metadata->getMethodContainer()->getMethod('find'), $identifiers); $entityCache = $this->manager->getEntityCache(); if (null !== $entityCache) { $body = $entityCache->get($metadata->getName(), $identifiers); if (null !== $body) { return $body; } } $response = $client->invoke([$request])->getResponse($request); if (!$response->isSuccessful()) { return null; } $body = $response->getBody(); if (null !== $entityCache) { $entityCache->set($body, $metadata, $identifiers); } return $body; }