示例#1
0
 /**
  * Ends a downtime
  * (Within the next 60 secs to avoid "Downtime can't be ended in the past" errors).
  * @param \Downtime $dt
  * @param \User $user User making the request
  * @throws \Exception
  */
 public function endDowntime(\Downtime $dt, \User $user = null)
 {
     //Check the portal is not in read only mode, throws exception if it is
     $this->checkPortalIsNotReadOnlyOrUserIsAdmin($user);
     $ses = $dt->getServices();
     $this->authorization($ses, $user);
     // Make sure all dates are treated as UTC!
     //date_default_timezone_set("UTC");
     if (!$dt->isOnGoing()) {
         throw new \Exception("Downtime isn't on-going.");
     }
     $sixtySecs = \DateInterval::createFromDateString('1 minutes');
     $now = new \DateTime(null, new \DateTimeZone('UTC'));
     $sixtySecsFromNow = $now->add($sixtySecs);
     //$this->validateDates($dt->getStartDate(), $end);
     // dt start date is in the future
     if ($dt->getStartDate() >= $sixtySecsFromNow) {
         throw new \Exception("Logic error - Downtime start time is after the requested end time");
     }
     // dt has already ended
     if ($dt->getEndDate() < $now) {
         throw new \Exception("Logic error - Downtime has already ended or will within the next 60 secs");
     }
     // ok. dt endDate is in the future and dt is ongoing/has started.
     $this->em->getConnection()->beginTransaction();
     try {
         $dt->setEndDate($sixtySecsFromNow);
         $this->em->merge($dt);
         $this->em->getConnection()->commit();
         $this->em->flush();
     } catch (\Exception $e) {
         $this->em->getConnection()->rollback();
         $this->em->close();
         throw $e;
     }
 }