Completed
Pull Request — master (#19)
by John
05:56 queued 03:42
created

RendererDocument::render()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 22
nc 3
nop 3
1
<?php
2
3
namespace Graze\CiffRenderer\Renderer;
4
5
use Graze\CiffRenderer\Parser\ParserDocument;
6
use Graze\CiffRenderer\Renderer\FieldRenderer\FieldRendererFactory;
7
use Intervention\Image\ImageManager;
8
9
class RendererDocument
10
{
11
    /**
12
     * @var ParserDocument
13
     */
14
    private $parserDocument;
15
16
    /**
17
     * @var FieldRendererFactory
18
     */
19
    private $fieldRendererFactory;
20
21
    /**
22
     * @var ImageManager
23
     */
24
    private $imageManager;
25
26
    /**
27
     * @param ParserDocument $parserDocument
28
     * @param FieldRendererFactory $fieldRendererFactory
29
     * @param ImageManager $imageManager
30
     */
31
    public function __construct(
32
        ParserDocument $parserDocument,
33
        FieldRendererFactory $fieldRendererFactory,
34
        ImageManager $imageManager
35
    ) {
36
        $this->parserDocument = $parserDocument;
37
        $this->fieldRendererFactory = $fieldRendererFactory;
38
        $this->imageManager = $imageManager;
39
    }
40
41
    /**
42
     * @param \SimpleXMLElement $xml
43
     * @param callable $fontResolver
0 ignored issues
show
Documentation introduced by
Should the type for parameter $fontResolver not be null|callable?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
44
     * @param callable $graphicResolver
0 ignored issues
show
Documentation introduced by
Should the type for parameter $graphicResolver not be null|callable?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
45
     * @return Intervention\Image\Image
0 ignored issues
show
Documentation introduced by
Should the return type not be \Intervention\Image\Image?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
46
     */
47
    public function render(
48
        \SimpleXMLElement $xml,
49
        callable $fontResolver = null,
50
        callable $graphicResolver = null
51
    ) {
52
        $this->parserDocument->parse($xml);
53
54
        $canvas = $this->imageManager->canvas(
55
            $this->parserDocument->getWidth(),
56
            $this->parserDocument->getHeight(),
57
            '#fff'
58
        );
59
60
        foreach ($this->parserDocument->getFieldParsers() as $fieldParser) {
61
            if (!$fieldParser->isDisplayed()) {
62
                continue;
63
            }
64
65
            $renderer = $this->fieldRendererFactory->getFieldRenderer($fieldParser);
66
            $image = $renderer->render(
67
                $this->imageManager,
68
                $fieldParser,
69
                $fontResolver,
70
                $graphicResolver
71
            );
72
73
            // Intervention/Image requires dimensions as ints
74
            $positionX = (int) ($fieldParser->getPositionX());
75
            $positionY = (int) ($fieldParser->getPositionY());
76
77
            $canvas->insert($image, 'top-left', $positionX, $positionY);
78
        }
79
80
        return $canvas;
81
    }
82
83
    /**
84
     * @return RendererDocument
85
     */
86
    public static function factory()
87
    {
88
        return new static(
89
            ParserDocument::factory(),
90
            FieldRendererFactory::factory(),
91
            new ImageManager()
92
        );
93
    }
94
}
95