Passed
Push — sevtor ( dd1152 )
by Greg
21:38
created

TcpdfWrapper::Header()   F

Complexity

Conditions 16
Paths 528

Size

Total Lines 75
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 53
nc 528
nop 0
dl 0
loc 75
rs 2.0555
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2021 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Report;
21
22
use TCPDF;
23
use Fisharebest\Webtrees\Carbon;
24
25
/**
26
 * Class TcpdfWrapper
27
 */
28
class TcpdfWrapper extends TCPDF
29
{
30
    /**
31
     * Expose protected method in base class.
32
     *
33
     * @return float Return the remaining width
34
     */
35
    public function getRemainingWidth(): float
36
    {
37
        return parent::getRemainingWidth();
38
    }
39
40
    /**
41
     * Expose protected method in base class.
42
     *
43
     * @param mixed $h       Cell height. Default value: 0.
44
     * @param mixed $y       Starting y position, leave empty for current position.
45
     * @param bool  $add_page If true add a page, otherwise only return the true/false state
46
     *
47
     * @return boolean true in case of page break, false otherwise.
48
     */
49
    public function checkPageBreak($h = 0, $y = '', $add_page = true): bool
50
    {
51
        return parent::checkPageBreak($h, $y, $add_page);
52
    }
53
54
55
    //Page header
56
    public function Header()
57
    {
58
        $this->setHeaderTemplateAutoreset(true);
59
        $f = $this->getHeaderFont();
60
        $f[2] = 12;
61
        $this->setHeaderFont($f);
62
        $this->header_line_color = array(255,255,255);  // the line cuts through the pedigree chart in landscape mode
63
    // and no line gives a nicer look
64
65
        if ($this->header_xobjid === false) {
66
            // start a new XObject Template
67
            $this->header_xobjid = $this->startTemplate($this->w, $this->tMargin);
68
            $headerfont = $this->getHeaderFont();
69
            $headerdata = $this->getHeaderData();
70
            $this->y = $this->header_margin;
71
            if ($this->rtl) {
72
                $this->x = $this->w - $this->original_rMargin;
73
            } else {
74
                $this->x = $this->original_lMargin;
75
            }
76
            if (($headerdata['logo']) and ($headerdata['logo'] != K_BLANK_IMAGE)) {
77
                $imgtype = TCPDF_IMAGES::getImageFileType(K_PATH_IMAGES . $headerdata['logo']);
78
                if (($imgtype == 'eps') or ($imgtype == 'ai')) {
79
                    $this->ImageEps(K_PATH_IMAGES . $headerdata['logo'], '', '', $headerdata['logo_width']);
80
                } elseif ($imgtype == 'svg') {
81
                    $this->ImageSVG(K_PATH_IMAGES . $headerdata['logo'], '', '', $headerdata['logo_width']);
82
                } else {
83
                    $this->Image(K_PATH_IMAGES . $headerdata['logo'], '', '', $headerdata['logo_width']);
84
                }
85
                $imgy = $this->getImageRBY();
86
            } else {
87
                $imgy = $this->y;
88
            }
89
            $cell_height = $this->getCellHeight($headerfont[2] / $this->k);
90
            // set starting margin for text data cell
91
            if ($this->getRTL()) {
92
                $header_x = $this->original_rMargin + ($headerdata['logo_width'] * 1.1);
93
            } else {
94
                $header_x = $this->original_lMargin + ($headerdata['logo_width'] * 1.1);
95
            }
96
            $cw = $this->w - $this->original_lMargin - $this->original_rMargin - ($headerdata['logo_width'] * 1.1);
97
            $this->SetTextColorArray($this->header_text_color);
98
            // header title
99
            $this->SetFont($headerfont[0], 'B', $headerfont[2] + 1);
100
            $this->SetX($header_x);
101
            $this->Cell($cw, $cell_height, $headerdata['title'], 0, 1, 'C', 0, '', 0);
102
            if ($this->page == 1) {   // allows for a second line, not used (yet)
103
                // header string
104
                $this->SetFont($headerfont[0], $headerfont[1], $headerfont[2]);
105
                $this->SetX($header_x);
106
                $this->MultiCell($cw, $cell_height, $headerdata['string'], 0, '', 0, 1, '', '', true, 0, false, true, 0, 'T', false);
107
                $this->endTemplate();
108
            }
109
        }
110
111
        // print header template
112
        $xx = 0;
113
        if ($this->original_lMargin < 15) {
114
            $xx = 15;   // approx 5mm (15/72*25.4)  // my Lexmark printer masks ~2 mm at the paper edge
115
                    // only applicable on pedigree charts(?)
116
        }
117
        $dx = 0;
118
        if (!$this->header_xobj_autoreset and $this->booklet and (($this->page % 2) == 0)) {
119
            // adjust margins for booklet mode
120
            $dx = ($this->original_lMargin - $this->original_rMargin);
121
        }
122
        if ($this->rtl) {
123
            $x = $this->w + $dx;
124
        } else {
125
            $x = $xx + $dx;
126
        }
127
        $this->printTemplate($this->header_xobjid, $x, 0, 0, 0, '', '', false);
128
        if ($this->header_xobj_autoreset) {
129
            // reset header xobject template at each page
130
            $this->header_xobjid = false;
131
        }
132
    }
133
134
135
    // Page footer
136
    public function Footer()
137
    {
138
        // Set font
139
        $this->SetFont('helvetica', '', 8);
140
        // Position at 30 pt ~ 10 mm from bottom and 5 mm from left paper edge
141
        $this->SetXY(15, -30);
142
        $this->Cell(0, 11, $this->creator, 0, false, 'L', 0, '', 0, false, 'T', 'M');
143
        $this->SetXY(15, -30);
144
        $this->Cell(0, 11, Carbon::now()->local()->toDateTimeString(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
145
        $this->SetFont('helvetica', '', 10);
146
        $this->SetXY(15, -30);
147
        $this->Cell(0, 11, $this->getAliasNumPage() . '/' . $this->getAliasNbPages(), 0, false, 'R', 0, '', 0, false, 'T', 'M');
148
    }
149
}
150