client = $client; $this->config = $config; } /** * {@inheritdoc} */ public function doWrite($id, $data, $isDataChanged) { // Prepare the attributes $expires = time() + $this->config->get('session_lifetime'); $attributes = array( 'expires' => array( 'Value' => array( 'N' => (string) $expires ) ) ); if ($isDataChanged) { if ($data != '') { $attributes['data'] = array( 'Value' => array( 'S' => $data ) ); } else { $attributes['data'] = array( 'Action' => 'DELETE' ); } } $attributes = array_merge($attributes, $this->getExtraAttributes()); // Perform the UpdateItem command try { return (bool) $this->client->getCommand('UpdateItem', array( 'TableName' => $this->config->get('table_name'), 'Key' => $this->formatKey($id), 'AttributeUpdates' => $attributes, Ua::OPTION => Ua::SESSION ))->execute(); } catch (DynamoDbException $e) { return false; } } /** * {@inheritdoc} */ public function doDestroy($id) { try { return (bool) $this->client->getCommand('DeleteItem', array( 'TableName' => $this->config->get('table_name'), 'Key' => $this->formatKey($id), Ua::OPTION => Ua::SESSION ))->execute(); } catch (DynamoDbException $e) { return false; } } /** * Generates the correct key structure based on the key value and DynamoDB API version * * @param string $keyValue The value of the key (i.e., the session ID) * * @return array formatted key structure */ protected function formatKey($keyValue) { $keyName = ($this->client->getApiVersion() < '2012-08-10') ? 'HashKeyElement' : $this->config->get('hash_key'); return array($keyName => array('S' => $keyValue)); } /** * Allows the specific strategy to add additional attributes to update * * @return array */ abstract protected function getExtraAttributes(); }