Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace LDMR\FormBundle\Service;
- use Doctrine\ORM\EntityManager;
- use LDMR\FormBundle\Entity\Form;
- use LDMR\FormBundle\Entity\LuluForm;
- use LDMR\FormBundle\Repository\FormQuestionRepository;
- use LDMR\FormBundle\Repository\LuluFormRepository;
- use LDMR\LuluBundle\Entity\Lulu;
- use Symfony\Component\Templating\EngineInterface as TemplatingEngine;
- use Symfony\Component\Filesystem\Filesystem;
- use Knp\Bundle\GaufretteBundle\FilesystemMap;
- use Knp\Snappy\GeneratorInterface as PdfGenerator;
- use \LogicException;
- use \Exception;
- use Doctrine\ORM\NonUniqueResultException;
- use Doctrine\ORM\OptimisticLockException;
- /**
- * Class FormPdf
- * @package LDMR\FormBundle\Service;
- */
- class FormPdf
- {
- /** @var TemplatingEngine **/
- private $templatingEngine;
- /** @var FilesystemMap **/
- private $filesystemMap;
- /** @var EntityManager **/
- private $em;
- /** @var PdfGenerator **/
- private $pdfGenerator;
- /** @var FormQuestionRepository **/
- private $repoQuestion;
- /** @var LuluFormRepository **/
- private $repoLuluForm;
- /** @var string **/
- private $kernelEnv;
- /** @var string **/
- private $envSubDir;
- public function __construct(EntityManager $em, TemplatingEngine $templatingEngine, PdfGenerator $pdfGenerator, FilesystemMap $filesystemMap, string $kernelEnv, string $envSubDir) {
- $this->em = $em;
- $this->repoLuluForm = $this->em->getRepository('LDMRFormBundle:LuluForm');
- $this->repoQuestion = $this->em->getRepository('LDMRFormBundle:FormQuestion');
- $this->pdfGenerator = $pdfGenerator;
- $this->templatingEngine = $templatingEngine;
- $this->filesystemMap = $filesystemMap;
- $this->kernelEnv = $kernelEnv;
- $this->envSubDir = $envSubDir;
- }
- /**
- * @param Lulu $lulu
- * @param Form $form
- *
- * @return string
- *
- * @throws NonUniqueResultException
- * @throws OptimisticLockException
- * @throws Exception
- */
- public function generateFormPdf (Lulu $lulu, Form $form)
- {
- /** @var LuluForm $luluForm */
- /*$luluForm = $this->repoLuluForm->findOneObjectBy(['form' => $form, 'lulu' => $lulu], ['followupEmployee']);
- if (null === $luluForm){
- throw new LogicException(sprintf("Lulu '%s' does not have answered Form '%s'.", $lulu->getId(), $form->getName()));
- }*/
- $formResponse = $this->repoLuluForm->getFormattedLuluFormAnswers($form->getUniqueId(), $lulu->getId());
- $luluFormPath = $this->envSubDir . '/form/'.$form->getUniqueId().'_'.$lulu->getId().'.pdf';
- $luluFormPathTmpPath = $this->getFormattedOutputFilePath('/tmp/' . $luluFormPath);
- $fs = new Filesystem();
- try {
- $templateHtml = $this->templatingEngine->render(
- '::Pdf/Form/lulu_form.html.twig',
- [
- 'user' => $lulu->getUser(),
- 'lulu' => $lulu,
- 'formResponse' => $formResponse
- ]
- );
- $this->pdfGenerator->generateFromHtml(
- $templateHtml,
- $luluFormPathTmpPath,
- ['encoding' => 'utf-8'],
- true
- );
- }
- catch (Exception $exception) {
- $fs->remove($luluFormPathTmpPath);
- throw new Exception($exception->getMessage());
- }
- $bucket = $this->filesystemMap->get('storage');
- $bucket->write($luluFormPath, file_get_contents($luluFormPathTmpPath), true);
- $fs->remove($luluFormPathTmpPath);
- $luluForm->setPdfUrl($luluFormPath);
- $this->repoLuluForm->flush();
- $this->em->detach($formResponse);
- return $luluFormPath;
- }
- /**
- * format output file path for windows
- *
- * @param string $file
- *
- * @return string
- */
- protected function getFormattedOutputFilePath(string $file)
- {
- if (0 === strpos(PHP_OS, 'WIN')) {
- $file = str_replace('\\', '/', sys_get_temp_dir()).str_replace('/tmp', '', $file);
- }
- return $file;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement