Completed
Push — refactor-04-parser-tests ( dc4950...bd4663 )
by John
06:06
created

RendererDocument::render()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3.0026

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 14
cts 15
cp 0.9333
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 21
nc 3
nop 3
crap 3.0026
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 FieldRendererFactory
13
     */
14
    private $fieldRendererFactory;
15
16
    /**
17
     * @var ImageManager
18
     */
19
    private $imageManager;
20
21
    /**
22
     * @param FieldRendererFactory $fieldRendererFactory
23
     * @param ImageManager $imageManager
24
     */
25 1
    public function __construct(
26
        FieldRendererFactory $fieldRendererFactory,
27
        ImageManager $imageManager
28
    ) {
29 1
        $this->fieldRendererFactory = $fieldRendererFactory;
30 1
        $this->imageManager = $imageManager;
31 1
    }
32
33
    /**
34
     * @param ParserDocument $parserDocument
35
     * @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...
36
     * @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...
37
     * @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...
38
     */
39 1
    public function render(
40
        ParserDocument $parserDocument,
41
        callable $fontResolver = null,
42
        callable $graphicResolver = null
43
    ) {
44 1
        $canvas = $this->imageManager->canvas(
45 1
            $parserDocument->getWidth(),
46 1
            $parserDocument->getHeight(),
47 1
            '#fff'
48
        );
49
50 1
        foreach ($parserDocument->getFieldParsers() as $fieldParser) {
51 1
            if (!$fieldParser->isDisplayed()) {
52
                continue;
53
            }
54
55 1
            $renderer = $this->fieldRendererFactory->getFieldRenderer($fieldParser);
56 1
            $image = $renderer->render(
57 1
                $this->imageManager,
58
                $fieldParser,
59
                $fontResolver,
60
                $graphicResolver
61
            );
62
63
            // Intervention/Image requires dimensions as ints
64 1
            $positionX = (int) ($fieldParser->getPositionX());
65 1
            $positionY = (int) ($fieldParser->getPositionY());
66
67 1
            $canvas->insert($image, 'top-left', $positionX, $positionY);
68
        }
69
70 1
        return $canvas;
71
    }
72
73
    /**
74
     * @return RendererDocument
75
     */
76
    public static function factory()
77
    {
78
        return new static(
79
            FieldRendererFactory::factory(),
80
            new ImageManager()
81
        );
82
    }
83
}
84