public function fetch($hash, $className) { $metadata = $this->metadataManager->loadByClassname($className); $data = null; if ($this->storageAdapter->contains($hash, $metadata->getContext())) { $data = $this->storageAdapter->fetch($hash, $metadata->getContext()); } // dispatch event $event = new DatabaseFetchEvent($hash, $data, $className, $metadata); $this->eventDispatcher->dispatch(DatabaseEvent::FETCH_EVENT, $event); // possibility to cancel store in a event-handler if ($event->isCanceled()) { return; } // possibility to change data in the event-handler $data = $event->getData(); if (!$data) { throw new \Exception('Object not found'); } if ($data['class'] !== $className) { throw new \Exception('Classname not match!'); } $model = $this->getModel($data['class']); $this->accessor->setValue($model, 'hash', $hash); if ($model instanceof DistributedModelInterface) { $this->accessor->setValue($model, 'policyCollection', unserialize($data['policies'])); } $this->serializer->deserialize($model, $data['data'], array_merge($metadata->getDataFields(), $metadata->getMetadataFields()), $this); return $model; }
public function fetch($hash, $class, $username) { $classMetadata = $this->metadataManager->loadByClassname($class); if (!$this->storageAdapter->contains($hash, $classMetadata->getContext())) { throw new ObjectNotFoundException($hash); } $data = $this->storageAdapter->fetch($hash, $classMetadata->getContext()); if (array_key_exists('type', $data) && $data['type'] === 'backup') { /** @var PolicyCollectionInterface $policyCollection */ $policyCollection = unserialize($data['policies']); /** @var ReplicatorPolicy $policy */ $policy = $policyCollection->get(self::POLICY_NAME); throw new NotPrimaryServerException($policy->getPrimaryServer()); } // TODO security-checker with given username ("%s::%s", server, username) return $data; }
/** * @dataProvider adapterProvider * @expectedException \Exception * * @param StorageAdapterInterface $storageAdapter * @param string $hash * @param array $data * @param string $context */ public function testFetchNotExists(StorageAdapterInterface $storageAdapter, $hash, $data, $context) { $storageAdapter->fetch($hash, $context); }