/**
  * @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?
      */
 }
 /**
  * Updates DB Schema. Changes from Diamante only will be applied for current schema. Other bundles updating skips
  * @throws \Exception if there are no changes in entities
  */
 protected function updateDbSchema()
 {
     /**
      * @var $em \Doctrine\ORM\EntityManager
      */
     $em = $this->getContainer()->get('doctrine.orm.entity_manager');
     $event = $em->getEventManager();
     $sm = $em->getConnection()->getSchemaManager();
     $allMetadata = $em->getMetadataFactory()->getAllMetadata();
     $schemaTool = new SchemaTool($em);
     $entitiesMetadata = array($em->getClassMetadata(\Diamante\DeskBundle\Entity\Branch::getClassName()), $em->getClassMetadata(\Diamante\DeskBundle\Entity\Ticket::getClassName()), $em->getClassMetadata(\Diamante\DeskBundle\Entity\Comment::getClassName()), $em->getClassMetadata(\Diamante\DeskBundle\Entity\Attachment::getClassName()), $em->getClassMetadata(\Diamante\DeskBundle\Entity\BranchEmailConfiguration::getClassName()), $em->getClassMetadata(\Diamante\DeskBundle\Entity\MessageReference::getClassName()), $em->getClassMetadata(\Diamante\DeskBundle\Entity\TicketHistory::getClassName()), $em->getClassMetadata(\Diamante\DeskBundle\Entity\WatcherList::getClassName()), $em->getClassMetadata(\Diamante\DeskBundle\Entity\TicketTimeline::getClassName()), $em->getClassMetadata(\Diamante\DeskBundle\Entity\Audit::getClassName()), $em->getClassMetadata(\Diamante\DeskBundle\Entity\AuditField::getClassName()), $em->getClassMetadata(\Diamante\DeskBundle\Entity\Article::getClassName()));
     $event->disableListeners();
     $currentSchema = $sm->createSchema();
     $schemaFromMetadata = $schemaTool->getSchemaFromMetadata($allMetadata);
     $entitiesSchema = $schemaTool->getSchemaFromMetadata($entitiesMetadata);
     $entitiesTables = $entitiesSchema->getTables();
     $entitiesTableName = array_keys($entitiesTables);
     $currentDiamanteSchema = $this->getTargetSchema($currentSchema, $entitiesTableName);
     $diamanteSchemaFromMetadata = $this->getTargetSchema($schemaFromMetadata, $entitiesTableName);
     $comparator = new Comparator();
     $diff = $comparator->compare($currentDiamanteSchema, $diamanteSchemaFromMetadata);
     $toUpdate = $diff->toSql($em->getConnection()->getDatabasePlatform());
     if (empty($toUpdate)) {
         throw new \Exception('No new updates found. DiamanteDesk is up to date!');
     }
     $conn = $em->getConnection();
     foreach ($toUpdate as $sql) {
         $conn->executeQuery($sql);
     }
 }
 /**
  * Get list of Diamante tables to purge data from
  *
  * @return array
  */
 protected function getTablesList()
 {
     $entitiesMetadata = array($this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\Branch::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\BranchEmailConfiguration::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\Ticket::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\Comment::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\Attachment::getClassName()));
     $toPurge = array();
     /** @var $entity \Doctrine\ORM\Mapping\ClassMetadata */
     foreach ($entitiesMetadata as $entity) {
         $tableName = $entity->getTableName();
         $toPurge[] = $tableName;
     }
     return $toPurge;
 }
 public function testRenderTicket()
 {
     $tags = array(array('id' => 1, 'name' => 'Ticket Tag'));
     $renderTagResult = '<span class="tag-inline">Ticket Tag</span>';
     $this->registry->expects($this->once())->method('getRepository')->will($this->returnValue($this->sharedRepository));
     $this->tagManager->expects($this->once())->method('loadTagging')->will($this->returnValue($this->tagManager));
     $this->twig->expects($this->once())->method('render')->will($this->returnValue($renderTagResult));
     $this->sharedRepository->expects($this->once())->method('get')->will($this->returnValue($this->ticket));
     $this->ticket->expects($this->once())->method('getTags')->will($this->returnValue($this->commonCollection));
     $this->commonCollection->expects($this->once())->method('getValues')->will($this->returnValue($tags));
     $ticketResult = $this->renderTagExtensionExtension->renderTag($this->twig, 1, 'ticket');
     $this->assertEquals($renderTagResult, $ticketResult);
 }
 public function testUpdatePropertiesByKey()
 {
     $this->ticketRepository->expects($this->once())->method('get')->will($this->returnValue($this->ticket));
     $properties = array('subject' => 'DUMMY_SUBJECT_UPDT_BY_KEY', 'description' => 'DUMMY_DESC_UPDT_BY_KEY', 'status' => 'open', 'priority' => 'high', 'source' => 'phone');
     $this->ticket->expects($this->once())->method('updateProperties')->with($this->equalTo($properties));
     $this->ticket->expects($this->once())->method('getId')->will($this->returnValue(1));
     $this->ticketRepository->expects($this->once())->method('getByTicketKey')->with(new TicketKey('DT', 1))->will($this->returnValue($this->ticket));
     $this->ticketRepository->expects($this->once())->method('store')->with($this->equalTo($this->ticket));
     $this->authorizationService->expects($this->any())->method('isActionPermitted')->with($this->anything(), $this->equalTo($this->ticket))->will($this->returnValue(true));
     $command = new UpdatePropertiesCommand();
     $command->key = static::DUMMY_TICKET_KEY;
     $command->properties = $properties;
     $this->ticketService->updatePropertiesByKey($command);
 }
 /**
  * @param CommentCommand $command
  * @param $callback
  * @param Ticket $ticket
  * @return array
  */
 private function edit(CommentCommand $command, $callback, Ticket $ticket)
 {
     $response = null;
     $form = $this->createForm(new CommentType(), $command);
     $formView = $form->createView();
     $formView->children['attachmentsInput']->vars = array_replace($formView->children['attachmentsInput']->vars, array('full_name' => 'diamante_comment_form[attachmentsInput][]'));
     try {
         $this->handle($form);
         $callback($command);
         if ($command->id) {
             $this->addSuccessMessage('diamante.desk.comment.messages.save.success');
         } else {
             $this->addSuccessMessage('diamante.desk.comment.messages.create.success');
         }
         $response = $this->getSuccessSaveResponse((string) $ticket->getKey());
     } catch (MethodNotAllowedException $e) {
         $response = array('form' => $formView, 'ticket' => $ticket);
     } catch (\Exception $e) {
         $this->container->get('monolog.logger.diamante')->error(sprintf('Comment saving failed: %s', $e->getMessage()));
         $this->addErrorMessage('diamante.desk.comment.messages.create.error');
         $response = array('form' => $formView, 'ticket' => $ticket);
     }
     return $response;
 }
 /**
  * Get list of Diamante tables to purge data from
  *
  * @return array
  */
 protected function getTablesList()
 {
     $entitiesMetadata = array($this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\Branch::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\BranchEmailConfiguration::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\Ticket::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\Comment::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\Attachment::getClassName()), $this->entityManager->getClassMetadata(\Diamante\DeskBundle\Entity\TicketHistory::getClassName()), $this->entityManager->getClassMetadata('\\Oro\\Bundle\\TagBundle\\Entity\\Tag'), $this->entityManager->getClassMetadata('\\Oro\\Bundle\\TagBundle\\Entity\\Tagging'));
     $toPurge = array();
     $quoteStrategy = new DefaultQuoteStrategy();
     /** @var $entity \Doctrine\ORM\Mapping\ClassMetadata */
     foreach ($entitiesMetadata as $entity) {
         $tableName = $entity->getTableName();
         $toPurge[] = $tableName;
         foreach ($entity->getAssociationMappings() as $assoc) {
             if (isset($assoc['joinTable'])) {
                 $toPurge[] = $quoteStrategy->getJoinTableName($assoc, $entity, $this->dbPlatform);
             }
         }
     }
     return $toPurge;
 }
 /**
  * @param CommentCommand $command
  * @param $callback
  * @param Ticket $ticket
  * @return array
  */
 private function edit(CommentCommand $command, $callback, Ticket $ticket)
 {
     $response = null;
     $form = $this->createForm('diamante_comment_form', $command);
     $formView = $form->createView();
     $formView->children['attachmentsInput']->vars = array_replace($formView->children['attachmentsInput']->vars, array('full_name' => 'diamante_comment_form[attachmentsInput][]'));
     try {
         $this->handle($form);
         $callback($command);
         if ($command->id) {
             $this->addSuccessMessage('diamante.desk.comment.messages.save.success');
         } else {
             $this->addSuccessMessage('diamante.desk.comment.messages.create.success');
         }
         $response = $this->getSuccessSaveResponse('diamante_comment_update', 'diamante_ticket_view', ['key' => (string) $ticket->getKey()]);
     } catch (\Exception $e) {
         $this->handleException($e);
         $response = array('form' => $formView, 'ticket' => $ticket);
     }
     return $response;
 }
 /**
  * @return int
  */
 public function getTicketId()
 {
     return $this->ticket->getId();
 }