Completed
Pull Request — develop (#207)
by Franck
12:42 queued 05:23
created

AbstractDecoratorWriter::writeBorder()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 61
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 6

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 61
ccs 32
cts 32
cp 1
rs 8.6806
cc 6
eloc 35
nc 6
nop 3
crap 6

How to fix   Long Method   

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\PhpPresentation\Writer\PowerPoint2007;
4
5
use PhpOffice\Common\Drawing as CommonDrawing;
6
use PhpOffice\Common\XMLWriter;
7
use PhpOffice\PhpPresentation\Style\Border;
8
use PhpOffice\PhpPresentation\Style\Color;
9
use PhpOffice\PhpPresentation\Style\Fill;
10
use PhpOffice\PhpPresentation\Style\Outline;
11
12
abstract class AbstractDecoratorWriter extends \PhpOffice\PhpPresentation\Writer\AbstractDecoratorWriter
13
{
14
    /**
15
     * Write relationship
16
     *
17
     * @param  \PhpOffice\Common\XMLWriter $objWriter   XML Writer
18
     * @param  int                            $pId         Relationship ID. rId will be prepended!
19
     * @param  string                         $pType       Relationship type
20
     * @param  string                         $pTarget     Relationship target
21
     * @param  string                         $pTargetMode Relationship target mode
22
     * @throws \Exception
23
     */
24 97
    protected function writeRelationship(XMLWriter $objWriter, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '')
25
    {
26 97
        if ($pType == '' || $pTarget == '') {
27
            throw new \Exception("Invalid parameters passed.");
28
        }
29 97
        if (strpos($pId, 'rId') === false) {
30 97
            $pId = 'rId' . $pId;
31
        }
32
33
        // Write relationship
34 97
        $objWriter->startElement('Relationship');
35 97
        $objWriter->writeAttribute('Id', $pId);
36 97
        $objWriter->writeAttribute('Type', $pType);
37 97
        $objWriter->writeAttribute('Target', $pTarget);
38
39 97
        if ($pTargetMode != '') {
40 4
            $objWriter->writeAttribute('TargetMode', $pTargetMode);
41
        }
42
43 97
        $objWriter->endElement();
44 97
    }
45
46
    /**
47
     * Write Border
48
     *
49
     * @param  \PhpOffice\Common\XMLWriter $objWriter    XML Writer
50
     * @param  \PhpOffice\PhpPresentation\Style\Border     $pBorder      Border
51
     * @param  string                         $pElementName Element name
52
     * @throws \Exception
53
     */
54 35
    protected function writeBorder(XMLWriter $objWriter, $pBorder, $pElementName = 'L')
55
    {
56
        if (!($pBorder instanceof Border)) {
57 35
            return;
58 35
        }
59 9
60
        if ($pBorder->getLineStyle() == Border::LINE_NONE && $pElementName == '') {
61
            return;
62
        }
63 35
64
        // Line style
65
        $lineStyle = $pBorder->getLineStyle();
66 35
        if ($lineStyle == Border::LINE_NONE) {
67 35
            $lineStyle = Border::LINE_SINGLE;
68 35
        }
69 35
70 35
        // Line width
71
        $lineWidth = 12700 * $pBorder->getLineWidth();
72
73 35
        // a:ln $pElementName
74
        $objWriter->startElement('a:ln' . $pElementName);
75 9
        $objWriter->writeAttribute('w', $lineWidth);
76
        $objWriter->writeAttribute('cap', 'flat');
77
        $objWriter->writeAttribute('cmpd', $lineStyle);
78 35
        $objWriter->writeAttribute('algn', 'ctr');
79 35
80 35
        // Fill?
81
        if ($pBorder->getLineStyle() == Border::LINE_NONE) {
82
            // a:noFill
83
            $objWriter->writeElement('a:noFill', null);
84 35
        } else {
85 35
            // a:solidFill
86 35
            $objWriter->startElement('a:solidFill');
87
            $this->writeColor($objWriter, $pBorder->getColor());
88
            $objWriter->endElement();
89 35
        }
90
91
        // Dash
92 35
        $objWriter->startElement('a:prstDash');
93 35
        $objWriter->writeAttribute('val', $pBorder->getDashStyle());
94 35
        $objWriter->endElement();
95 35
96 35
        // a:round
97
        $objWriter->writeElement('a:round', null);
98
99 35
        // a:headEnd
100 35
        $objWriter->startElement('a:headEnd');
101 35
        $objWriter->writeAttribute('type', 'none');
102 35
        $objWriter->writeAttribute('w', 'med');
103 35
        $objWriter->writeAttribute('len', 'med');
104
        $objWriter->endElement();
105 35
106 35
        // a:tailEnd
107
        $objWriter->startElement('a:tailEnd');
108
        $objWriter->writeAttribute('type', 'none');
109
        $objWriter->writeAttribute('w', 'med');
110
        $objWriter->writeAttribute('len', 'med');
111
        $objWriter->endElement();
112
113 57
        $objWriter->endElement();
114
    }
115 57
116 56
    /**
117
     * @param XMLWriter $objWriter
118
     * @param Color $color
119
     * @param int|null $alpha
120 57
     */
121 57
    protected function writeColor(XMLWriter $objWriter, Color $color, $alpha = null)
122
    {
123
        if (is_null($alpha)) {
124 57
            $alpha = $color->getAlpha();
125 57
        }
126 57
127
        // a:srgbClr
128 57
        $objWriter->startElement('a:srgbClr');
129 57
        $objWriter->writeAttribute('val', $color->getRGB());
130
131
        // a:alpha
132
        $objWriter->startElement('a:alpha');
133
        $objWriter->writeAttribute('val', $alpha * 1000);
134
        $objWriter->endElement();
135
136
        $objWriter->endElement();
137
    }
138 54
139
    /**
140
     * Write Fill
141 54
     *
142 47
     * @param  \PhpOffice\Common\XMLWriter $objWriter XML Writer
143
     * @param  \PhpOffice\PhpPresentation\Style\Fill       $pFill     Fill style
144
     * @throws \Exception
145
     */
146 14
    protected function writeFill(XMLWriter $objWriter, Fill $pFill)
147 9
    {
148 9
        // Is it a fill?
149
        if ($pFill->getFillType() == Fill::FILL_NONE) {
150
            return;
151
        }
152 5
153
        // Is it a solid fill?
154 4
        if ($pFill->getFillType() == Fill::FILL_SOLID) {
155
            $this->writeSolidFill($objWriter, $pFill);
156
            return;
157 1
        }
158
159 5
        // Check if this is a pattern type or gradient type
160
        if ($pFill->getFillType() == Fill::FILL_GRADIENT_LINEAR || $pFill->getFillType() == Fill::FILL_GRADIENT_PATH) {
161
            // Gradient fill
162
            $this->writeGradientFill($objWriter, $pFill);
163
        } else {
164
            // Pattern fill
165
            $this->writePatternFill($objWriter, $pFill);
166
        }
167
    }
168 9
169
    /**
170
     * Write Solid Fill
171 9
     *
172 9
     * @param  \PhpOffice\Common\XMLWriter $objWriter XML Writer
173 9
     * @param  \PhpOffice\PhpPresentation\Style\Fill       $pFill     Fill style
174 9
     * @throws \Exception
175
     */
176
    protected function writeSolidFill(XMLWriter $objWriter, Fill $pFill)
177
    {
178
        // a:gradFill
179
        $objWriter->startElement('a:solidFill');
180
        $this->writeColor($objWriter, $pFill->getStartColor());
181
        $objWriter->endElement();
182
    }
183 4
184
    /**
185
     * Write Gradient Fill
186 4
     *
187
     * @param  \PhpOffice\Common\XMLWriter $objWriter XML Writer
188
     * @param  \PhpOffice\PhpPresentation\Style\Fill       $pFill     Fill style
189 4
     * @throws \Exception
190
     */
191 4
    protected function writeGradientFill(XMLWriter $objWriter, Fill $pFill)
192 4
    {
193 4
        // a:gradFill
194 4
        $objWriter->startElement('a:gradFill');
195
196
        // a:gsLst
197 4
        $objWriter->startElement('a:gsLst');
198 4
        // a:gs
199 4
        $objWriter->startElement('a:gs');
200 4
        $objWriter->writeAttribute('pos', '0');
201
        $this->writeColor($objWriter, $pFill->getStartColor());
202 4
        $objWriter->endElement();
203
204
        // a:gs
205 4
        $objWriter->startElement('a:gs');
206 4
        $objWriter->writeAttribute('pos', '100000');
207 4
        $this->writeColor($objWriter, $pFill->getEndColor());
208 4
        $objWriter->endElement();
209
210 4
        $objWriter->endElement();
211 4
212
        // a:lin
213
        $objWriter->startElement('a:lin');
214
        $objWriter->writeAttribute('ang', CommonDrawing::degreesToAngle($pFill->getRotation()));
215
        $objWriter->writeAttribute('scaled', '0');
216
        $objWriter->endElement();
217
218
        $objWriter->endElement();
219
    }
220 1
221
    /**
222
     * Write Pattern Fill
223 1
     *
224
     * @param  \PhpOffice\Common\XMLWriter $objWriter XML Writer
225
     * @param  \PhpOffice\PhpPresentation\Style\Fill       $pFill     Fill style
226 1
     * @throws \Exception
227
     */
228 1
    protected function writePatternFill(XMLWriter $objWriter, Fill $pFill)
229
    {
230 1
        // a:pattFill
231
        $objWriter->startElement('a:pattFill');
232
233 1
        // fgClr
234
        $objWriter->startElement('a:fgClr');
235 1
236
        $this->writeColor($objWriter, $pFill->getStartColor());
237 1
238
        $objWriter->endElement();
239 1
240 1
        // bgClr
241
        $objWriter->startElement('a:bgClr');
242
243
        $this->writeColor($objWriter, $pFill->getEndColor());
244
245
        $objWriter->endElement();
246
247 11
        $objWriter->endElement();
248
    }
249 11
250 11
    /**
251
     * Write Outline
252
     * @param XMLWriter $objWriter
253 3
     * @param Outline $oOutline
254
     */
255 3
    protected function writeOutline(XMLWriter $objWriter, $oOutline)
256
    {
257 3
        if (!$oOutline instanceof Outline) {
258
            return;
259
        }
260 3
        // Width : pts
261 3
        $width = $oOutline->getWidth();
262
        // Width : pts => px
263
        $width = CommonDrawing::pointsToPixels($width);
264 3
        // Width : px => emu
265
        $width = CommonDrawing::pixelsToEmu($width);
266
267 3
        // a:ln
268 3
        $objWriter->startElement('a:ln');
269
        $objWriter->writeAttribute('w', $width);
270
271
        // Fill
272
        $this->writeFill($objWriter, $oOutline->getFill());
273
274
        // > a:ln
275
        $objWriter->endElement();
276
    }
277
278
    /**
279
     * Determine absolute zip path
280
     *
281
     * @param  string $path
282
     * @return string
283
     */
284
    protected function absoluteZipPath($path)
285
    {
286
        $path      = str_replace(array(
287
            '/',
288
            '\\'
289
        ), DIRECTORY_SEPARATOR, $path);
290
        $parts     = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
291
        $absolutes = array();
292
        foreach ($parts as $part) {
293
            if ('.' == $part) {
294
                continue;
295
            }
296
            if ('..' == $part) {
297
                array_pop($absolutes);
298
            } else {
299
                $absolutes[] = $part;
300
            }
301
        }
302
303
        return implode('/', $absolutes);
304
    }
305
}
306