<commit_before><?php

/**
 * Description of PdfManager
 *
 */
namespace CanalTP\MttBundle\Services;

use Symfony\Component\DependencyInjection\Container;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Doctrine\Common\Persistence\ObjectManager;

use CanalTP\MttBundle\Entity\StopPoint;
use CanalTP\MttBundle\Entity\Timetable;

class PdfManager
{
    private $router = null;
    private $stopPoint = null;
    private $pdfGenerator = null;
    private $mediaManager = null;
    private $co = null;
    private $hashingLib = null;

    public function __construct(
        ObjectManager $om,
        Router $router,
        PdfGenerator $pdfGenerator,
        MediaManager $mediaManager,
        Container $co,
        PdfHashingLib $hashingLib
    )
    {
        $this->stopPoint = $om->getRepository('CanalTPMttBundle:StopPoint');
        $this->router = $router;
        $this->pdfGenerator = $pdfGenerator;
        $this->mediaManager = $mediaManager;
        $this->co = $co;
        $this->hashingLib = $hashingLib;
    }

    /**
     * We use http kernel to forward, would take more time using curl
     */
    public function getTimetableHtml($args)
    {
        $args['_controller'] = 'CanalTPMttBundle:Timetable:view';
        $subRequest = $this->co->get('request')->duplicate(array(), null, $args);
        $subRequest->headers->remove('X-Requested-With');

        return $this->co->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST)->getContent();
    }

    public function getPdfHash(Timetable $timetable, $externalStopPointId)
    {
        $response = $this->getTimetableHtml(
            array(
                'externalNetworkId' => $timetable->getLineConfig()->getSeason()->getNetwork()->getExternalId(),
                'seasonId'          => $timetable->getLineConfig()->getSeason()->getId(),
                'externalLineId'    => $timetable->getLineConfig()->getExternalLineId(),
                'externalStopPointId'=> $externalStopPointId,
                'externalRouteId'    => $timetable->getExternalRouteId(),
                'timetableOnly'      => true
            )
        );
        $cssVersion = $timetable->getLineConfig()->getLayoutConfig()->getLayout()->getCssVersion();

        return $this->hashingLib->getPdfHash($response, $cssVersion);
    }

    public function getStoppointPdfUrl(Timetable $timetable, $externalStopPointId)
    {
        $hash = $this->getPdfHash($timetable, $externalStopPointId);
        $stopPoint = $this->stopPoint->findOneByExternalId($externalStopPointId);

        if (!empty($stopPoint) && $hash == $stopPoint->getPdfHash()) {
            $pdfMedia = $this->mediaManager->getStopPointTimetableMedia($timetable, $externalStopPointId);
        } else {
            $url = $this->co->get('request')->getHttpHost() . $this->co->get('router')->generate(
                'canal_tp_mtt_timetable_view',
                array(
                    'externalNetworkId' => $timetable->getLineConfig()->getSeason()->getNetwork()->getExternalId(),
                    'seasonId'          => $timetable->getLineConfig()->getSeason()->getId(),
                    'externalLineId'    => $timetable->getLineConfig()->getExternalLineId(),
                    'externalStopPointId'=> $externalStopPointId,
                    'externalRouteId'    => $timetable->getExternalRouteId()
                )
            );

            $pdfPath = $this->pdfGenerator->getPdf(
                $url, 
                $timetable->getLineConfig()->getLayoutConfig()->getLayout()->getOrientation()
            );
            if ($pdfPath) {
                $pdfMedia = $this->mediaManager->saveStopPointTimetable($timetable, $externalStopPointId, $pdfPath);
                $this->stopPoint->updatePdfGenerationInfos($externalStopPointId, $timetable, $hash);
            } else {
                throw new \Exception('PdfGenerator Webservice returned an empty response.');
            }
        }

        return $this->mediaManager->getUrlByMedia($pdfMedia);
    }
}
<commit_msg>Fix generate pdf (bad orientation send) + add deploy for templates<commit_after><?php

/**
 * Description of PdfManager
 *
 */
namespace CanalTP\MttBundle\Services;

