public function testFetchOroUser()
 {
     $id = 'oro_1';
     $user = User::fromString($id);
     $this->userService->expects($this->once())->method('getOroUser')->with($this->equalTo($user))->will($this->returnValue(new \Oro\Bundle\UserBundle\Entity\User()));
     $this->userDetailsExtension->fetchOroUser($user);
 }
 /**
  * {@inheritdoc}
  */
 public function reverseTransform($value)
 {
     if (filter_var($value, FILTER_VALIDATE_EMAIL)) {
         return $value;
     } elseif (preg_match('/^(diamante|oro)_\\d+$/i', $value)) {
         return User::fromString($value);
     } else {
         return null;
     }
 }
 /**
  * @test
  */
 public function testConvertItem()
 {
     $id = 'diamante_1';
     $userObj = User::fromString($id);
     $userDetails = $this->createUserDetails(User::TYPE_DIAMANTE);
     $this->userService->expects($this->once())->method('fetchUserDetails')->with($this->equalTo($userObj))->will($this->returnValue($userDetails));
     $result = $this->diamanteUserSearchHandler->convertItem($userObj);
     $this->assertInternalType('array', $result);
     $this->assertEquals($id, $result['id']);
     $this->assertEquals(User::TYPE_DIAMANTE, $result['type']);
 }
 /**
  * @param User $user
  *
  * @return UserDetails
  *
  * @throws \Twig_Error_Runtime
  */
 public function fetchUserDetails($user)
 {
     if (empty($user)) {
         return '';
     }
     if (is_string($user)) {
         $user = User::fromString($user);
     }
     /**
      * @var UserDetails $details
      */
     $details = $this->userService->fetchUserDetails($user);
     if (empty($details)) {
         throw new \Twig_Error_Runtime('Failed to load user details');
     }
     return $details;
 }
 /**
  * @Route(
  *       "/watchers/ticket/{ticketId}",
  *      name="diamante_ticket_watchers",
  *      requirements={"ticketId"="\d+"}
  * )
  * @Template("DiamanteDeskBundle:Ticket:widget/watchers.html.twig")
  *
  * @param int $ticketId
  * @return array
  */
 public function watchers($ticketId)
 {
     $ticket = $this->get('diamante.ticket.service')->loadTicket($ticketId);
     $users = [];
     foreach ($ticket->getWatcherList() as $watcher) {
         $users[] = User::fromString($watcher->getUserType());
     }
     return ['watchers' => $users];
 }
 /**
  * Post Comment for Ticket
  * @param Command\CommentCommand $command
  * @return Comment
  *
  * @throws ForbiddenException
  * @throws TicketNotFoundException
  */
 public function postNewCommentForTicket(Command\CommentCommand $command)
 {
     $this->isGranted('CREATE', 'Entity:DiamanteDeskBundle:Comment');
     \Assert\that($command->attachmentsInput)->nullOr()->all()->isInstanceOf('Diamante\\DeskBundle\\Api\\Dto\\AttachmentInput');
     /**
      * @var $ticket \Diamante\DeskBundle\Model\Ticket\Ticket
      */
     $ticket = $this->loadTicketBy($command->ticket);
     $author = User::fromString($command->author);
     $comment = $this->commentFactory->create($command->content, $ticket, $author, $command->private);
     $this->createAttachments($command, $comment);
     $ticket->updateStatus(new Status($command->ticketStatus));
     $ticket->updatedTimestamps();
     $ticket->postNewComment($comment);
     $this->ticketRepository->store($ticket);
     $this->dispatchWorkflowEvent($this->registry, $this->dispatcher, $comment);
     return $comment;
 }
 /**
  * Creates Comment for Ticket
  *
  * @param $content
  * @param $authorId
  * @param $messageId
  * @param array|null $attachments
  * @return Ticket|null
  */
 public function createCommentForTicket($content, $authorId, $messageId, array $attachments = null)
 {
     if (empty($content)) {
         return null;
     }
     $reference = $this->messageReferenceRepository->getReferenceByMessageId($messageId);
     if (is_null($reference)) {
         $this->logger->error(sprintf('Ticket not found for message: %s', $messageId));
         throw new \RuntimeException('Ticket loading failed, ticket not found.');
     }
     $ticket = $reference->getTicket();
     $command = new CommentCommand();
     $command->ticket = $ticket->getId();
     $command->content = $content;
     $command->author = User::fromString($authorId);
     $command->ticketStatus = $ticket->getStatus();
     $command->attachmentsInput = $this->convertAttachments($attachments);
     $this->commentService->postNewCommentForTicket($command);
     return $ticket;
 }
 /**
  * @Route(
  *      "/watcher/ticket/{ticketId}/{user}",
  *      name="diamante_remove_watcher",
  *      requirements={"ticketId"="\d+"}
  * )
  *
  * @param int $ticketId
  * @param string $user
  * @return array|Response
  */
 public function deleteWatcherAction($ticketId, $user)
 {
     $repository = $this->getDoctrine()->getManager()->getRepository('DiamanteDeskBundle:Ticket');
     $ticket = $repository->get($ticketId);
     $ticketKey = $ticket->getKey();
     try {
         $user = User::fromString($user);
         $this->get('diamante.watcher.service.api')->removeWatcher($ticket, $user);
         $this->addSuccessMessage('diamante.desk.ticket.messages.watcher_remove.success');
         $response = $this->redirect($this->generateUrl('diamante_ticket_view', array('key' => $ticketKey)));
     } catch (TicketNotFoundException $e) {
         $this->handleException($e);
         $response = $this->redirect($this->generateUrl('diamante_ticket_list'));
     } catch (\Exception $e) {
         $this->handleException($e);
         $response = $this->redirect($this->generateUrl('diamante_ticket_view', array('key' => $ticketKey)));
     }
     return $response;
 }
 /**
  * @param Notification $notification
  * @return \ArrayAccess
  */
 private function postProcessChangesList(Notification $notification)
 {
     $changes = $notification->getChangeList();
     if (isset($changes['Reporter']) && strpos($changes['Reporter'], '_')) {
         $r = $changes['Reporter'];
         $u = User::fromString($r);
         $details = $this->userService->fetchUserDetails($u);
         $changes['Reporter'] = $details->getFullName();
     }
     return $changes;
 }
 /**
  * Delete Watcher from ticket
  *
  * @ApiDoc(
  *  description="Delete watcher",
  *  uri="/tickets/{id}/watchers/{userId}.{_format}",
  *  method="DELETE",
  *  resource=true,
  *  requirements={
  *      {
  *          "name"="id",
  *          "dataType"="integer",
  *          "requirement"="\d+",
  *          "description"="Ticket Id"
  *      },
  *      {
  *          "name"="userId",
  *          "dataType"="string",
  *          "description"="User id"
  *      }
  *  },
  *  statusCodes={
  *      201="Returned when successful",
  *      403="Returned when the user is not authorized",
  *      404="Returned when the ticket not found"
  *  }
  * )
  *
  * @param Command\RemoveWatcherCommand $command
  * @return null
  */
 public function removeWatcherById(Command\RemoveWatcherCommand $command)
 {
     $ticket = $this->getTicket($command);
     /** @var WatcherList $watcher */
     foreach ($ticket->getWatcherList() as $watcher) {
         if ($watcher->getUserType() == $command->userId) {
             $user = User::fromString($command->userId);
             $this->removeWatcher($ticket, $user);
             break;
         }
     }
 }
 /**
  * @param string $id
  * @return $this
  */
 public function setReporter($id)
 {
     $reporter = User::fromString($id);
     $this->reporter = $reporter;
     return $this;
 }
 /**
  * Post Comment for Ticket
  * @param Command\CommentCommand $command
  * @return \Diamante\DeskBundle\Model\Ticket\Comment
  */
 public function postNewCommentForTicket(Command\CommentCommand $command)
 {
     $this->isGranted('CREATE', 'Entity:DiamanteDeskBundle:Comment');
     \Assert\that($command->attachmentsInput)->nullOr()->all()->isInstanceOf('Diamante\\DeskBundle\\Api\\Dto\\AttachmentInput');
     /**
      * @var $ticket \Diamante\DeskBundle\Model\Ticket\Ticket
      */
     $ticket = $this->loadTicketBy($command->ticket);
     $author = User::fromString($command->author);
     $comment = $this->commentFactory->create($command->content, $ticket, $author, $command->private);
     if ($command->attachmentsInput) {
         foreach ($command->attachmentsInput as $each) {
             $this->attachmentManager->createNewAttachment($each->getFilename(), $each->getContent(), $comment);
         }
     }
     $ticket->updateStatus(new Status($command->ticketStatus));
     $ticket->postNewComment($comment);
     $this->ticketRepository->store($ticket);
     $this->dispatchEvents($comment, $ticket);
     return $comment;
 }
 /**
  * @Route(
  *       "/watchers/ticket/{ticket}",
  *      name="diamante_ticket_watchers",
  *      requirements={"ticket"="\d+"}
  * )
  * @Template("DiamanteDeskBundle:Ticket:widget/watchers.html.twig")
  *
  * @param Ticket $ticket
  * @return array
  */
 public function watchersAction($ticket)
 {
     $ticket = $this->container->get('diamante.ticket.repository')->get($ticket);
     $users = [];
     foreach ($ticket->getWatcherList() as $watcher) {
         $users[] = User::fromString($watcher->getUserType());
     }
     return ['ticket' => $ticket, 'watchers' => $users];
 }
 /**
  * @test
  */
 public function testCreatesFromString()
 {
     $user = User::fromString(User::TYPE_ORO . User::DELIMITER . self::TEST_ID);
     $this->assertEquals(User::TYPE_ORO, $user->getType());
     $this->assertEquals(self::TEST_ID, $user->getId());
 }
 /**
  * Creates Comment for Ticket
  *
  * @param $content
  * @param $authorId
  * @param $messageId
  * @param array $attachments
  * @return Ticket|null
  */
 public function createCommentForTicket($content, $authorId, $messageId, array $attachments = null)
 {
     $reference = $this->messageReferenceRepository->getReferenceByMessageId($messageId);
     if (is_null($reference)) {
         $this->logger->error(sprintf('Ticket not found for message: %s', $messageId));
         throw new \RuntimeException('Ticket loading failed, ticket not found.');
     }
     $ticket = $reference->getTicket();
     $author = User::fromString($authorId);
     if (empty($content)) {
         return null;
     }
     $comment = $this->commentFactory->create($content, $ticket, $author);
     if ($attachments) {
         $this->createAttachments($attachments, $comment);
     }
     $ticket->postNewComment($comment);
     $this->ticketRepository->store($ticket);
     $this->dispatchEvents($ticket);
     return $ticket;
 }
 /**
  * @param Ticket $ticket
  */
 protected function startAutoReply(Ticket $ticket)
 {
     $ticketText = $ticket->getSubject() . ' ' . $ticket->getDescription();
     $repository = $this->registry->getManager()->getRepository('DiamanteDeskBundle:Article');
     $results = [];
     /** @var Article $article */
     foreach ($repository->findByStatus(1) as $article) {
         $articleText = $article->getTitle() . ' ' . $article->getContent();
         $results[$article->getId()] = $this->compare($ticketText, $articleText);
     }
     $maxResult = max($results);
     /**
      * TODO: should be configured by admin
      */
     if ($maxResult < 3) {
         return;
     }
     $articleId = array_search(max($results), $results);
     /**
      * TODO: should be extracted from previous call of $repository->getAll()
      */
     $article = $repository->find($articleId);
     /**
      * TODO: should be extracted from configuration???
      */
     $user = User::fromString('oro_1');
     $content = $this->getAutoReplayNoticeHtml() . $article->getContent();
     $comment = new Comment($content, $ticket, $user, false);
     $this->registry->getManager()->persist($comment);
     $this->uow->computeChangeSet($this->em->getClassMetadata($comment->getClassName()), $comment);
     /**
      * TODO: should be executed workflowEven?
      */
 }