Пример #1
0
 public function delete($id)
 {
     if (!$this instanceof AbstractEntityController) {
         throw new InternalErrorException("This trait must be used in children of the AbstractEntityController.");
     }
     $this->checkPermissions($id, __FUNCTION__);
     try {
         $this->getEntityManager()->beginTransaction();
         $entity = $this->retrieveEntity($this->getEntityClass(), $id);
         if (!$entity instanceof DeletableInterface) {
             throw new InternalErrorException("Only entities which implement the DeletableInterface can be deleted here.");
         }
         if ($entity->isDeleted()) {
             throw new BadRequestException(Translate::t("This entity is already deleted."));
         }
         $entity->setDeleted(true);
         $this->getEntityManager()->persist($entity);
         $this->getEntityManager()->flush();
         $this->getLogger()->log(LogLevel::INFO, "agit.api.entity", sprintf(Translate::tl("Object “%s” of type “%s” has been deleted."), $entity->getId(), $this->getEntityClassName($entity)), true);
         $this->getEntityManager()->commit();
     } catch (Exception $e) {
         $this->getEntityManager()->rollBack();
         throw $e;
     }
     return true;
 }
Пример #2
0
 public function remove($id)
 {
     if (!$this instanceof AbstractEntityController) {
         throw new InternalErrorException("This trait must be used in children of the AbstractEntityController.");
     }
     $this->checkPermissions($id, __FUNCTION__);
     $entity = $this->retrieveEntity($this->getEntityClass(), $id);
     try {
         $this->getEntityManager()->beginTransaction();
         $this->getEntityManager()->remove($entity);
         $this->getEntityManager()->flush();
         $this->getLogger()->log(LogLevel::INFO, "agit.api.entity", sprintf(Translate::tl("Object %s of type %s has been removed permanently."), $entity->getId(), $this->getEntityClassName($entity)), true);
         $this->getEntityManager()->commit();
     } catch (Exception $e) {
         $this->getEntityManager()->rollBack();
         throw new BadRequestException(Translate::t("This object cannot be removed, because there are other objects depending on it."));
     }
 }
Пример #3
0
 public function create(AbstractEntityObject $requestObject)
 {
     if (!$this instanceof AbstractEntityController) {
         throw new InternalErrorException("This trait must be used in children of the AbstractEntityController.");
     }
     $this->checkPermissions($requestObject, __FUNCTION__);
     $this->validate($requestObject);
     try {
         $this->getEntityManager()->beginTransaction();
         $className = $this->getEntityManager()->getClassMetadata($this->getEntityClass())->getName();
         $entity = $this->saveEntity(new $className(), $requestObject);
         $this->getLogger()->log(LogLevel::INFO, "agit.api.entity", sprintf(Translate::tl("New object %s of type %s has been created."), $entity->getId(), $this->getEntityClassName($entity)), true);
         $this->getEntityManager()->commit();
     } catch (Exception $e) {
         $this->getEntityManager()->rollBack();
         throw $e;
     }
     return $this->createObject($this->getResponseObjectApiClass(), $entity);
 }
Пример #4
0
 public function saveSettings(array $settings, $force = false)
 {
     $this->loadSettings();
     $changedSettings = [];
     $changedSettingNames = [];
     try {
         $this->entityManager->beginTransaction();
         foreach ($settings as $id => $value) {
             if (!isset($this->settings[$id])) {
                 throw new SettingNotFoundException(sprintf(Translate::t("A setting `%s` does not exist."), $id));
             }
             $setting = $this->settings[$id];
             if (!$force && $setting->isReadonly()) {
                 throw new SettingReadonlyException(sprintf("Setting `%s` is read-only.", $id));
             }
             try {
                 $oldValue = $setting->getValue();
                 $setting->setValue($value);
                 // implicitely validates
                 $this->entities[$id]->setValue($value);
                 if ($oldValue !== $value) {
                     $changedSettings[$id] = ["old" => $oldValue, "new" => $value];
                     $changedSettingNames[] = $setting->getName();
                 }
             } catch (Exception $e) {
                 throw new InvalidSettingValueException(sprintf(Translate::t("Invalid value for “%s”: %s"), $setting->getName(), $e->getMessage()));
             }
             $this->entityManager->persist($this->entities[$id]);
         }
         $this->entityManager->flush();
         if ($this->logger && count($changedSettings)) {
             $this->logger->log(LogLevel::INFO, "agit.settings", sprintf(Translate::tl("The following settings have been changed: %s"), implode(", ", $changedSettingNames)), true);
         }
         $this->entityManager->commit();
     } catch (Exception $e) {
         $this->entityManager->rollBack();
         throw $e;
     }
     if ($changedSettings) {
         $this->eventDispatcher->dispatch("agit.settings.modified", new SettingsModifiedEvent($this, $changedSettings));
         $this->eventDispatcher->dispatch("agit.settings.loaded", new SettingsLoadedEvent($this));
     }
 }
Пример #5
0
 public function onPaymentFailed(PaymentFailedEvent $event)
 {
     $message = $event->getMessage();
     $context = $event->getMessageContext();
     $extra = $event->getExtraInfo();
     $this->logger->log(LogLevel::WARNING, "agit.payment", vsprintf(Translate::tl($message), $context) . ($extra ? " – [{$extra}]" : ""), $this->userService->getCurrentUser());
     $this->addOrderMessage($event->getPayment()->getOrder(), OrderMessage::TYPE_ERROR, $message, $context);
 }