Advertisement
Guest User

Untitled

a guest
Aug 26th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.22 KB | None | 0 0
  1. <?php
  2.  
  3. namespace LDMR\FormBundle\Service;
  4.  
  5.  
  6. use Doctrine\ORM\EntityManager;
  7. use LDMR\FormBundle\Entity\Form;
  8. use LDMR\FormBundle\Entity\LuluForm;
  9. use LDMR\FormBundle\Repository\FormQuestionRepository;
  10. use LDMR\FormBundle\Repository\LuluFormRepository;
  11. use LDMR\LuluBundle\Entity\Lulu;
  12. use Symfony\Component\Templating\EngineInterface as TemplatingEngine;
  13. use Symfony\Component\Filesystem\Filesystem;
  14. use Knp\Bundle\GaufretteBundle\FilesystemMap;
  15. use Knp\Snappy\GeneratorInterface as PdfGenerator;
  16. use \LogicException;
  17. use \Exception;
  18. use Doctrine\ORM\NonUniqueResultException;
  19. use Doctrine\ORM\OptimisticLockException;
  20.  
  21.  
  22.  
  23. /**
  24.  * Class FormPdf
  25.  * @package LDMR\FormBundle\Service;
  26.  */
  27. class FormPdf
  28. {
  29.     /** @var TemplatingEngine **/
  30.     private $templatingEngine;
  31.  
  32.     /** @var FilesystemMap **/
  33.     private $filesystemMap;
  34.  
  35.     /** @var EntityManager **/
  36.     private $em;
  37.  
  38.     /** @var PdfGenerator **/
  39.     private $pdfGenerator;
  40.  
  41.     /** @var FormQuestionRepository **/
  42.     private $repoQuestion;
  43.  
  44.     /** @var LuluFormRepository **/
  45.     private $repoLuluForm;
  46.  
  47.     /** @var string **/
  48.     private $kernelEnv;
  49.  
  50.     /** @var string **/
  51.     private $envSubDir;
  52.  
  53.     public function __construct(EntityManager $em, TemplatingEngine $templatingEngine, PdfGenerator $pdfGenerator, FilesystemMap $filesystemMap, string $kernelEnv, string $envSubDir) {
  54.         $this->em               = $em;
  55.         $this->repoLuluForm     = $this->em->getRepository('LDMRFormBundle:LuluForm');
  56.         $this->repoQuestion     = $this->em->getRepository('LDMRFormBundle:FormQuestion');
  57.         $this->pdfGenerator     = $pdfGenerator;
  58.         $this->templatingEngine = $templatingEngine;
  59.         $this->filesystemMap    = $filesystemMap;
  60.         $this->kernelEnv        = $kernelEnv;
  61.         $this->envSubDir        = $envSubDir;
  62.     }
  63.  
  64.  
  65.     /**
  66.      * @param Lulu $lulu
  67.      * @param Form $form
  68.      *
  69.      * @return string
  70.      *
  71.      * @throws NonUniqueResultException
  72.      * @throws OptimisticLockException
  73.      * @throws Exception
  74.      */
  75.     public function generateFormPdf (Lulu $lulu, Form $form)
  76.     {
  77.         /** @var LuluForm $luluForm */
  78.         /*$luluForm = $this->repoLuluForm->findOneObjectBy(['form' => $form, 'lulu' => $lulu], ['followupEmployee']);
  79.  
  80.         if (null === $luluForm){
  81.             throw new LogicException(sprintf("Lulu '%s' does not have answered Form '%s'.", $lulu->getId(), $form->getName()));
  82.         }*/
  83.  
  84.         $formResponse = $this->repoLuluForm->getFormattedLuluFormAnswers($form->getUniqueId(), $lulu->getId());
  85.  
  86.         $luluFormPath        = $this->envSubDir . '/form/'.$form->getUniqueId().'_'.$lulu->getId().'.pdf';
  87.         $luluFormPathTmpPath = $this->getFormattedOutputFilePath('/tmp/' . $luluFormPath);
  88.         $fs = new Filesystem();
  89.  
  90.         try {
  91.             $templateHtml = $this->templatingEngine->render(
  92.                 '::Pdf/Form/lulu_form.html.twig',
  93.                 [
  94.                     'user'         => $lulu->getUser(),
  95.                     'lulu'         => $lulu,
  96.                     'formResponse' => $formResponse
  97.                 ]
  98.             );
  99.  
  100.             $this->pdfGenerator->generateFromHtml(
  101.                 $templateHtml,
  102.                 $luluFormPathTmpPath,
  103.                 ['encoding' => 'utf-8'],
  104.                 true
  105.             );
  106.         }
  107.         catch (Exception $exception) {
  108.             $fs->remove($luluFormPathTmpPath);
  109.             throw new Exception($exception->getMessage());
  110.         }
  111.  
  112.         $bucket  = $this->filesystemMap->get('storage');
  113.         $bucket->write($luluFormPath, file_get_contents($luluFormPathTmpPath), true);
  114.  
  115.         $fs->remove($luluFormPathTmpPath);
  116.  
  117.         $luluForm->setPdfUrl($luluFormPath);
  118.         $this->repoLuluForm->flush();
  119.         $this->em->detach($formResponse);
  120.  
  121.         return $luluFormPath;
  122.     }
  123.  
  124.     /**
  125.      * format output file path for windows
  126.      *
  127.      * @param string $file
  128.      *
  129.      * @return string
  130.      */
  131.     protected function getFormattedOutputFilePath(string $file)
  132.     {
  133.         if (0 === strpos(PHP_OS, 'WIN')) {
  134.             $file = str_replace('\\', '/', sys_get_temp_dir()).str_replace('/tmp', '', $file);
  135.         }
  136.  
  137.         return $file;
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement