Completed
Pull Request — master (#19)
by John
09:10 queued 04:43
created

FieldRendererFixedText::factory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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
    const FONT_SIZE_MULTIPLIER = 4.2;
13
14
    const LETTER_SPACING_ADJUSTMENT = 0.97;
15
16
    /**
17
     * @var Font
18
     */
19
    private $font;
20
21
    /**
22
     * @param Font $font
23
     */
24
    public function __construct(Font $font)
25
    {
26
        $this->font = $font;
27
    }
28
29
    /**
30
     * @param ImageManager $imageManager
31
     * @param FieldParserInterface $parser
32
     * @param callable $fontResolver
33
     * @param callable $graphicResolver
34
     * @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...
35
     */
36
    public function render(
37
        ImageManager $imageManager,
38
        FieldParserInterface $parser,
39
        callable $fontResolver = null,
40
        callable $graphicResolver = null
41
    ) {
42
        // create a canvas to add each line of text to
43
        $canvas = $imageManager->canvas(
44
            $parser->getWidth(),
45
            $parser->getHeight(),
46
            '#fff'
47
        );
48
49
        $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

49
        $fontPath = $fontResolver($parser->/** @scrutinizer ignore-call */ getFontFace());
Loading history...
50
51
        // split text into lines
52
        $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

52
        $lines = explode("\n", $parser->/** @scrutinizer ignore-call */ getText());
Loading history...
53
        $offsetX = 0;
54
        $offsetY = 0;
55
        foreach ($lines as $line) {
56
            $text = $this->renderLine($imageManager, $parser, $fontPath, $line);
57
            if (is_null($text)) {
58
                continue;
59
            }
60
61
            // add the text to the canvas
62
            $canvas->insert($text, 'top-left', (int) $offsetX, (int) $offsetY);
63
64
            // increase the offset for the next line
65
            $offsetY += $text->getHeight();
66
        }
67
68
        $oriontation = $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

68
        /** @scrutinizer ignore-call */ 
69
        $oriontation = $parser->getOrientation();
Loading history...
69
        if ($oriontation) {
70
            $canvas->rotate(360 - $oriontation);
71
        }
72
73
        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...
74
    }
75
    /**
76
     * @param ImageManager $imageManager
77
     * @param FieldParserInterface $parser
78
     * @param string $fontPath
79
     * @param string $text
80
     * @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...
81
     */
82
    private function renderLine(ImageManager $imageManager, FieldParserInterface $parser, $fontPath, $text)
83
    {
84
        // create the text here so it can be measured
85
        $fontOverride = new Font($text);
0 ignored issues
show
Bug introduced by
$text of type string is incompatible with the type Intervention\Image\Strinf expected by parameter $text of Intervention\Image\Gd\Font::__construct(). ( Ignorable by Annotation )

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

85
        $fontOverride = new Font(/** @scrutinizer ignore-type */ $text);
Loading history...
86
        $fontOverride->file($fontPath);
87
        $fontOverride->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

87
        $fontOverride->size($parser->/** @scrutinizer ignore-call */ getFontSize());
Loading history...
88
        $fontOverride->color('#000');
89
        $fontOverride->valign('top');
90
91
        // if text consists of invisible chars, measured size will be zero, nothing to print
92
        $size = $fontOverride->getBoxSize();
93
        if ($size['width'] < 1 || $size['height'] < 1) {
94
            return null;
95
        }
96
97
        $fontCallback = function (&$font) use ($fontOverride) {
98
            // override the font created by Canvas::text()
99
            $font = $fontOverride;
100
        };
101
102
        $canvas = $imageManager->canvas($size['width'], $size['height']);
103
104
        // no need to pass the text in here as we override the font in the callback
105
        $canvas->text('', null, null, $fontCallback);
106
        $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

106
        $canvas->resize(/** @scrutinizer ignore-type */ round($size['width'] * self::LETTER_SPACING_ADJUSTMENT), $size['height']);
Loading history...
107
108
        return $canvas;
109
    }
110
111
    /**
112
     * @return FieldParserInterface
113
     */
114
    public static function factory()
115
    {
116
        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...
117
            new Font()
118
        );
119
    }
120
}
121