Failed Conditions
Push — master ( 11f758...e55052 )
by Adrien
18:18 queued 05:56
created

PageSettings::setMargins()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 4
nc 1
nop 2
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 5
rs 9.6111
c 1
b 0
f 0
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 13
    public function __construct(SimpleXMLElement $xmlX, array $namespaces)
18
    {
19 13
        $printSettings = $this->pageSetup($xmlX, $namespaces, $this->getPrintDefaults());
20 13
        $this->printSettings = $this->printSetup($xmlX, $printSettings);
21 13
    }
22
23 13
    public function loadPageSettings(Spreadsheet $spreadsheet): void
24
    {
25 13
        $spreadsheet->getActiveSheet()->getPageSetup()
26 13
            ->setPaperSize($this->printSettings->paperSize)
27 13
            ->setOrientation($this->printSettings->orientation)
28 13
            ->setScale($this->printSettings->scale)
29 13
            ->setVerticalCentered($this->printSettings->verticalCentered)
30 13
            ->setHorizontalCentered($this->printSettings->horizontalCentered)
31 13
            ->setPageOrder($this->printSettings->printOrder);
32 13
        $spreadsheet->getActiveSheet()->getPageMargins()
33 13
            ->setTop($this->printSettings->topMargin)
34 13
            ->setHeader($this->printSettings->headerMargin)
35 13
            ->setLeft($this->printSettings->leftMargin)
36 13
            ->setRight($this->printSettings->rightMargin)
37 13
            ->setBottom($this->printSettings->bottomMargin)
38 13
            ->setFooter($this->printSettings->footerMargin);
39 13
    }
40
41 13
    private function getPrintDefaults(): stdClass
42
    {
43
        return (object) [
44 13
            '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 13
    private function pageSetup(SimpleXMLElement $xmlX, array $namespaces, stdClass $printDefaults): stdClass
60
    {
61 13
        if (isset($xmlX->WorksheetOptions->PageSetup)) {
62 12
            foreach ($xmlX->WorksheetOptions->PageSetup as $pageSetupData) {
63 12
                foreach ($pageSetupData as $pageSetupKey => $pageSetupValue) {
64 12
                    $pageSetupAttributes = $pageSetupValue->attributes($namespaces['x']);
1 ignored issue
show
Bug introduced by
The method attributes() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
                    /** @scrutinizer ignore-call */ 
65
                    $pageSetupAttributes = $pageSetupValue->attributes($namespaces['x']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65 12
                    if (!$pageSetupAttributes) {
66
                        continue;
67
                    }
68
69
                    switch ($pageSetupKey) {
70 12
                        case 'Layout':
71 2
                            $this->setLayout($printDefaults, $pageSetupAttributes);
72
73 2
                            break;
74 12
                        case 'Header':
75 12
                            $printDefaults->headerMargin = (float) $pageSetupAttributes->Margin ?: 1.0;
76
77 12
                            break;
78 12
                        case 'Footer':
79 12
                            $printDefaults->footerMargin = (float) $pageSetupAttributes->Margin ?: 1.0;
80
81 12
                            break;
82 12
                        case 'PageMargins':
83 12
                            $this->setMargins($printDefaults, $pageSetupAttributes);
84
85 12
                            break;
86
                    }
87
                }
88
            }
89
        }
90
91 13
        return $printDefaults;
92
    }
93
94 13
    private function printSetup(SimpleXMLElement $xmlX, stdClass $printDefaults): stdClass
95
    {
96 13
        if (isset($xmlX->WorksheetOptions->Print)) {
97 11
            foreach ($xmlX->WorksheetOptions->Print as $printData) {
98 11
                foreach ($printData as $printKey => $printValue) {
99
                    switch ($printKey) {
100 11
                        case 'LeftToRight':
101 2
                            $printDefaults->printOrder = PageSetup::PAGEORDER_OVER_THEN_DOWN;
102
103 2
                            break;
104 11
                        case 'PaperSizeIndex':
105 2
                            $printDefaults->paperSize = (int) $printValue ?: 9;
106
107 2
                            break;
108 11
                        case 'Scale':
109 2
                            $printDefaults->scale = (int) $printValue ?: 100;
110
111 2
                            break;
112
                    }
113
                }
114
            }
115
        }
116
117 13
        return $printDefaults;
118
    }
119
120 2
    private function setLayout(stdClass $printDefaults, SimpleXMLElement $pageSetupAttributes): void
121
    {
122 2
        $printDefaults->orientation = (string) strtolower($pageSetupAttributes->Orientation ?? '') ?: PageSetup::ORIENTATION_PORTRAIT;
123 2
        $printDefaults->horizontalCentered = (bool) $pageSetupAttributes->CenterHorizontal ?: false;
124 2
        $printDefaults->verticalCentered = (bool) $pageSetupAttributes->CenterVertical ?: false;
125 2
    }
126
127 12
    private function setMargins(stdClass $printDefaults, SimpleXMLElement $pageSetupAttributes): void
128
    {
129 12
        $printDefaults->leftMargin = (float) $pageSetupAttributes->Left ?: 1.0;
130 12
        $printDefaults->rightMargin = (float) $pageSetupAttributes->Right ?: 1.0;
131 12
        $printDefaults->topMargin = (float) $pageSetupAttributes->Top ?: 1.0;
132 12
        $printDefaults->bottomMargin = (float) $pageSetupAttributes->Bottom ?: 1.0;
133 12
    }
134
}
135