/** * Content callback for the site actions. * * @param string $publicKey * An optional public key to get, update, or delete * @param bool $delete * Indicates if this should be a delete command. * @return \Symfony\Component\HttpFoundation\Response * A valid HTTP response. */ public function site($publicKey = NULL, $delete = FALSE) { $data = $this->parser->getRestParameters(); $sites = $this->state()->get(self::KEY_SITES) ?: array(); if (!empty($publicKey)) { // Validate authentication. if (!$this->parser->validateOAuth($sites)) { return $this->getErrorResponse(DrupalClient::AUTH_ERROR); } // Check whether publicKey exists. if (!isset($sites[$publicKey])) { return $this->getErrorResponse(Response::HTTP_NOT_FOUND); } } if (\Drupal::request()->getMethod() == 'GET') { // Return existing site. if (isset($publicKey)) { $response = $sites[$publicKey]; } else { $response = array('list' => array_values($sites), 'listCount' => count($sites), 'listOffset' => 0, 'listTotal' => count($sites)); return $this->getSuccessResponse($response); } } else { // Update site. if (!empty($publicKey) && !$delete) { $sites[$publicKey] = $data + $sites[$publicKey]; $this->state()->set(self::KEY_SITES, $sites); $response = $sites[$publicKey]; } elseif (!$delete) { $data['publicKey'] = $publicKey = md5(rand() . REQUEST_TIME); $data['privateKey'] = $privateKey = md5(rand() . REQUEST_TIME); // Apply default values. $data += array('url' => '', 'email' => '', 'expectedLanguages' => array(), 'subscriptionType' => ''); $sites[$publicKey] = $data; $this->state()->set(self::KEY_SITES, $sites); $response = $data; } else { unset($sites[$publicKey]); $this->state()->set(self::KEY_SITES, $sites); return TRUE; } } return $this->getSuccessResponse(array('site' => $response)); }
/** * Delete an existing site. * @param $publicKey * @return bool|\Symfony\Component\HttpFoundation\Response */ public function deleteSite($publicKey) { $sites = $this->state()->get(self::KEY_SITES) ?: array(); // Validate authentication. if (!$this->parser->validateOAuth($sites)) { return $this->getErrorResponse(DrupalClient::AUTH_ERROR); } // Check whether publicKey exists. if (!isset($sites[$publicKey])) { return $this->getErrorResponse(Response::HTTP_NOT_FOUND); } unset($sites[$publicKey]); $this->state()->set(self::KEY_SITES, $sites); return $this->getSuccessResponse(); }