Completed
Pull Request — master (#19)
by John
03:53 queued 02:04
created

FieldRendererFixedText::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 2
    public function __construct(Font $font)
25
    {
26 2
        $this->font = $font;
27 2
    }
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 1
    public function render(
37
        ImageManager $imageManager,
38
        FieldParserInterface $parser,
39
        callable $fontResolver = null,
40
        callable $graphicResolver = null
41
    ) {
42 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

42
        $fontPath = $fontResolver($parser->/** @scrutinizer ignore-call */ getFontFace());
Loading history...
43
44
        // split text into lines
45 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

45
        $lines = explode("\n", $parser->/** @scrutinizer ignore-call */ getText());
Loading history...
46 1
        $texts = [];
47 1
        $width = 0;
48 1
        $height = 0;
49 1
        foreach ($lines as $line) {
50 1
            $text = $this->renderLine($imageManager, $parser, $fontPath, $line);
51 1
            if (is_null($text)) {
52
                continue;
53
            }
54
55
            // store dimensions ready to create a canvas
56 1
            $width = max($width, $text->getWidth());
57 1
            $height += $text->getHeight();
58
59 1
            $texts[] = $text;
60 1
        }
61
62
        // create a canvas to add each line of text to
63 1
        $canvas = $imageManager->canvas($width, $height);
64
65 1
        $offsetX = 0;
66 1
        $offsetY = 0;
67 1
        foreach ($texts as $text) {
68
            // add the text to the canvas
69 1
            $canvas->insert($text, 'top-left', (int) $offsetX, (int) $offsetY);
70
71
            // increase the offset for the next line
72 1
            $offsetY += $text->getHeight();
73 1
        }
74
75 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

75
        /** @scrutinizer ignore-call */ 
76
        $orientation = $parser->getOrientation();
Loading history...
76 1
        if ($orientation) {
77 1
            $canvas->rotate(360 - $orientation);
78 1
        }
79
80 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...
81
    }
82
83
    /**
84
     * @param ImageManager $imageManager
85
     * @param FieldParserInterface $parser
86
     * @param string $fontPath
87
     * @param string $text
88
     * @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...
89
     */
90 1
    private function renderLine(ImageManager $imageManager, FieldParserInterface $parser, $fontPath, $text)
91
    {
92
        // create the text here so it can be measured
93 1
        $this->font->text($text);
94
95 1
        $this->font->file($fontPath);
96 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

96
        $this->font->size($parser->/** @scrutinizer ignore-call */ getFontSize());
Loading history...
97 1
        $this->font->color('#000');
98 1
        $this->font->valign('top');
99
100 1
        $size = $this->font->getBoxSize();
101
102
        // if text consists of invisible chars, measured size will be zero, nothing to print
103 1
        if ($size['width'] < 1 || $size['height'] < 1) {
104
            return null;
105
        }
106
107 1
        $fontCallback = function (&$font) {
108
            // override the font created by Canvas::text()
109 1
            $font = $this->font;
110 1
        };
111
112 1
        $canvas = $imageManager->canvas($size['width'], $size['height']);
113
114
        // no need to pass the text in here as we override the font in the callback
115 1
        $canvas->text('', null, null, $fontCallback);
116
117 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

117
        $canvas->resize(/** @scrutinizer ignore-type */ round($size['width'] * self::LETTER_SPACING_ADJUSTMENT), $size['height']);
Loading history...
118
119 1
        return $canvas;
120
    }
121
122
    /**
123
     * @return FieldParserInterface
124
     */
125 1
    public static function factory()
126
    {
127 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...
128 1
            new Font()
129 1
        );
130
    }
131
}
132