use Symfony\Component\DependencyInjection\Container;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Doctrine\Common\Persistence\ObjectManager;

use CanalTP\MttBundle\Entity\StopPoint;
use CanalTP\MttBundle\Entity\Timetable;

class PdfManager
{
    private $router = null;
    private $stopPoint = null;
    private $pdfGenerator = null;
    private $mediaManager = null;
    private $co = null;
    private $hashingLib = null;

    public function __construct(
        ObjectManager $om,
        Router $router,
        PdfGenerator $pdfGenerator,
        MediaManager $mediaManager,
        Container $co,
        PdfHashingLib $hashingLib
    )
    {
        $this->stopPoint = $om->getRepository('CanalTPMttBundle:StopPoint');
        $this->router = $router;
        $this->pdfGenerator = $pdfGenerator;
        $this->mediaManager = $mediaManager;
        $this->co = $co;
        $this->hashingLib = $hashingLib;
    }

    /**
     * We use http kernel to forward, would take more time using curl
     */
    public function getTimetableHtml($args)
    {
        $args['_controller'] = 'CanalTPMttBundle:Timetable:view';
        $subRequest = $this->co->get('request')->duplicate(array(), null, $args);
        $subRequest->headers->remove('X-Requested-With');

        return $this->co->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST)->getContent();
    }

    public function getPdfHash(Timetable $timetable, $externalStopPointId)
    {
        $response = $this->getTimetableHtml(
            array(
                'externalNetworkId' => $timetable->getLineConfig()->getSeason()->getNetwork()->getExternalId(),
                'seasonId'          => $timetable->getLineConfig()->getSeason()->getId(),
                'externalLineId'    => $timetable->getLineConfig()->getExternalLineId(),
                'externalStopPointId'=> $externalStopPointId,
                'externalRouteId'    => $timetable->getExternalRouteId(),
                'timetableOnly'      => true
            )
        );
        $cssVersion = $timetable->getLineConfig()->getLayoutConfig()->getLayout()->getCssVersion();

        return $this->hashingLib->getPdfHash($response, $cssVersion);
    }

    public function getStoppointPdfUrl(Timetable $timetable, $externalStopPointId)
    {
        $hash = $this->getPdfHash($timetable, $externalStopPointId);
        $stopPoint = $this->stopPoint->findOneByExternalId($externalStopPointId);

        if (!empty($stopPoint) && $hash == $stopPoint->getPdfHash()) {
            $pdfMedia = $this->mediaManager->getStopPointTimetableMedia($timetable, $externalStopPointId);
        } else {
            $url = $this->co->get('request')->getHttpHost() . $this->co->get('router')->generate(
                'canal_tp_mtt_timetable_view',
                array(
                    'externalNetworkId' => $timetable->getLineConfig()->getSeason()->getNetwork()->getExternalId(),
                    'seasonId'          => $timetable->getLineConfig()->getSeason()->getId(),
                    'externalLineId'    => $timetable->getLineConfig()->getExternalLineId(),
                    'externalStopPointId'=> $externalStopPointId,
                    'externalRouteId'    => $timetable->getExternalRouteId()
                )
            );

            $pdfPath = $this->pdfGenerator->getPdf(
                $url,
                $timetable->getLineConfig()->getLayoutConfig()->getLayout()->getOrientationAsString()
            );
            if ($pdfPath) {
                $pdfMedia = $this->mediaManager->saveStopPointTimetable($timetable, $externalStopPointId, $pdfPath);
                $this->stopPoint->updatePdfGenerationInfos($externalStopPointId, $timetable, $hash);
            } else {
                throw new \Exception('PdfGenerator Webservice returned an empty response.');
            }
        }

        return $this->mediaManager->getUrlByMedia($pdfMedia);
    }
}

{'fraction_non_alphanumeric': 0.13662720463880165, 'fraction_numerical': 0.0, 'mean_word_length': 2.653574580759047, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 159, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3791632', 'n_tokens_mistral': 2354, 'n_tokens_neox': 2250, 'n_words': 353}