/**
  * Creates Ticket and Message Reference fot it
  *
  * @param $messageId
  * @param $branchId
  * @param $subject
  * @param $description
  * @param $reporter
  * @param $assigneeId
  * @param array $attachments
  * @return \Diamante\DeskBundle\Model\Ticket\Ticket
  * @throws \RuntimeException if unable to load required branch, reporter, assignee
  */
 public function createTicket($messageId, $branchId, $subject, $description, $reporter, $assigneeId, array $attachments = null)
 {
     if (empty($subject)) {
         $subject = self::EMPTY_SUBJECT_PLACEHOLDER;
     }
     $this->ticketBuilder->setSubject($subject)->setDescription($description)->setBranchId($branchId)->setReporter($reporter)->setAssigneeId($assigneeId)->setSource(Source::EMAIL);
     $ticket = $this->ticketBuilder->build();
     if ($attachments) {
         $this->createAttachments($attachments, $ticket);
     }
     $this->ticketRepository->store($ticket);
     $this->createMessageReference($messageId, $ticket);
     $this->em->detach($ticket);
     $this->dispatchEvents($ticket);
     return $ticket;
 }
 /**
  * Create Ticket
  * @param Command\CreateTicketCommand $command
  * @return Ticket
  *
  * @throws ForbiddenException
  */
 public function createTicket(Command\CreateTicketCommand $command)
 {
     $this->isGranted('CREATE', 'Entity:DiamanteDeskBundle:Ticket');
     \Assert\that($command->attachmentsInput)->nullOr()->all()->isInstanceOf('Diamante\\DeskBundle\\Api\\Dto\\AttachmentInput');
     $this->ticketBuilder->setSubject($command->subject)->setDescription($command->description)->setBranchId($command->branch)->setReporter($command->reporter)->setAssignee($command->assignee)->setPriority($command->priority)->setSource($command->source)->setStatus($command->status)->setTags($command->tags);
     $ticket = $this->ticketBuilder->build();
     $this->createAttachments($command, $ticket);
     $this->doctrineRegistry->getManager()->persist($ticket);
     $this->doctrineRegistry->getManager()->flush();
     if ($this->loggedUser instanceof OroUser) {
         $this->tagManager->saveTagging($ticket);
         $ticket->setTags(null);
         $this->loadTagging($ticket);
     }
     $this->dispatchWorkflowEvent($this->doctrineRegistry, $this->dispatcher, $ticket);
     return $ticket;
 }
 /**
  * @test
  */
 public function thatTicketCreatesWithAttachments()
 {
     $branchId = 1;
     $assigneeId = 3;
     $reporter = $this->createReporter();
     $this->ticketBuilder->expects($this->once())->method('setSubject')->with(self::SUBJECT)->will($this->returnValue($this->ticketBuilder));
     $this->ticketBuilder->expects($this->once())->method('setDescription')->with(self::DESCRIPTION)->will($this->returnValue($this->ticketBuilder));
     $this->ticketBuilder->expects($this->once())->method('setBranchId')->with($branchId)->will($this->returnValue($this->ticketBuilder));
     $this->ticketBuilder->expects($this->once())->method('setReporter')->with((string) $reporter)->will($this->returnValue($this->ticketBuilder));
     $this->ticketBuilder->expects($this->once())->method('setAssigneeId')->with($assigneeId)->will($this->returnValue($this->ticketBuilder));
     $this->ticketBuilder->expects($this->once())->method('setSource')->with(Source::EMAIL)->will($this->returnValue($this->ticketBuilder));
     $this->ticketBuilder->expects($this->once())->method('build')->will($this->returnValue($this->ticket));
     $this->attachmentManager->expects($this->once())->method('createNewAttachment')->with($this->equalTo(self::DUMMY_FILENAME), $this->equalTo(self::DUMMY_FILE_CONTENT), $this->equalTo($this->ticket));
     $this->ticketRepository->expects($this->once())->method('store')->with($this->equalTo($this->ticket));
     $messageReference = new MessageReference(self::DUMMY_MESSAGE_ID, $this->ticket);
     $this->messageReferenceRepository->expects($this->once())->method('store')->with($this->equalTo($messageReference));
     $this->ticket->expects($this->atLeastOnce())->method('getRecordedEvents')->will($this->returnValue(array()));
     $this->messageReferenceService->createTicket(self::DUMMY_MESSAGE_ID, $branchId, self::SUBJECT, self::DESCRIPTION, $reporter, $assigneeId, $this->attachments());
 }
 /**
  * Create Ticket
  * @param CreateTicketCommand $command
  * @return \Diamante\DeskBundle\Model\Ticket\Ticket
  * @throws \RuntimeException if unable to load required branch, reporter, assignee
  */
 public function createTicket(CreateTicketCommand $command)
 {
     $this->isGranted('CREATE', 'Entity:DiamanteDeskBundle:Ticket');
     \Assert\that($command->attachmentsInput)->nullOr()->all()->isInstanceOf('Diamante\\DeskBundle\\Api\\Dto\\AttachmentInput');
     $this->ticketBuilder->setSubject($command->subject)->setDescription($command->description)->setBranchId($command->branch)->setReporter($command->reporter)->setAssigneeId($command->assignee)->setPriority($command->priority)->setSource($command->source)->setStatus($command->status)->setTags($command->tags);
     $ticket = $this->ticketBuilder->build();
     if (is_array($command->attachmentsInput) && false === empty($command->attachmentsInput)) {
         foreach ($command->attachmentsInput as $each) {
             $this->attachmentManager->createNewAttachment($each->getFilename(), $each->getContent(), $ticket);
         }
     }
     $this->ticketRepository->store($ticket);
     if ($this->securityFacade->getOrganization()) {
         $this->tagManager->saveTagging($ticket);
     }
     $this->em->detach($ticket);
     $this->dispatchEvents($ticket);
     return $ticket;
 }
 /**
  * @test
  */
 public function thatTicketCreatesWithDefaultStatusAndAttachments()
 {
     $branchId = 1;
     $reporterId = 2;
     $assigneeId = 3;
     $status = Status::NEW_ONE;
     $priority = Priority::PRIORITY_LOW;
     $source = Source::PHONE;
     $number = new TicketSequenceNumber(null);
     $reporter = $this->createReporter($reporterId);
     $ticket = new Ticket(UniqueId::generate(), $number, self::SUBJECT, self::DESCRIPTION, $this->createBranch(), $reporter, $this->createAssignee(), new Source($source), new Priority($priority), new Status($status));
     $this->ticketBuilder->expects($this->once())->method('setSubject')->with(self::SUBJECT)->will($this->returnValue($this->ticketBuilder));
     $this->ticketBuilder->expects($this->once())->method('setDescription')->with(self::DESCRIPTION)->will($this->returnValue($this->ticketBuilder));
     $this->ticketBuilder->expects($this->once())->method('setBranchId')->with($branchId)->will($this->returnValue($this->ticketBuilder));
     $this->ticketBuilder->expects($this->once())->method('setReporter')->with((string) $reporter)->will($this->returnValue($this->ticketBuilder));
     $this->ticketBuilder->expects($this->once())->method('setAssigneeId')->with($assigneeId)->will($this->returnValue($this->ticketBuilder));
     $this->ticketBuilder->expects($this->once())->method('setSource')->with($source)->will($this->returnValue($this->ticketBuilder));
     $this->ticketBuilder->expects($this->once())->method('setPriority')->with($priority)->will($this->returnValue($this->ticketBuilder));
     $this->ticketBuilder->expects($this->once())->method('setStatus')->with(null)->will($this->returnValue($this->ticketBuilder));
     $this->ticketBuilder->expects($this->once())->method('build')->will($this->returnValue($ticket));
     $attachmentInputs = $this->attachmentInputs();
     $this->attachmentManager->expects($this->exactly(count($attachmentInputs)))->method('createNewAttachment')->with($this->isType(\PHPUnit_Framework_Constraint_IsType::TYPE_STRING), $this->isType(\PHPUnit_Framework_Constraint_IsType::TYPE_STRING), $this->equalTo($ticket));
     $this->ticketRepository->expects($this->once())->method('store')->with($this->equalTo($ticket));
     $this->authorizationService->expects($this->once())->method('isActionPermitted')->with($this->equalTo('CREATE'), $this->equalTo('Entity:DiamanteDeskBundle:Ticket'))->will($this->returnValue(true));
     $command = new CreateTicketCommand();
     $command->branch = $branchId;
     $command->subject = self::SUBJECT;
     $command->description = self::DESCRIPTION;
     $command->reporter = (string) $reporter;
     $command->assignee = $assigneeId;
     $command->priority = $priority;
     $command->source = $source;
     $command->status = null;
     $command->attachmentsInput = $attachmentInputs;
     $this->ticketService->createTicket($command);
 }