Completed
Pull Request — master (#6)
by Dan
07:19
created

Drawer   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 9
dl 0
loc 161
ccs 50
cts 50
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A create() 0 4 1
A createImage() 0 8 1
A setFont() 0 5 1
A drawText() 0 8 1
A drawBox() 0 23 3
A drawBoxForText() 0 5 1
A getBoxFoxText() 0 13 4
A drawMask() 0 7 2
A getImage() 0 5 1
1
<?php
2
3
namespace SixtyNine\Cloud\Drawer;
4
5
use Imagine\Gd\Imagine;
6
use Imagine\Image\Box;
7
use Imagine\Image\Color;
8
use Imagine\Image\ImageInterface;
9
use Imagine\Image\Point;
10
use SixtyNine\Cloud\Model\Box as MyBox;
11
use SixtyNine\Cloud\Factory\FontsFactory;
12
use SixtyNine\Cloud\Usher\MaskInterface;
13
use Webmozart\Assert\Assert;
14
15
class Drawer
16
{
17
    /** @var \SixtyNine\Cloud\Factory\FontsFactory */
18
    protected $fontsFactory;
19
20
    /** @var \Imagine\Gd\Imagine */
21
    protected $imagine;
22
23
    /** @var ImageInterface */
24
    protected $image;
25
26
    /** @var string */
27
    protected $font;
28
29
    /**
30
     * @param FontsFactory $fontsFactory
31
     */
32 4
    protected function __construct(FontsFactory $fontsFactory)
33
    {
34 4
        $this->imagine = new Imagine();
35 4
        $this->fontsFactory = $fontsFactory;
36 4
    }
37
38
    /**
39
     * @return Drawer
40
     */
41 4
    public static function create(FontsFactory $fontsFactory)
42
    {
43 4
        return new self($fontsFactory);
44
    }
45
46
    /**
47
     * @param int $width
48
     * @param int $height
49
     * @param string $color
50
     * @return Drawer
51
     */
52 4
    public function createImage($width, $height, $color = '#FFFFFF')
53
    {
54 4
        $this->image = $this->imagine->create(
55 4
            new Box($width, $height),
56 4
            new Color($color)
57
        );
58 4
        return $this;
59
    }
60
61
    /**
62
     * @param string $fontName
63
     * @return Drawer
64
     */
65 4
    public function setFont($fontName)
66
    {
67 4
        $this->font = $fontName;
68 4
        return $this;
69
    }
70
71
    /**
72
     * @param int $x
73
     * @param int $y
74
     * @param string $text
75
     * @param int $size
76
     * @param string $color
77
     * @param int $angle
78
     * @throws \InvalidArgumentException
79
     * @return Drawer
80
     */
81 4
    public function drawText($x, $y, $text, $size, $color = '#000000', $angle = 0)
82
    {
83 4
        Assert::notNull($this->font, 'Font not set');
84 4
        $font = $this->fontsFactory->getImagineFont($this->font, $size, $color);
85 4
        $this->image->draw()->text($text, $font, new Point($x , $y), $angle);
86
87 4
        return $this;
88
    }
89
90
    /**
91
     * @param int $x
92
     * @param int $y
93
     * @param int $width
94
     * @param int $height
95
     * @param string $color
96
     * @return Drawer
97
     */
98 4
    public function drawBox($x, $y, $width, $height, $color = '#000000')
99
    {
100 4
        if ($x < 0) {
101 1
            $width += $x;
102 1
            $x = 0;
103
        }
104
105 4
        if ($y < 0) {
106 1
            $height += $y;
107 1
            $y = 0;
108
        }
109
110
        $points = array(
111 4
            new Point($x, $y),
112 4
            new Point($x + $width, $y),
113 4
            new Point($x + $width, $y + $height),
114 4
            new Point($x, $y + $height),
115
        );
116
117 4
        $this->image->draw()->polygon($points, new Color($color));
118
119 4
        return $this;
120
    }
121
122
    /**
123
     * @param int $x
124
     * @param int $y
125
     * @param int $width
126
     * @param int $height
127
     * @param int $angle
128
     * @param string $color
129
     * @return Drawer
130
     */
131 2
    public function drawBoxForText($x, $y, $width, $height, $angle, $color = '#000000')
132
    {
133 2
        $box = self::getBoxFoxText($x, $y, $width, $height, $angle);
134 2
        return $this->drawBox($box->getX(), $box->getY(), $box->getWidth(), $box->getHeight(), $color);
135
    }
136
137
    /**
138
     * @param int $x
139
     * @param int $y
140
     * @param int $width
141
     * @param int $height
142
     * @param int $angle
143
     * @return MyBox
144
     */
145 7
    public static function getBoxFoxText($x, $y, $width, $height, $angle)
146
    {
147
        switch ($angle) {
148 7
            case 90:
149 1
                return new MyBox($x, $y + $height, $height, $width);
150 7
            case 180:
151 1
                return new MyBox($x - $width, $y + $height, $width, $height);
152 7
            case 270:
153 5
                return new MyBox($x - $height, $y - $width + $height, $height, $width);
154
        }
155
156 6
        return new MyBox($x, $y, $width, $height);
157
    }
158
159 1
    public function drawMask(MaskInterface $mask, $color = '#ffffff')
160
    {
161 1
        foreach ($mask->getBoxes() as $box) {
162 1
            $this->drawBox($box->getX(), $box->getY(), $box->getWidth(), $box->getHeight(), $color);
163
        }
164 1
        return $this;
165
    }
166
167
    /**
168
     * @return ImageInterface
169
     */
170 4
    public function getImage()
171
    {
172 4
        Assert::notNull($this->image, 'Image not created');
173 4
        return $this->image;
174
    }
175
}
176