/**
  * Creates Ticket and Message Reference fot it
  *
  * @param Message $message
  * @param $branchId
  * @param $reporter
  * @param $assigneeId
  * @param array|null $attachments
  * @return \Diamante\DeskBundle\Model\Ticket\Ticket
  * @throws \RuntimeException if unable to load required branch, reporter, assignee
  */
 public function createTicket(Message $message, $branchId, $reporter, $assigneeId, array $attachments = null)
 {
     $subject = $message->getSubject();
     if (empty($message->getSubject())) {
         $subject = self::EMPTY_SUBJECT_PLACEHOLDER;
     }
     $command = new CreateTicketCommand();
     $command->subject = $subject;
     $command->description = $message->getContent();
     $command->branch = $branchId;
     $command->reporter = $reporter;
     $command->assignee = $assigneeId;
     $command->source = Source::EMAIL;
     $command->attachmentsInput = $this->convertAttachments($attachments);
     $command->priority = Priority::PRIORITY_LOW;
     $command->status = Status::NEW_ONE;
     $ticket = $this->ticketService->createTicket($command);
     $this->createMessageReference($message->getMessageId(), $ticket, $message->getTo());
     return $ticket;
 }
 public function testProcessWhenMessageWithoutReferenceWithoutDefaultBranch()
 {
     $message = new Message(self::DUMMY_UNIQUE_ID, self::DUMMY_MESSAGE_ID, self::DUMMY_SUBJECT, self::DUMMY_CONTENT, $this->getDummyFrom(), self::DUMMY_MESSAGE_TO);
     $assigneeId = 1;
     $diamanteUser = $this->getReporter(1);
     $this->userService->expects($this->once())->method('getUserByEmail')->with($this->equalTo(self::DUMMY_MESSAGE_FROM))->will($this->returnValue($diamanteUser));
     $reporter = $this->getReporter($diamanteUser->getId());
     preg_match('/@(.*)/', self::DUMMY_MESSAGE_FROM, $output);
     $customerDomain = $output[1];
     $this->branchEmailConfigurationService->expects($this->once())->method('getConfigurationBySupportAddressAndCustomerDomain')->with($this->equalTo(self::DUMMY_MESSAGE_TO), $this->equalTo($customerDomain))->will($this->returnValue(self::DUMMY_BRANCH_ID));
     $this->branchEmailConfigurationService->expects($this->once())->method('getBranchDefaultAssignee')->with($this->equalTo(2))->will($this->returnValue(1));
     $this->messageReferenceService->expects($this->once())->method('createTicket')->with($this->equalTo($message->getMessageId()), self::DUMMY_BRANCH_ID, $message->getSubject(), $message->getContent(), $reporter, $assigneeId);
     $this->ticketStrategy->process($message);
 }
 /**
  * @param Message $message
  */
 public function process(Message $message)
 {
     $email = $message->getFrom()->getEmail();
     $diamanteUser = $this->diamanteUserRepository->findUserByEmail($email);
     $type = User::TYPE_DIAMANTE;
     if (is_null($diamanteUser)) {
         $sender = $message->getFrom();
         $diamanteUser = $this->diamanteUserFactory->create($email, $sender->getFirstName(), $sender->getLastName());
         $this->diamanteUserRepository->store($diamanteUser);
     }
     $reporterId = $diamanteUser->getId();
     $reporter = new User($reporterId, $type);
     $attachments = $message->getAttachments();
     if (!$message->getReference()) {
         $branchId = $this->getAppropriateBranch($message->getFrom()->getEmail(), $message->getTo());
         $assigneeId = $this->branchEmailConfigurationService->getBranchDefaultAssignee($branchId);
         $ticket = $this->messageReferenceService->createTicket($message->getMessageId(), $branchId, $message->getSubject(), $message->getContent(), $reporter, $assigneeId, $attachments);
     } else {
         $this->eventDispatcher->removeListener('commentWasAddedToTicket', array($this->ticketNotificationsSubscriber, 'processEvent'));
         $ticket = $this->messageReferenceService->createCommentForTicket($message->getContent(), $reporter, $message->getReference(), $attachments);
     }
     $this->processWatchers($message, $ticket);
 }
 public function testProcessWhenMessageWithoutReferenceWithoutDefaultBranch()
 {
     $message = new Message(self::DUMMY_UNIQUE_ID, self::DUMMY_MESSAGE_ID, self::DUMMY_SUBJECT, self::DUMMY_CONTENT, $this->getDummyFrom(), self::DUMMY_MESSAGE_TO);
     $assigneeId = 1;
     $diamanteUser = $this->getReporter(1);
     $this->userService->expects($this->once())->method('getUserByEmail')->with($this->equalTo(self::DUMMY_MESSAGE_FROM))->will($this->returnValue($diamanteUser));
     $this->configManager->expects($this->once())->method('get')->with($this->equalTo('diamante_desk.default_branch'))->will($this->returnValue(1));
     $this->branchService->expects($this->once())->method('getBranch')->with($this->equalTo(1))->will($this->returnValue($this->defaultBranch));
     $this->defaultBranch->expects($this->any())->method('getDefaultAssigneeId')->will($this->returnValue($assigneeId));
     $reporter = $this->getReporter($diamanteUser->getId());
     $this->messageReferenceService->expects($this->once())->method('createTicket')->with($this->equalTo($message->getMessageId()), self::DUMMY_BRANCH_ID, $message->getSubject(), $message->getContent(), $reporter, $assigneeId);
     $this->ticketStrategy->process($message);
 }