Exemple #1
0
 /**
  * Изменение статуса кампании
  *
  * @param Campaign $campaign
  *
  * @Rest\Put("campaigns/{id}/status", requirements={"id"="\d+"})
  * @ParamConverter("campaign", class="VifeedCampaignBundle:Campaign")
  * @ApiDoc(
  *     section="Campaign API",
  *     requirements={
  *       {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="id кампании"}
  *     },
  *     statusCodes={
  *         200="Returned when successful",
  *         400="Returned when the something was wrong",
  *         403="Returned when the user is not authorized to use this method",
  *         404="Returned when campaign not found"
  *     }
  * )
  *
  * @return Response
  */
 public function putCampaignStatusAction(Campaign $campaign)
 {
     if ($campaign->getUser() != $this->getUser()) {
         throw new AccessDeniedHttpException('Можно изменять только свои кампании');
     }
     $oldStatus = $campaign->getStatus();
     $form = $this->createForm(new CampaignStatusType(), $campaign);
     $form->submit($this->get('request'), false);
     if (!$form->isValid()) {
         return $this->handleView(new View($form, 400));
     }
     $newStatus = $campaign->getStatus();
     if ($newStatus == Campaign::STATUS_ON) {
         // включение
         try {
             $this->campaignManager->tryTurnStatusOn($campaign, $oldStatus);
         } catch (CampaignStatusException $e) {
             $form['status']->addError(new FormError($e->getMessage()));
             return $this->handleView(new View($form, 400));
         }
     } else {
         // пауза или архив
         if ($oldStatus == Campaign::STATUS_ON) {
             $canTurnOffByTime = $this->campaignManager->canTurnOffByTime($campaign);
             if ($canTurnOffByTime !== true) {
                 $msg = 'Невозможно остановить кампанию в течение двух часов после запуска. Вы сможете остановить кампанию через ' . $canTurnOffByTime . ' мин.';
                 $form['status']->addError(new FormError($msg));
                 return $this->handleView(new View($form, 400));
             }
         }
         if ($newStatus == Campaign::STATUS_ARCHIVED) {
             $this->campaignManager->transferMoneyBackToUser($campaign);
         }
     }
     $this->campaignManager->save($campaign);
     $view = new View('', 200);
     return $this->handleView($view);
 }