示例#1
0
 /**
  * Edit a service type
  * @param \ServiceType $serviceType service type to be altered
  * @param array $newValues new values to be applied to the service type
  * @param \User $user   user making the changes
  * @return \ServiceType the altered service type
  * @throws \Exception
  */
 public function editServiceType(\ServiceType $serviceType, $newValues, \User $user = null)
 {
     //Throws exception if user is not an administrator
     $this->checkUserIsAdmin($user);
     //Validate the values as per the GOCDB schema and check values are present and valid.
     $this->validate($newValues);
     //check the name is unique, if it has changed
     if ($newValues['Name'] != $serviceType->getName()) {
         if (!$this->serviceTypeNameIsUnique($newValues['Name'])) {
             throw new \Exception("Service type names must be unique, '" . $newValues['Name'] . "' is already in use");
         }
     }
     //Start transaction
     $this->em->getConnection()->beginTransaction();
     // suspend auto-commit
     try {
         //set name
         $serviceType->setName($newValues['Name']);
         //set description
         $serviceType->setDescription($newValues['Description']);
         $this->em->merge($serviceType);
         $this->em->flush();
         $this->em->getConnection()->commit();
     } catch (\Exception $e) {
         $this->em->getConnection()->rollback();
         $this->em->close();
         throw $e;
     }
     return $serviceType;
 }