ContentPage   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 31
c 2
b 1
f 0
dl 0
loc 66
ccs 0
cts 40
cp 0
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generatePage() 0 34 6
A createImageTag() 0 19 4
1
<?php namespace Todaymade\Daux\Format\Confluence;
2
3
use Todaymade\Daux\Format\Base\EmbedImages;
4
use Todaymade\Daux\Tree\ComputedRaw;
5
use Todaymade\Daux\Tree\Entry;
6
use Todaymade\Daux\Tree\Raw;
7
8
class ContentPage extends \Todaymade\Daux\Format\Base\ContentPage
9
{
10
    public $attachments = [];
11
12
    protected function generatePage()
13
    {
14
        $content = parent::generatePage();
15
16
        //Embed images
17
        // We do it after generation so we can catch the images that were in html already
18
        $content = (new EmbedImages($this->params['tree']))
19
            ->embed(
20
                $content,
21
                $this->file,
22
                function($src, array $attributes, Entry $file) {
23
24
                    //Add the attachment for later upload
25
                    if ($file instanceof Raw) {
26
                        $filename = basename($file->getPath());
27
                        $this->attachments[$filename] = ['filename' => $filename, 'file' => $file];
28
                    } else if ($file instanceof ComputedRaw) {
29
                        $filename = $file->getUri();
30
                        $this->attachments[$filename] = ['filename' => $filename, 'content' => $file->getContent()];
31
                    } else {
32
                        throw new \RuntimeException("Cannot embed image as we don't understand its type.");
33
                    }
34
35
                    return $this->createImageTag($filename, $attributes);
36
                }
37
            );
38
39
40
        $intro = '';
41
        if (array_key_exists('confluence', $this->params) && array_key_exists('header', $this->params['confluence']) && !empty($this->params['confluence']['header'])) {
0 ignored issues
show
Bug introduced by Stéphane Goetz
$this->params of type Todaymade\Daux\Config is incompatible with the type array expected by parameter $search of array_key_exists(). ( Ignorable by Annotation )

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

41
        if (array_key_exists('confluence', /** @scrutinizer ignore-type */ $this->params) && array_key_exists('header', $this->params['confluence']) && !empty($this->params['confluence']['header'])) {
Loading history...
42
            $intro = '<ac:structured-macro ac:name="info"><ac:rich-text-body>' . $this->params['confluence']['header'] . '</ac:rich-text-body></ac:structured-macro>';
43
        }
44
45
        return $intro . $content;
46
    }
47
48
    /**
49
     * Create an image tag for the specified filename
50
     *
51
     * @param string $filename
52
     * @param array $attributes
53
     * @return string
54
     */
55
    private function createImageTag($filename, $attributes)
56
    {
57
        $img = '<ac:image';
58
59
        foreach ($attributes as $name => $value) {
60
            if ($name == 'style') {
61
                $re = '/float:\s*?(left|right);?/';
62
                if (preg_match($re, $value, $matches)) {
63
                    $img .= ' ac:align="' . $matches[1] . '"';
64
                    $value = preg_replace($re, "", $value, 1);
65
                }
66
            }
67
68
            $img .= ' ac:' . $name . '="' . htmlentities($value, ENT_QUOTES, 'UTF-8', false) . '"';
69
        }
70
71
        $img .= "><ri:attachment ri:filename=\"$filename\" /></ac:image>";
72
73
        return $img;
74
    }
75
}
76