Completed
Push — master ( 1f4ad9...320f5c )
by John
03:15
created

FieldRendererFixedText::render()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 45
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 5.0014

Importance

Changes 0
Metric Value
cc 5
eloc 22
nc 12
nop 4
dl 0
loc 45
ccs 25
cts 26
cp 0.9615
crap 5.0014
rs 9.2568
c 0
b 0
f 0
1
<?php
2
3
namespace Graze\CiffRenderer\Renderer\FieldRenderer;
4
5
use Graze\CiffRenderer\Renderer\FieldRenderer\FieldRendererInterface;
6
use Graze\CiffRenderer\Parser\FieldParser\FieldParserInterface;
7
use Intervention\Image\ImageManager;
8
use Intervention\Image\Gd\Font;
9
10
class FieldRendererFixedText implements FieldRendererInterface
11
{
12
    /**
13
     * @var float
14
     */
15
    const LETTER_SPACING_ADJUSTMENT = 0.97;
16
17
    /**
18
     * @var Font
19
     */
20
    private $font;
21
22
    /**
23
     * @param Font $font
24
     */
25 2
    public function __construct(Font $font)
26
    {
27 2
        $this->font = $font;
28 2
    }
29
30
    /**
31
     * @param ImageManager $imageManager
32
     * @param FieldParserInterface $parser
33
     * @param callable $fontResolver
34
     * @param callable $graphicResolver
35
     * @return Intervention\Image\Image
0 ignored issues
show
Bug introduced by
The type Graze\CiffRenderer\Rende...ntervention\Image\Image was not found. Did you mean Intervention\Image\Image? If so, make sure to prefix the type with \.
Loading history...
36
     */
37 1
    public function render(
38
        ImageManager $imageManager,
39
        FieldParserInterface $parser,
40
        callable $fontResolver = null,
41
        callable $graphicResolver = null
42
    ) {
43 1
        $fontPath = $fontResolver($parser->getFontFace());
0 ignored issues
show
Bug introduced by
The method getFontFace() does not exist on Graze\CiffRenderer\Parse...er\FieldParserInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Graze\CiffRenderer\Parse...dParserGraphicPrimitive or Graze\CiffRenderer\Parse...ser\AbstractFieldParser or Graze\CiffRenderer\Parse...ieldParserStaticGraphic or Graze\CiffRenderer\Parse...dParserGraphicPrimitive or Graze\CiffRenderer\Parse...ieldParserStaticGraphic. Are you sure you never get one of those? ( Ignorable by Annotation )

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

43
        $fontPath = $fontResolver($parser->/** @scrutinizer ignore-call */ getFontFace());
Loading history...
44
45
        // split text into lines
46 1
        $lines = explode("\n", $parser->getText());
0 ignored issues
show
Bug introduced by
The method getText() does not exist on Graze\CiffRenderer\Parse...er\FieldParserInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Graze\CiffRenderer\Parse...dParserGraphicPrimitive or Graze\CiffRenderer\Parse...ser\AbstractFieldParser or Graze\CiffRenderer\Parse...ieldParserStaticGraphic or Graze\CiffRenderer\Parse...dParserGraphicPrimitive or Graze\CiffRenderer\Parse...ieldParserStaticGraphic. Are you sure you never get one of those? ( Ignorable by Annotation )

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

46
        $lines = explode("\n", $parser->/** @scrutinizer ignore-call */ getText());
Loading history...
47 1
        $texts = [];
48 1
        $width = 0;
49 1
        $height = 0;
50 1
        foreach ($lines as $line) {
51 1
            $text = $this->renderLine($imageManager, $parser, $fontPath, $line);
52 1
            if (is_null($text)) {
53
                continue;
54
            }
55
56
            // store dimensions ready to create a canvas
57 1
            $width = max($width, $text->getWidth());
58 1
            $height += $text->getHeight();
59
60 1
            $texts[] = $text;
61 1
        }
62
63
        // create a canvas to add each line of text to
64 1
        $canvas = $imageManager->canvas($width, $height);
65
66 1
        $offsetX = 0;
67 1
        $offsetY = 0;
68 1
        foreach ($texts as $text) {
69
            // add the text to the canvas
70 1
            $canvas->insert($text, 'top-left', (int) $offsetX, (int) $offsetY);
71
72
            // increase the offset for the next line
73 1
            $offsetY += $text->getHeight();
74 1
        }
75
76 1
        $orientation = $parser->getOrientation();
0 ignored issues
show
Bug introduced by
The method getOrientation() does not exist on Graze\CiffRenderer\Parse...er\FieldParserInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Graze\CiffRenderer\Parse...dParserGraphicPrimitive or Graze\CiffRenderer\Parse...ser\AbstractFieldParser or Graze\CiffRenderer\Parse...ieldParserStaticGraphic or Graze\CiffRenderer\Parse...dParserGraphicPrimitive or Graze\CiffRenderer\Parse...ieldParserStaticGraphic. Are you sure you never get one of those? ( Ignorable by Annotation )

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

76
        /** @scrutinizer ignore-call */ 
77
        $orientation = $parser->getOrientation();
Loading history...
77 1
        if ($orientation) {
78 1
            $canvas->rotate(360 - $orientation);
79 1
        }
80
81 1
        return $canvas;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $canvas returns the type Intervention\Image\Image which is incompatible with the documented return type Graze\CiffRenderer\Rende...ntervention\Image\Image.
Loading history...
82
    }
83
84
    /**
85
     * @param ImageManager $imageManager
86
     * @param FieldParserInterface $parser
87
     * @param string $fontPath
88
     * @param string $text
89
     * @return Intervention/Image/Image
0 ignored issues
show
Documentation Bug introduced by
The doc comment Intervention/Image/Image at position 0 could not be parsed: Unknown type name 'Intervention/Image/Image' at position 0 in Intervention/Image/Image.
Loading history...
90
     */
91 1
    private function renderLine(ImageManager $imageManager, FieldParserInterface $parser, $fontPath, $text)
92
    {
93
        // create the text here so it can be measured
94 1
        $this->font->text($text);
95
96 1
        $this->font->file($fontPath);
97 1
        $this->font->size($parser->getFontSize());
0 ignored issues
show
Bug introduced by
The method getFontSize() does not exist on Graze\CiffRenderer\Parse...er\FieldParserInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Graze\CiffRenderer\Parse...dParserGraphicPrimitive or Graze\CiffRenderer\Parse...ser\AbstractFieldParser or Graze\CiffRenderer\Parse...ieldParserStaticGraphic or Graze\CiffRenderer\Parse...dParserGraphicPrimitive or Graze\CiffRenderer\Parse...ieldParserStaticGraphic. Are you sure you never get one of those? ( Ignorable by Annotation )

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

97
        $this->font->size($parser->/** @scrutinizer ignore-call */ getFontSize());
Loading history...
98 1
        $this->font->color('#000');
99 1
        $this->font->valign('top');
100
101 1
        $size = $this->font->getBoxSize();
102
103
        // if text consists of invisible chars, measured size will be zero, nothing to print
104 1
        if ($size['width'] < 1 || $size['height'] < 1) {
105
            return null;
106
        }
107
108 1
        $fontCallback = function (&$font) {
109
            // override the font created by Canvas::text()
110 1
            $font = $this->font;
111 1
        };
112
113 1
        $canvas = $imageManager->canvas($size['width'], $size['height']);
114
115
        // no need to pass the text in here as we override the font in the callback
116 1
        $canvas->text('', null, null, $fontCallback);
117
118 1
        $canvas->resize(round($size['width'] * self::LETTER_SPACING_ADJUSTMENT), $size['height']);
0 ignored issues
show
Bug introduced by
round($size['width'] * s...TER_SPACING_ADJUSTMENT) of type double is incompatible with the type integer expected by parameter $width of Intervention\Image\Image::resize(). ( Ignorable by Annotation )

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

118
        $canvas->resize(/** @scrutinizer ignore-type */ round($size['width'] * self::LETTER_SPACING_ADJUSTMENT), $size['height']);
Loading history...
119
120 1
        return $canvas;
121
    }
122
123
    /**
124
     * @return FieldParserInterface
125
     */
126 1
    public static function factory()
127
    {
128 1
        return new static(
0 ignored issues
show
Bug Best Practice introduced by
The expression return new static(new In...ention\Image\Gd\Font()) returns the type Graze\CiffRenderer\Rende...\FieldRendererFixedText which is incompatible with the documented return type Graze\CiffRenderer\Parse...er\FieldParserInterface.
Loading history...
129 1
            new Font()
130 1
        );
131
    }
132
}
133