Passed
Push — master ( e79061...af4049 )
by Bingo
03:12
created

ImageRenderer   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 11
dl 0
loc 23
ccs 12
cts 12
cp 1
rs 10
c 1
b 0
f 1
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 14 1
A __construct() 0 3 1
1
<?php
2
3
namespace PhpDocxTemplate\Twig\Impl;
4
5
use PhpDocxTemplate\PhpDocxTemplate;
6
use PhpDocxTemplate\Twig\RendererInterface;
7
8
class ImageRenderer implements RendererInterface
9
{
10
    private $template;
11
12 7
    public function __construct(PhpDocxTemplate $template)
13
    {
14 7
        $this->template = $template;
15 7
    }
16
17 1
    public function render(string $path, int $width, int $height): string
18
    {
19 1
        $docx = $this->template->getDocx();
20 1
        $preparedImageAttrs = $docx->prepareImageAttrs(['path' => $path, 'width' => $width, 'height' => $height]);
21 1
        $imgPath = $preparedImageAttrs['src'];
22
23
        // get image index
24 1
        $imgIndex = $docx->getNextRelationsIndex($path);
25 1
        $rid = 'rId' . $imgIndex;
26
27
        // replace preparations
28 1
        $docx->addImageToRelations($path, $rid, $imgPath, $preparedImageAttrs['mime']);
29 1
        $xmlImage = str_replace(array('{RID}', '{WIDTH}', '{HEIGHT}'), array($rid, $preparedImageAttrs['width'], $preparedImageAttrs['height']), $docx->getImageTemplate());
30 1
        return $xmlImage;
31
    }
32
}
33