Passed
Push — master ( d91102...128247 )
by Dan
03:07
created

CloudRenderer::renderUsher()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 13
cts 13
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 5
nop 4
crap 4
1
<?php
2
3
namespace SixtyNine\Cloud\Renderer;
4
5
6
use Imagine\Gd\Imagine;
7
use Imagine\Image\Box;
8
use Imagine\Image\Color;
9
use Imagine\Image\ImageInterface;
10
use Imagine\Image\Point;
11
use SixtyNine\Cloud\Drawer\Drawer;
12
use SixtyNine\Cloud\Factory\FontsFactory;
13
use SixtyNine\Cloud\Model\Cloud;
14
use SixtyNine\Cloud\Placer\PlacerInterface;
15
16
class CloudRenderer
17
{
18
    /**
19
     * @param Cloud $cloud
20
     * @param \SixtyNine\Cloud\Factory\FontsFactory $fontsFactory
21
     * @param bool $drawBoundingBoxes
22
     * @return \Imagine\Gd\Image|\Imagine\Image\ImageInterface
23
     */
24 1
    public function render(Cloud $cloud, FontsFactory $fontsFactory, $drawBoundingBoxes = false)
25
    {
26 1
        $drawer = Drawer::create($fontsFactory)
27 1
            ->createImage($cloud->getWidth(), $cloud->getHeight(), $cloud->getBackgroundColor())
28 1
            ->setFont($cloud->getFont())
29
        ;
30
31
        /** @var \SixtyNine\Cloud\Model\CloudWord $word */
32 1
        foreach ($cloud->getWords() as $word) {
33
34 1
            if (!$word->getIsVisible()) {
35
                continue;
36
            }
37
38 1
            $pos = $word->getPosition();
39 1
            $box = $word->getBox();
40
41 1
            if ($word->getAngle() === 270) {
42 1
                $drawer->drawText($pos[0] + $box->getWidth(), $pos[1] + $box->getHeight() - $word->getSize(), $word->getText(), $word->getSize(), $word->getColor(), $word->getAngle());
43
            } else {
44 1
                $drawer->drawText($pos[0], $pos[1], $word->getText(), $word->getSize(), $word->getColor(), $word->getAngle());
45
            }
46
47 1
            if ($drawBoundingBoxes) {
48 1
                $drawer->drawBox($box->getX(), $box->getY(), $box->getWidth(), $box->getHeight(), '#ff0000');
49
            }
50
        }
51
52 1
        return $drawer->getImage();
53
    }
54
55
    /**
56
     * @param ImageInterface $image
57
     * @param PlacerInterface $placer
58
     * @param string $color
59
     * @param int $maxIterations
60
     */
61 3
    public function renderUsher(
62
        ImageInterface $image,
63
        PlacerInterface $placer,
64
        $color,
65
        $maxIterations = 5000
66
    ) {
67 3
        $i = 0;
68 3
        $cur = $placer->getFirstPlaceToTry();
69 3
        $color = new Color($color);
70
71 3
        while($cur) {
72
73 3
            $next = $placer->getNextPlaceToTry($cur);
74
75 3
            if ($next) {
76 3
                $image->draw()->line($cur, $next, $color);
77
            }
78
79 3
            $i++;
80 3
            $cur = $next;
81
82 3
            if ($i >= $maxIterations) {
83 1
                break;
84
            }
85
        }
86 3
    }
87
}
88