Completed
Pull Request — develop (#565)
by
unknown
06:32
created

PptComments::writeSlideComments()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 19.4369

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 7
cts 25
cp 0.28
rs 8.425
c 0
b 0
f 0
cc 6
nc 9
nop 1
crap 19.4369

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\Adapter\Zip\ZipInterface;
6
use PhpOffice\Common\Drawing as CommonDrawing;
7
use PhpOffice\Common\XMLWriter;
8
use PhpOffice\PhpPresentation\Shape\Comment;
9
use PhpOffice\PhpPresentation\Slide;
10
11
class PptComments extends AbstractDecoratorWriter
12
{
13
    /**
14
     * @return ZipInterface
15
     * @throws \Exception
16
     */
17 4
    public function render()
18
    {
19 4
        foreach ($this->getPresentation()->getAllSlides() as $numSlide => $oSlide) {
20 4
            $contentXml = $this->writeSlideComments($oSlide);
21 4
            if (empty($contentXml)) {
22 4
                continue;
23
            }
24
            $this->getZip()->addFromString('ppt/comments/comment'.($numSlide + 1).'.xml', $contentXml);
25
        }
26 4
        return $this->getZip();
27
    }
28
29
    /**
30
     * @param Slide $oSlide
31
     * @return string
32
     */
33 4
    protected function writeSlideComments(Slide $oSlide)
34
    {
35
        /**
36
         * @var Comment[]
37
         */
38 4
        $arrayComment = array();
39 4
        foreach ($oSlide->getShapeCollection() as $oShape) {
40 4
            if ($oShape instanceof Comment) {
41 4
                $arrayComment[] = $oShape;
42
            }
43
        }
44
45 4
        if (empty($arrayComment)) {
46 4
            return '';
47
        }
48
49
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
50
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
51
52
        // p:cmLst
53
        $objWriter->startElement('p:cmLst');
54
        $objWriter->writeAttribute('xmlns:p', 'http://schemas.openxmlformats.org/presentationml/2006/main');
55
56
        foreach ($arrayComment as $idxComment => $oComment) {
57
            $oAuthor = $oComment->getAuthor();
58
59
            // p:cmLst > p:cm
60
            $objWriter->startElement('p:cm');
61
            $objWriter->writeAttribute('authorId', $oAuthor instanceof Comment\Author ? $oAuthor->getIndex() : 0);
62
            $objWriter->writeAttribute('dt', date('Y-m-d\TH:i:s.000000000', $oComment->getDate()));
63
            $objWriter->writeAttribute('idx', $idxComment);
64
65
            // p:cmLst > p:cm > p:pos
66
            // Uses 1/8pt for positionning
67
            // @link : https://social.msdn.microsoft.com/Forums/fr-FR/ebdc12f2-0cff-4fa8-b901-fa6e3198364e/comment-position-units
68
            $objWriter->startElement('p:pos');
69
            $objWriter->writeAttribute('x', (int) CommonDrawing::pixelsToPoints($oComment->getOffsetX() * 8));
70
            $objWriter->writeAttribute('y', (int) CommonDrawing::pixelsToPoints($oComment->getOffsetY() * 8));
71
            $objWriter->endElement();
72
73
            // p:cmLst > p:cm > p:text
74
            $objWriter->writeElement('p:text', $oComment->getText());
75
76
            // p:cmLst > ## p:cm
77
            $objWriter->endElement();
78
        }
79
80
        // ## p:cmLst
81
        $objWriter->endElement();
82
83
        return $objWriter->getData();
84
    }
85
}
86