Google\Cloud\Datastore\DatastoreSessionHandler::read PHP Method

read() public method

Read the session data from Cloud Datastore.
public read ( $id )
    public function read($id)
    {
        try {
            $key = $this->datastore->key($this->kind, $id, ['namespaceId' => $this->namespaceId]);
            $entity = $this->transaction->lookup($key);
            if ($entity !== null && isset($entity['data'])) {
                return $entity['data'];
            }
        } catch (Exception $e) {
            trigger_error(sprintf('Datastore lookup failed: %s', $e->getMessage()), E_USER_WARNING);
        }
        return '';
    }

Usage Example

 public function testReadEntity()
 {
     $key = new Key('projectid');
     $key->pathElement(self::KIND, 'sessionid');
     $entity = new Entity($key, ['data' => 'sessiondata']);
     $this->transaction->lookup($key)->shouldBeCalledTimes(1)->willReturn($entity);
     $this->datastore->transaction()->shouldBeCalledTimes(1)->willReturn($this->transaction->reveal());
     $this->datastore->key(self::KIND, 'sessionid', ['namespaceId' => self::NAMESPACE_ID])->shouldBeCalledTimes(1)->willReturn($key);
     $datastoreSessionHandler = new DatastoreSessionHandler($this->datastore->reveal());
     $datastoreSessionHandler->open(self::NAMESPACE_ID, self::KIND);
     $ret = $datastoreSessionHandler->read('sessionid');
     $this->assertEquals('sessiondata', $ret);
 }