Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AttachmentTemplate
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 render
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
12
 applyTemplate
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/** @noinspection PhpUnhandledExceptionInspection */
3declare(strict_types=1);
4// TODO unit-test
5
6class AttachmentTemplate {
7  private const DEFAULT_LABEL_TEMPLATE = "%TESTTAKER% | %BOOKLET% | %UNIT% | %VAR%";
8
9  public static function render(?string $labelTemplate, Attachment ...$attachments): string {
10    $title = (count($attachments) > 1)
11      ? implode(', ',
12        array_unique(
13          array_map(
14            function(Attachment $attachment): string {
15              return $attachment->_groupLabel;
16            },
17            $attachments
18          )
19        )
20      )
21      : self::applyTemplate($attachments[0], $labelTemplate);
22
23    $pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
24    $pdf->SetCreator('IQB-Testcenter');
25    $pdf->SetTitle($title);
26    $pdf->setPrintHeader(false);
27    $pdf->setPrintFooter(false);
28
29    foreach ($attachments as $attachment) {
30      $label = self::applyTemplate($attachment, $labelTemplate);
31
32      $pdf->AddPage();
33      $pdf->Bookmark($label, 0, 0, '', 'B', array(0, 64, 128));
34
35      $pdf->MultiCell(0, 15, $label, 0, 'C');
36
37      $style = array(
38        'border' => 0,
39        'vpadding' => 0,
40        'hpadding' => 0,
41        'fgcolor' => array(0, 0, 0),
42        'bgcolor' => false, //array(255,255,255)
43        'module_width' => 1, // width of a single module in points
44        'module_height' => 1 // height of a single module in points
45      );
46      $pdf->write2DBarcode($attachment->attachmentId, 'QRCODE,L', 20, 20, 40, 40, $style, 'N');
47    }
48
49    return $pdf->Output('/* ignored */', 'S');
50  }
51
52  private static function applyTemplate(Attachment $attachment, ?string $labelTemplate = null): string {
53    return str_replace(
54      [
55        '%GROUP%',
56        '%TESTTAKER%',
57        '%BOOKLET%',
58        '%UNIT%',
59        '%VAR%',
60        '%LOGIN%',
61        '%CODE%'
62      ],
63      [
64        $attachment->_groupName,
65        $attachment->personLabel,
66        $attachment->_bookletName,
67        $attachment->_unitName,
68        $attachment->variableId,
69        $attachment->_loginName,
70        $attachment->_loginNameSuffix
71      ],
72      $labelTemplate ?? self::DEFAULT_LABEL_TEMPLATE
73    );
74  }
75}