safePersist() public method

Warning: On success you must NOT use the passed entity further in your application. Use the returned one instead!
public safePersist ( $entity ) : boolean | object
$entity
return boolean | object
示例#1
0
 /**
  * @param Url $url
  * @return Url
  * @throws UrlAlreadyExistsException
  */
 private function create(Url $url)
 {
     $url = $this->em->safePersist($url);
     if ($url === false) {
         throw new UrlAlreadyExistsException();
     }
     return $url;
 }
示例#2
0
 /**
  * Executes the current command.
  *
  * This method is not abstract because you can use this class
  * as a concrete class. In this case, instead of defining the
  * execute() method, you set the code to execute by passing
  * a Closure to the setCode() method.
  *
  * @param InputInterface $input An InputInterface instance
  * @param OutputInterface $output An OutputInterface instance
  *
  * @return null|int null or 0 if everything went fine, or an error code
  *
  * @throws LogicException When this abstract method is not implemented
  *
  * @see setCode()
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     /** @var QuestionHelper $helper */
     $helper = $this->getHelper('question');
     do {
         if (!isset($username)) {
             $username = $this->askUsername($helper, $input, $output);
         }
         if (!isset($password)) {
             $password = $this->askPassword($helper, $input, $output);
         }
         if (!isset($email)) {
             $email = $this->askEmail($helper, $input, $output);
         }
         $output->writeln(sprintf('Summary:
                 username: %s
                 password: %s
                 E-mail: %s', $username, $password, $email));
         // CONFIRMATION
         $question = new ConfirmationQuestion('Do you want to create this new User? ', true);
         if (!$helper->ask($input, $output, $question)) {
             $output->writeln('New user insertion has been CANCELED!');
             return;
         }
         try {
             $user = new User($username, $email, $password);
             $new_user = $this->em->safePersist($user);
             if ($new_user === false) {
                 $uname = $this->userRepository->findOneBy(['username' => $username]);
                 if ($uname !== null) {
                     $output->writeln(sprintf('User with username "%s" already exists', $username));
                     $username = null;
                 }
                 $umail = $this->userRepository->findOneBy(['email' => $email]);
                 if ($umail !== null) {
                     $output->writeln(sprintf('User with email "%s" already exists', $email));
                     $email = null;
                 }
                 unset($user);
                 continue;
             }
             $output->writeln('Your new User has been SUCCESSFULLY created!');
             $continue = new ConfirmationQuestion('Would you like to create another one?', true);
             if ($helper->ask($input, $output, $continue)) {
                 $username = null;
                 $password = null;
                 $email = null;
                 continue;
             }
             return 0;
         } catch (\Exception $e) {
             $output->writeLn("That's bad. An Error occurred: <error>{$e->getMessage()}</error>");
             return 1;
         }
     } while (true);
 }
示例#3
0
 /**
  * Executes the current command.
  *
  * This method is not abstract because you can use this class
  * as a concrete class. In this case, instead of defining the
  * execute() method, you set the code to execute by passing
  * a Closure to the setCode() method.
  *
  * @param InputInterface $input An InputInterface instance
  * @param OutputInterface $output An OutputInterface instance
  *
  * @return null|int null or 0 if everything went fine, or an error code
  *
  * @throws LogicException When this abstract method is not implemented
  *
  * @see setCode()
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     /** @var QuestionHelper $helper */
     $helper = $this->getHelper('question');
     do {
         if (!isset($urlPath)) {
             $urlPath = $this->askUrlPath($helper, $input, $output);
         }
         if (!isset($presenter)) {
             $presenter = $this->askPresenter($helper, $input, $output);
         }
         if (!isset($internalID)) {
             $internalID = null;
             $internalIDConfirmation = new ConfirmationQuestion('Would you like to set Internal ID?', true);
             if ($helper->ask($input, $output, $internalIDConfirmation)) {
                 $internalID = $this->askInternalID($helper, $input, $output);
             }
         }
         $output->writeln(sprintf('Summary:
                  Url path: %s
                  Presenter: %s
                  internal ID: %s', $urlPath, $presenter, $internalID === null ? 'null' : $internalID));
         $anotherUrl = new ConfirmationQuestion('Do you want to save your Url?', true);
         if (!$helper->ask($input, $output, $anotherUrl)) {
             return;
         }
         try {
             $url = UrlGenerator::create($urlPath, $presenter, null, $internalID);
             $url = $this->em->safePersist($url);
             if ($url === false) {
                 $output->writeln(sprintf('Somebody has recently created an Url with path "%s".', $urlPath));
                 $changeQuestion = new ConfirmationQuestion('Do you want to change that Url path?', true);
                 if ($helper->ask($input, $output, $changeQuestion)) {
                     $urlPath = null;
                     continue;
                 }
                 $output->writeln(sprintf('Your Url couldn\'t have been saved because url with path "%s" already exists.', $urlPath));
                 return 1;
             }
             $output->writeln('New Url has been SUCCESSFULLY created!');
             $continueQuestion = new ConfirmationQuestion('Would you like to create another one?', true);
             if ($helper->ask($input, $output, $continueQuestion)) {
                 $urlPath = null;
                 $presenter = null;
                 $internalID = null;
                 continue;
             }
             return 0;
         } catch (\Exception $e) {
             $output->writeLn("That's bad. An Error occurred: <error>{$e->getMessage()}</error>");
             return -1;
         }
     } while (true);
 }
示例#4
0
 /**
  * @param array $values
  * @param Tag|null $tag
  * @return Tag
  * @throws TagNameAlreadyExistsException
  * @throws UrlAlreadyExistsException
  * @throws \Doctrine\DBAL\DBALException
  * @throws \Exception
  */
 private function create(array $values, Tag $tag = null)
 {
     $this->em->beginTransaction();
     if ($tag === null) {
         $tag = new Tag($values['name'], $values['color']);
     }
     $this->fillTag($values, $tag);
     /** @var Tag $tag */
     $tag = $this->em->safePersist($tag);
     if ($tag === false) {
         throw new TagNameAlreadyExistsException();
     }
     $this->createUrl($tag);
     $this->em->commit();
     return $tag;
 }
示例#5
0
 /**
  * @param array $values
  * @param Page|null $page
  * @return Page
  * @throws UrlAlreadyExistsException
  * @throws LocaleNotFoundException
  * @throws PageTitleAlreadyExistsException
  * @throws PageIntroHtmlLengthException
  */
 private function createNewPage(array $values, Page $page = null)
 {
     $this->em->beginTransaction();
     $url = $this->establishPageUrl($values['title'], $values['url']);
     $url = $this->urlFacade->saveUrl($url);
     // still needs internalID to be set! (next in code)
     $locale = $this->localeFacade->getByName($values['lang']);
     if ($locale === null) {
         throw new LocaleNotFoundException();
     }
     if ($page === null) {
         $page = new Page($values['title'], $values['intro'], $values['text'], $url, $values['author'], $locale);
     }
     $this->fillPageEntity($values, $page);
     /** @var Page $page */
     $page = $this->em->safePersist($page);
     if ($page === false) {
         throw new PageTitleAlreadyExistsException();
     }
     $url->setInternalId($page->getId());
     $this->em->persist($url);
     $this->addTags2Page($values['tags'], $page);
     $this->em->persist($page);
     $this->em->flush();
     $this->em->commit();
     return $page;
 }
示例#6
0
 /**
  * @param array $values
  * @return Role
  * @throws RoleAlreadyExistsException
  * @throws RoleMissingException
  */
 public function createRole(array $values)
 {
     $parentRole = null;
     if ($values['parent'] !== null) {
         $parentRole = $this->em->find(Role::class, $values['parent']);
         if ($parentRole === null) {
             throw new RoleMissingException();
         }
     }
     $role = new Role($values['name'], $parentRole);
     $role = $this->em->safePersist($role);
     if ($role === false) {
         throw new RoleAlreadyExistsException();
     }
     $this->onSuccessRoleCreation($role);
     return $role;
 }
 /**
  * @param User $newUser
  * @param Invitation $invitation
  * @return User
  * @throws DuplicateUsernameException
  * @throws DuplicateEmailException
  * @throws InvalidUserInvitationEmailException
  */
 public function registerUser(User $newUser, Invitation $invitation)
 {
     if ($newUser->email !== $invitation->email) {
         throw new InvalidUserInvitationEmailException();
     }
     $this->em->beginTransaction();
     $user = $this->em->safePersist($newUser);
     if ($user === false) {
         $this->em->rollback();
         // e.g. when two users are trying to register
         // at the same time on the same Invitation
         if ($this->usersReader->isEmailRegistered($newUser->email)) {
             $this->invitationsWriter->removeInvitation($invitation);
             throw new DuplicateEmailException();
         }
         if ($this->usersReader->isUsernameRegistered($newUser->username)) {
             throw new DuplicateUsernameException();
         }
     }
     $this->invitationsWriter->removeInvitation($invitation);
     $this->em->commit();
     return $user;
 }
示例#8
0
 /**
  * @param array $values
  * @param User|null $user
  * @return ValidationObject
  */
 private function create(array $values, User $user = null)
 {
     if ($user === null) {
         $user = new User($values['username'], $values['email'], $values['password']);
     }
     $this->em->beginTransaction();
     $user->setUsername($values['username']);
     $user->setEmail($values['email']);
     $user->setPassword($values['password']);
     $user->setFirstName($values['first_name']);
     $user->setLastName($values['last_name']);
     $validationObject = new ValidationObject();
     $role = $this->getRole($values['role'], $validationObject);
     if (!$validationObject->isValid()) {
         $this->em->rollback();
         return $validationObject;
     }
     $user->addRole($role);
     $newUser = $this->em->safePersist($user);
     if ($newUser === false) {
         // username or email already exists
         if ($this->usernameExists($values['username'])) {
             $validationObject->addError('users.user.form.messages.usernameExists', FlashMessage::WARNING);
         }
         if ($this->emailExists($values['email'])) {
             $validationObject->addError('users.user.form.messages.emailExists', FlashMessage::WARNING);
         }
     }
     if ($validationObject->isValid()) {
         $this->onSuccessUserCreation($user);
         $this->em->commit();
     } else {
         $this->em->rollback();
     }
     return $validationObject;
 }