NewPdfGeneratorHelper::generateHTML()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 33
rs 9.6
c 0
b 0
f 0
cc 2
nc 1
nop 1
1
<?php
2
3
namespace Stfalcon\Bundle\EventBundle\Helper;
4
5
use Application\Bundle\DefaultBundle\Service\SvgToJpg;
6
use Stfalcon\Bundle\EventBundle\Entity\Ticket;
7
use TFox\MpdfPortBundle\Service\MpdfService;
8
use Twig_Environment;
9
use Symfony\Component\Routing\Router;
10
use Endroid\QrCode\QrCode;
11
use Symfony\Component\HttpKernel\Kernel;
12
13
/**
14
 * Class PdfGeneratorHelper.
15
 */
16
class NewPdfGeneratorHelper
17
{
18
    /**
19
     * @var Twig_Environment
20
     */
21
    protected $templating;
22
23
    /**
24
     * @var Router
25
     */
26
    protected $router;
27
28
    /**
29
     * @var QrCode
30
     */
31
    protected $qrCode;
32
33
    /**
34
     * @var Kernel
35
     */
36
    protected $kernel;
37
38
    /**
39
     * @var MpdfService
40
     */
41
    protected $mPdfPort;
42
43
    /**
44
     * @var SvgToJpg
45
     */
46
    protected $svgToJpgService;
47
48
    /**
49
     * Constructor.
50
     *
51
     * @param Twig_Environment $templating      Twig
52
     * @param Router           $router          Router
53
     * @param QrCode           $qrCode          QrCode generator
54
     * @param Kernel           $kernel          Kernel
55
     * @param MpdfService      $mPdfPort
56
     * @param SvgToJpg         $svgToJpgService
57
     */
58
    public function __construct($templating, $router, $qrCode, $kernel, $mPdfPort, $svgToJpgService)
59
    {
60
        $this->templating = $templating;
61
        $this->router = $router;
62
        $this->qrCode = $qrCode;
63
        $this->kernel = $kernel;
64
        $this->mPdfPort = $mPdfPort;
65
        $this->svgToJpgService = $svgToJpgService;
66
    }
67
68
    /**
69
     * Generate PDF-file of ticket.
70
     *
71
     * @param Ticket $ticket
72
     * @param string $html
73
     *
74
     * @return mixed
75
     */
76
    public function generatePdfFile(Ticket $ticket, $html)
77
    {
78
        // Override default fonts directory for mPDF
79
        define('_MPDF_SYSTEM_TTFONTS', realpath($this->kernel->getRootDir().'/../web/fonts/').'/');
80
81
        $this->mPdfPort->setAddDefaultConstructorArgs(false);
82
83
        $constructorArgs = array(
84
            'mode' => 'BLANK',
85
            'format' => [87, 151],
86
            'margin_left' => 2,
87
            'margin_right' => 2,
88
            'margin_top' => 2,
89
            'margin_bottom' => 2,
90
            'margin_header' => 2,
91
            'margin_footer' => 2,
92
        );
93
94
        $mPDF = $this->mPdfPort->getMpdf($constructorArgs);
95
96
        // Fwdays font settings
97
        $mPDF->fontdata['fwdays'] = array(
98
            'R' => 'FwDaysFont-Medium.ttf',
99
        );
100
        // phpcs:disable Zend.NamingConventions.ValidVariableName.NotCamelCaps
101
        $mPDF->sans_fonts[] = 'fwdays';
102
        $mPDF->available_unifonts[] = 'fwdays';
103
        $mPDF->default_available_fonts[] = 'fwdays';
104
        // phpcs:enable
105
        $mPDF->SetDisplayMode('fullpage');
106
        $mPDF->WriteHTML($html);
107
        $pdfFile = $mPDF->Output($ticket->generatePdfFilename(), 'S');
108
109
        return $pdfFile;
110
    }
111
112
    /**
113
     * @param Ticket $ticket
114
     *
115
     * @return string
116
     *
117
     * @throws \Endroid\QrCode\Exceptions\ImageFunctionFailedException
118
     * @throws \Endroid\QrCode\Exceptions\ImageFunctionUnknownException
119
     */
120
    public function generateHTML(Ticket $ticket)
121
    {
122
        $twig = $this->templating;
123
124
        $url = $this->router->generate(
125
            'event_ticket_registration',
126
            [
127
                'ticket' => $ticket->getId(),
128
                'hash' => $ticket->getHash(),
129
            ],
130
            true
131
        );
132
133
        $this->qrCode->setText($url);
134
        $this->qrCode->setSize(105);
135
        $this->qrCode->setPadding(0);
136
        $qrCodeBase64 = base64_encode($this->qrCode->get());
137
        $templateContent = $twig->load('ApplicationDefaultBundle:Ticket:_new_pdf.html.twig');
138
        $logoFile = $ticket->getEvent()->getSmallLogoFile() ?: $ticket->getEvent()->getLogoFile();
139
        $imageData = $this->svgToJpgService->convert($logoFile);
140
141
        $base64EventSmallLogo = base64_encode($imageData);
142
143
        $body = $templateContent->render(
144
            [
145
                'ticket' => $ticket,
146
                'qrCodeBase64' => $qrCodeBase64,
147
                'path' => realpath($this->kernel->getRootDir().'/../web').'/',
148
                'event_logo' => $base64EventSmallLogo,
149
            ]
150
        );
151
152
        return $body;
153
    }
154
}
155