/** * @param $cart * @param ApiResponse $response * @return \RestApiBundle\Entity\Cart */ public function add($cart, ApiResponse $response) { $cart = $this->em->getRepository('RestApiBundle:Cart')->add($cart); $response->setStatusCode(Response::HTTP_OK); $response->setId($cart->getItemId()); $response->addHeader('Location', '/path_to_cart/' . $cart->getItemId()); return $cart; }
public function testGetResponseErrorCase() { $apiResponse = new ApiResponse(); $apiResponse->setError('error1'); $apiResponse->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR); $response = $apiResponse->getResponse(); $this->assertSame('{"error":"error1"}', $response->getContent()); $this->assertSame(Response::HTTP_INTERNAL_SERVER_ERROR, $response->getStatusCode()); }
/** * @param GetResponseForExceptionEvent $event * @throws \Exception */ public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); $message = sprintf('Exception: %s with code: %s', $exception->getMessage(), $exception->getCode()); $response = new ApiResponse(); $response->setError($message); $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR); $event->setResponse($response->getResponse()); }
/** * @param Item $item * @param ApiResponse $response * @return Item|null */ public function saveItem(Item $item, ApiResponse $response) { if (null === $item->getId()) { $item = $this->em->getRepository('RestApiBundle:Item')->add($item); $response->setStatusCode(Response::HTTP_CREATED); } else { $item = $this->em->getRepository('RestApiBundle:Item')->update($item); $response->setStatusCode(Response::HTTP_OK); } $response->setId($item->getId()); return $item; }