/**
  * 주어진 이메일을 인증처리 한다.
  * 주어진 등록 대기 이메일의 인증코드가 주어진 인증코드와 동일하면 해당 이메일을 해당회원의 실제 이메일로 등록하고, 본 등록대기 이메일은 삭제한다.
  *
  * @param string $email 인증할 이메일
  * @param string $code  인증코드
  *
  * @return bool 주어진 이메일의 인증처리가 성공하면 true를 반환한다.
  * @throws \Exception
  */
 public function confirmEmail($email, $code)
 {
     $pendingMail = $this->pendingMails->findByAddress($email);
     if ($pendingMail === null) {
         throw new PendingEmailNotExistsException();
     }
     if ($this->validateConfirmCode($pendingMail, $code) === false) {
         throw new InvalidConfirmationCodeException();
     }
     $info = ['address' => $pendingMail->getAddress(), 'memberId' => $pendingMail->getMemberId()];
     $mail = new MailEntity($info);
     try {
         $this->mails->insert($mail);
         $this->pendingMails->delete($pendingMail);
     } catch (\Exception $e) {
         throw $e;
     }
     return true;
 }
 public function addMail(Request $request)
 {
     $input = $request->only('address');
     // validation
     $validate = \Validator::make($request->all(), ['address' => 'email|required']);
     if ($validate->fails()) {
         $e = new InvalidArgumentException();
         $e->setMessage('이메일 형식이 잘못되었습니다.');
         throw $e;
     }
     // 이미 인증 요청중인 이메일이 있는지 확인한다.
     $useEmailConfirm = $this->handler->usingEmailConfirm();
     if ($useEmailConfirm) {
         if ($this->member->getPendingEmail() !== null) {
             throw new AlreadyPendingEmailExistsException();
         }
     }
     // 이미 존재하는 이메일이 있는지 확인한다.
     $exists = $this->mails->findByAddress($input['address']);
     if ($exists !== null) {
         throw new MailAlreadyExistsException();
     }
     array_set($input, 'memberId', $this->member->getId());
     XeDB::beginTransaction();
     try {
         if ($useEmailConfirm) {
             $mail = new PendingMailEntity($input);
             $mail = $this->pendingMails->insert($mail);
             /** @var EmailBroker $broker */
             $broker = app('xe.auth.email');
             $broker->sendEmailForConfirmation($mail);
         } else {
             $mail = new MailEntity($input);
             $mail = $this->mails->insert($mail);
         }
     } catch (\Exception $e) {
         XeDB::rollback();
         throw $e;
     }
     XeDB::commit();
     \Session::flash('alert', ['type' => 'success', 'message' => '추가되었습니다.']);
     return Presenter::makeApi(['message' => '추가되었습니다']);
 }