Completed
Pull Request — master (#1559)
by Adrien
09:36
created

PageSettings::pageSetup()   B

Complexity

Conditions 10
Paths 2

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 18
nc 2
nop 3
dl 0
loc 29
rs 7.6666
c 1
b 0
f 0

How to fix   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
namespace PhpOffice\PhpSpreadsheet\Reader\Xml;
4
5
use PhpOffice\PhpSpreadsheet\Spreadsheet;
6
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
7
use SimpleXMLElement;
8
use stdClass;
9
10
class PageSettings
11
{
12
    /**
13
     * @var stdClass
14
     */
15
    private $printSettings;
16
17
    public function __construct(SimpleXMLElement $xmlX, array $namespaces)
18
    {
19
        $printSettings = $this->pageSetup($xmlX, $namespaces, $this->getPrintDefaults());
20
        $this->printSettings = $this->printSetup($xmlX, $printSettings);
21
    }
22
23
    public function loadPageSettings(Spreadsheet $spreadsheet): void
24
    {
25
        $spreadsheet->getActiveSheet()->getPageSetup()
26
            ->setPaperSize($this->printSettings->paperSize)
27
            ->setOrientation($this->printSettings->orientation)
28
            ->setScale($this->printSettings->scale)
29
            ->setVerticalCentered($this->printSettings->verticalCentered)
30
            ->setHorizontalCentered($this->printSettings->horizontalCentered)
31
            ->setPageOrder($this->printSettings->printOrder);
32
        $spreadsheet->getActiveSheet()->getPageMargins()
33
            ->setTop($this->printSettings->topMargin)
34
            ->setHeader($this->printSettings->headerMargin)
35
            ->setLeft($this->printSettings->leftMargin)
36
            ->setRight($this->printSettings->rightMargin)
37
            ->setBottom($this->printSettings->bottomMargin)
38
            ->setFooter($this->printSettings->footerMargin);
39
    }
40
41
    private function getPrintDefaults(): stdClass
42
    {
43
        return (object) [
44
            'paperSize' => 9,
45
            'orientation' => PageSetup::ORIENTATION_DEFAULT,
46
            'scale' => 100,
47
            'horizontalCentered' => false,
48
            'verticalCentered' => false,
49
            'printOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER,
50
            'topMargin' => 0.75,
51
            'headerMargin' => 0.3,
52
            'leftMargin' => 0.7,
53
            'rightMargin' => 0.7,
54
            'bottomMargin' => 0.75,
55
            'footerMargin' => 0.3,
56
        ];
57
    }
58
59
    private function pageSetup(SimpleXMLElement $xmlX, array $namespaces, stdClass $printDefaults): stdClass
60
    {
61
        if (isset($xmlX->WorksheetOptions->PageSetup)) {
62
            foreach ($xmlX->WorksheetOptions->PageSetup as $pageSetupData) {
63
                foreach ($pageSetupData as $pageSetupKey => $pageSetupValue) {
64
                    $pageSetupAttributes = $pageSetupValue->attributes($namespaces['x']);
65
                    switch ($pageSetupKey) {
66
                        case 'Layout':
67
                            $this->setLayout($printDefaults, $pageSetupAttributes);
68
69
                            break;
70
                        case 'Header':
71
                            $printDefaults->headerMargin = (float) $pageSetupAttributes->Margin ?: 1.0;
72
73
                            break;
74
                        case 'Footer':
75
                            $printDefaults->footerMargin = (float) $pageSetupAttributes->Margin ?: 1.0;
76
77
                            break;
78
                        case 'PageMargins':
79
                            $this->setMargins($printDefaults, $pageSetupAttributes);
80
81
                            break;
82
                    }
83
                }
84
            }
85
        }
86
87
        return $printDefaults;
88
    }
89
90
    private function printSetup(SimpleXMLElement $xmlX, stdClass $printDefaults): stdClass
91
    {
92
        if (isset($xmlX->WorksheetOptions->Print)) {
93
            foreach ($xmlX->WorksheetOptions->Print as $printData) {
94
                foreach ($printData as $printKey => $printValue) {
95
                    switch ($printKey) {
96
                        case 'LeftToRight':
97
                            $printDefaults->printOrder = PageSetup::PAGEORDER_OVER_THEN_DOWN;
98
99
                            break;
100
                        case 'PaperSizeIndex':
101
                            $printDefaults->paperSize = (int) $printValue ?: 9;
102
103
                            break;
104
                        case 'Scale':
105
                            $printDefaults->scale = (int) $printValue ?: 100;
106
107
                            break;
108
                    }
109
                }
110
            }
111
        }
112
113
        return $printDefaults;
114
    }
115
116
    private function setLayout(stdClass $printDefaults, SimpleXMLElement $pageSetupAttributes): void
117
    {
118
        $printDefaults->orientation = (string) strtolower($pageSetupAttributes->Orientation) ?: PageSetup::ORIENTATION_PORTRAIT;
119
        $printDefaults->horizontalCentered = (bool) $pageSetupAttributes->CenterHorizontal ?: false;
120
        $printDefaults->verticalCentered = (bool) $pageSetupAttributes->CenterVertical ?: false;
121
    }
122
123
    private function setMargins(stdClass $printDefaults, SimpleXMLElement $pageSetupAttributes): void
124
    {
125
        $printDefaults->leftMargin = (float) $pageSetupAttributes->Left ?: 1.0;
126
        $printDefaults->rightMargin = (float) $pageSetupAttributes->Right ?: 1.0;
127
        $printDefaults->topMargin = (float) $pageSetupAttributes->Top ?: 1.0;
128
        $printDefaults->bottomMargin = (float) $pageSetupAttributes->Bottom ?: 1.0;
129
    }
130
}
131