Completed
Push — master ( ad6ea5...120fc0 )
by Julien
19s queued 15s
created

ChromeRequest::setScale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TheCodingMachine\Gotenberg;
6
7
use function count;
8
9
abstract class ChromeRequest extends Request implements GotenbergRequestInterface
10
{
11
    private const WAIT_DELAY = 'waitDelay';
12
    private const PAPER_WIDTH = 'paperWidth';
13
    private const PAPER_HEIGHT = 'paperHeight';
14
    private const MARGIN_TOP = 'marginTop';
15
    private const MARGIN_BOTTOM  = 'marginBottom';
16
    private const MARGIN_LEFT = 'marginLeft';
17
    private const MARGIN_RIGHT = 'marginRight';
18
    private const LANDSCAPE = 'landscape';
19
    private const PAGE_RANGES = 'pageRanges';
20
    private const GOOGLE_CHROME_RPCC_BUFFER_SIZE = 'googleChromeRpccBufferSize';
21
    private const SCALE = 'scale';
22
23
    /** @var float|null */
24
    private $waitDelay;
25
26
    /** @var Document|null */
27
    private $header;
28
29
    /** @var Document|null */
30
    private $footer;
31
32
    /** @var float|null */
33
    private $paperWidth;
34
35
    /** @var float|null */
36
    private $paperHeight;
37
38
    /** @var float|null */
39
    private $marginTop;
40
41
    /** @var float|null */
42
    private $marginBottom;
43
44
    /** @var float|null */
45
    private $marginLeft;
46
47
    /** @var float|null */
48
    private $marginRight;
49
50
    /** @var bool */
51
    private $landscape;
52
53
    /** @var string|null */
54
    private $pageRanges;
55
56
    /** @var int|null */
57
    private $googleChromeRpccBufferSize;
58
59
    /** @var float|null */
60
    private $scale;
61
62
    /**
63
     * @return array<string,mixed>
64
     */
65
    public function getFormValues(): array
66
    {
67
        $values = parent::getFormValues();
68
        if ($this->waitDelay !== null) {
69
            $values[self::WAIT_DELAY] = $this->waitDelay;
70
        }
71
        if ($this->paperWidth !== null) {
72
            $values[self::PAPER_WIDTH] = $this->paperWidth;
73
        }
74
        if ($this->paperHeight !== null) {
75
            $values[self::PAPER_HEIGHT] = $this->paperHeight;
76
        }
77
        if ($this->marginTop !== null) {
78
            $values[self::MARGIN_TOP] = $this->marginTop;
79
        }
80
        if ($this->marginBottom !== null) {
81
            $values[self::MARGIN_BOTTOM] = $this->marginBottom;
82
        }
83
        if ($this->marginLeft !== null) {
84
            $values[self::MARGIN_LEFT] = $this->marginLeft;
85
        }
86
        if ($this->marginRight !== null) {
87
            $values[self::MARGIN_RIGHT] = $this->marginRight;
88
        }
89
        if ($this->pageRanges !== null) {
90
            $values[self::PAGE_RANGES] = $this->pageRanges;
91
        }
92
        if ($this->googleChromeRpccBufferSize !== null) {
93
            $values[self::GOOGLE_CHROME_RPCC_BUFFER_SIZE] = $this->googleChromeRpccBufferSize;
94
        }
95
        if ($this->scale !== null) {
96
            $values[self::SCALE] = $this->scale;
97
        }
98
        $values[self::LANDSCAPE] = $this->landscape;
99
100
        return $values;
101
    }
102
103
    /**
104
     * @return array<string,Document>
105
     */
106
    public function getFormFiles(): array
107
    {
108
        $files = [];
109
        if (! empty($this->header)) {
110
            $files['header.html'] = $this->header;
111
        }
112
        if (! empty($this->footer)) {
113
            $files['footer.html'] = $this->footer;
114
        }
115
116
        return $files;
117
    }
118
119
    public function setWaitDelay(?float $waitDelay): void
120
    {
121
        $this->waitDelay = $waitDelay;
122
    }
123
124
    /**
125
     * @param float[] $paperSize
126
     *
127
     * @throws RequestException
128
     */
129
    public function setPaperSize(array $paperSize): void
130
    {
131
        if (count($paperSize) !== 2) {
132
            throw new RequestException('Wrong paper size.');
133
        }
134
        $this->paperWidth = $paperSize[0];
135
        $this->paperHeight = $paperSize[1];
136
    }
137
138
    /**
139
     * @param float[] $margins
140
     *
141
     * @throws RequestException
142
     */
143
    public function setMargins(array $margins): void
144
    {
145
        if (count($margins) !== 4) {
146
            throw new RequestException('Wrong margins.');
147
        }
148
        $this->marginTop = $margins[0];
149
        $this->marginBottom = $margins[1];
150
        $this->marginLeft = $margins[2];
151
        $this->marginRight = $margins[3];
152
    }
153
154
    public function setHeader(?Document $header): void
155
    {
156
        $this->header = $header;
157
    }
158
159
    public function setFooter(?Document $footer): void
160
    {
161
        $this->footer = $footer;
162
    }
163
164
    public function setPaperWidth(?float $paperWidth): void
165
    {
166
        $this->paperWidth = $paperWidth;
167
    }
168
169
    public function setPaperHeight(?float $paperHeight): void
170
    {
171
        $this->paperHeight = $paperHeight;
172
    }
173
174
    public function setMarginTop(?float $marginTop): void
175
    {
176
        $this->marginTop = $marginTop;
177
    }
178
179
    public function setMarginBottom(?float $marginBottom): void
180
    {
181
        $this->marginBottom = $marginBottom;
182
    }
183
184
    public function setMarginLeft(?float $marginLeft): void
185
    {
186
        $this->marginLeft = $marginLeft;
187
    }
188
189
    public function setMarginRight(?float $marginRight): void
190
    {
191
        $this->marginRight = $marginRight;
192
    }
193
194
    public function setLandscape(bool $landscape): void
195
    {
196
        $this->landscape = $landscape;
197
    }
198
199
    public function setPageRanges(string $pageRanges): void
200
    {
201
        $this->pageRanges = $pageRanges;
202
    }
203
204
    public function setGoogleChromeRpccBufferSize(?int $googleChromeRpccBufferSize): void
205
    {
206
        $this->googleChromeRpccBufferSize = $googleChromeRpccBufferSize;
207
    }
208
209
    public function setScale(?float $scale): void
210
    {
211
        $this->scale = $scale;
212
    }
213
}
214