Exemplo n.º 1
0
 /**
  * Delete user squad
  * @return ApiResponse
  */
 public function delete()
 {
     $squadId = $this->params('id', 0);
     $squadRepository = $this->getEntityManager()->getRepository('Frontend\\Squads\\Entity\\Squad');
     $userSquad = $squadRepository->findOneBy(array('user' => $this->getApiIdentity(), 'id' => $squadId));
     if (!$userSquad) {
         $errorResponse = new ApiResponse();
         if ($squadId <= 0) {
             $errorResponse->setStatusCode(400);
             $errorResponse->setErrorMessage('missing parameter id');
         } else {
             $errorResponse->setStatusCode(404);
             $errorResponse->setErrorMessage('squad not found');
         }
         return $errorResponse;
     }
     $this->getEntityManager()->remove($userSquad);
     $this->getEntityManager()->flush();
     return new ApiResponse(null, null, 200);
 }
 /**
  * On API Dispatch
  *
  * @param MvcEvent $e
  * @return ApiResponse|mixed
  * @throws \Zend\View\Exception\DomainException
  * @throws \Exception
  */
 public function onDispatch(MvcEvent $e)
 {
     $apiResponse = new ApiResponse();
     /** @var GenericHeader $apiRequestKey */
     if ($apiRequestKey = $this->requestApiKey()) {
         /** @var Key $key */
         $apiKeyRepository = $this->getEntityManager()->getRepository('Frontend\\Api\\Entity\\Key');
         if ($key = $apiKeyRepository->findOneBy(array('key' => $apiRequestKey))) {
             // check for limit reset
             $key->checkForRateReset();
             // show key limit usage
             $apiResponse->getHeaders()->addHeaders(array('X-RateLimit-Limit' => $key->getLimit(), 'X-RateLimit-Remaining' => $key->getRemainingRate(), 'X-RateLimit-Reset' => $key->getNextRateReset()->getTimestamp()));
             // check if key banned
             if (!$key->getStatus()) {
                 $apiResponse->setErrorMessage('API key banned');
                 $apiResponse->setStatusCode(403);
                 return $apiResponse;
             }
             // check key limit
             if ($key->isLimitExceeded()) {
                 $apiResponse->setErrorMessage('API limit exceeded');
                 $apiResponse->setStatusCode(429);
                 return $apiResponse;
             }
             // set the current api user
             $this->setApiIdentity($key->getUser());
             // all fine get work done
             $routeMatch = $e->getRouteMatch();
             if (!$routeMatch) {
                 throw new DomainException('Missing route matches; unsure how to retrieve action');
             }
             $action = $routeMatch->getParam('action', array('not-found'));
             $requestMethod = $_SERVER['REQUEST_METHOD'];
             if (is_string($action)) {
                 $apiResponse->setStatusCode(400);
                 switch ($action) {
                     case 'selectVersion':
                         $apiResponse->setErrorMessage('please specific api version /api/v[versionNumber]');
                         break;
                     case 'selectResource':
                         $apiResponse->setErrorMessage('no url resource path found');
                 }
                 return $apiResponse;
             }
             // fix for inject template listener
             $actionRequest = isset($action[$requestMethod]) ? $action[$requestMethod] : false;
             $routeMatch->setParam('action', $action);
             // add allow request methods
             $apiResponse->getHeaders()->addHeaders(array('Allow' => implode(',', array_keys($action)), 'Content-Type' => 'application/json'));
             if ($requestMethod == 'POST' || $requestMethod == 'PUT') {
                 $result = $this->validatePostData();
                 if ($result !== true) {
                     $apiResponse->setErrorMessage($result);
                     $apiResponse->setStatusCode(400);
                     return $apiResponse;
                 }
             }
             if (!$actionRequest || !method_exists($this, $actionRequest)) {
                 // invalid request
                 $apiResponse->setErrorMessage('method not supported');
                 $apiResponse->setStatusCode(501);
                 return $apiResponse;
             }
             /** @var ApiResponse $actionResponse */
             $actionResponse = $this->{$actionRequest}();
             if ($actionResponse instanceof ApiResponse) {
                 if (!$actionResponse->hasError()) {
                     // update successfully api request to key
                     $key->update();
                     $this->getEntityManager()->flush();
                 }
                 $actionResponse->getHeaders()->addHeaders($apiResponse->getHeaders());
                 return $actionResponse;
             } else {
                 throw new \Exception('Invalid API response');
             }
         } else {
             $apiResponse->setErrorMessage('api key invalid');
             $apiResponse->setStatusCode(403);
             return $apiResponse;
         }
     }
     // something is invalid with the key error
     $apiResponse->setErrorMessage('api key not found');
     $apiResponse->setStatusCode(401);
     return $apiResponse;
 }