/**
  * Process the result of webhook procession
  *
  * @param TinypassWebhookResult $webhookResult
  *
  * @throws Exception
  */
 private function processWebhookResult(TinypassWebhookResult $webhookResult)
 {
     switch ($webhookResult->action()) {
         case TinypassWebhookResult::ACTION_UPDATE_CONTENT_KEY:
             $metaString = get_post_meta($webhookResult->id(), self::META_NAME, true);
             // Get content settings from post meta
             $contentSettings = TinypassContentSettings::fromArray($metaString);
             if ($contentSettings->chargeOption() != TinypassContentSettings::CHARGE_OPTION_ALGORITHMIC) {
                 throw new Exception(__('Algorithmic keying is not enabled for this post', 'tinypass'));
             }
             // Set the key attribute to resulted from webhook processing
             $contentSettings->algorithmicKeyed($webhookResult->key());
             // Save post meta
             add_post_meta($webhookResult->id(), self::META_NAME, $contentSettings->toArray(), true) or update_post_meta($webhookResult->id(), self::META_NAME, $contentSettings->toArray());
             header('HTTP/1.1 200 OK');
             exit(__('Keying rules updated', 'tinypass'));
             break;
     }
 }
 /**
  * Set the content to be keyed/unkeyed from algorithm
  *
  * @param int $id
  * @param string $action
  *
  * @return TinypassWebhookResult
  * @throws Exception
  */
 private function webhookAlgorithmicKey($id, $action)
 {
     // If algorithmic keyng is disabled - exit
     if (!$this->algorithmicKeyAvailable()) {
         throw new Exception(__('Algorithmic keying is not available for this site', 'tinypass'));
     }
     $key = null;
     // Check for valid keying action: allowed only "key" or "unkey"
     switch ($action) {
         case 'lock':
             $key = true;
             break;
         case 'unlock':
             $key = false;
             break;
         default:
             throw new Exception(__('Invalid action', 'tinypass'));
     }
     if (!$id) {
         throw new Exception(__('No id provided', 'tinypass'));
     }
     $result = new TinypassWebhookResult();
     // Set result of operation - update keying settings
     $result->action(TinypassWebhookResult::ACTION_UPDATE_CONTENT_KEY)->id($id)->key($key);
     return $result;
 }