Completed
Push — master ( 69e804...2cb5a5 )
by Dan
02:47
created

CloudBuilder::setSizeGenerator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
namespace SixtyNine\Cloud\Builder;
3
4
use Imagine\Gd\Font;
5
use Imagine\Image\Color;
6
use Imagine\Image\Point;
7
use SixtyNine\Cloud\Factory\FontsFactory;
8
use SixtyNine\Cloud\Factory\PlacerFactory;
9
use SixtyNine\Cloud\FontMetrics;
10
use SixtyNine\Cloud\FontSize\FontSizeGeneratorInterface;
11
use SixtyNine\Cloud\FontSize\LinearFontSizeGenerator;
12
use SixtyNine\Cloud\Model\Cloud;
13
use SixtyNine\Cloud\Model\CloudWord;
14
use SixtyNine\Cloud\Model\Word;
15
use SixtyNine\Cloud\Model\WordsList;
16
use SixtyNine\Cloud\Usher\MaskInterface;
17
use SixtyNine\Cloud\Usher\Usher;
18
use Webmozart\Assert\Assert;
19
20
class CloudBuilder
21
{
22
    /** @var WordsList */
23
    protected $list;
24
    /** @var int */
25
    protected $width = 800;
26
    /** @var int */
27
    protected $height = 600;
28
    /** @var string */
29
    protected $font;
30
    /** @var string */
31
    protected $backgroundColor = '#ffffff';
32
    /** @var FontSizeGeneratorInterface */
33
    protected $sizeGenerator;
34
    /** @var string */
35
    protected $placerName;
36
    /** @var int */
37
    protected $minFontSize = 10;
38
    /** @var int */
39
    protected $maxFontSize = 60;
40
    /** @var FontsFactory */
41
    protected $fontsFactory;
42
    /** @var bool */
43
    protected $precise = false;
44
    /** @var MaskInterface */
45
    protected $mask;
46
47
    /**
48
     * @param FontsFactory $fontsFactory
49
     */
50 4
    protected function __construct(FontsFactory $fontsFactory)
51
    {
52 4
        $this->fontsFactory = $fontsFactory;
53 4
    }
54
55
    /**
56
     * @param \SixtyNine\Cloud\Factory\FontsFactory $fontsFactory
57
     * @return CloudBuilder
58
     */
59 4
    public static function create(FontsFactory $fontsFactory)
60
    {
61 4
        return new self($fontsFactory);
62
    }
63
64
    /**
65
     * @param int $width
66
     * @param int $height
67
     * @return CloudBuilder
68
     */
69 2
    public function setDimension($width, $height)
70
    {
71 2
        $this->width = $width;
72 2
        $this->height = $height;
73 2
        return $this;
74
    }
75
76
    /**
77
     * @param string $color
78
     * @return $this
79
     */
80 2
    public function setBackgroundColor($color)
81
    {
82 2
        $this->backgroundColor = $color;
83 2
        return $this;
84
    }
85
86
    /**
87
     * @param string $name
88
     * @return $this
89
     */
90 4
    public function setFont($name)
91
    {
92 4
        $this->font = $name;
93 4
        return $this;
94
    }
95
96
    /**
97
     * @param FontSizeGeneratorInterface $generator
98
     * @return $this
99
     */
100
    public function setSizeGenerator(FontSizeGeneratorInterface $generator)
101
    {
102
        $this->sizeGenerator = $generator;
103
        return $this;
104
    }
105
106
    /**
107
     * @param int $minSize
108
     * @param int $maxSize
109
     * @return $this
110
     */
111
    public function setFontSizes($minSize, $maxSize)
112
    {
113
        $this->minFontSize = $minSize;
114
        $this->maxFontSize = $maxSize;
115
        return $this;
116
    }
117
118
    /**
119
     * @param string $name
120
     * @return $this
121
     */
122 1
    public function setPlacer($name)
123
    {
124 1
        Assert::oneOf($name, PlacerFactory::getInstance()->getPlacersNames(), 'Placer not found: ' . $name);
125 1
        $this->placerName = $name;
126 1
        return $this;
127
    }
128
129
    /**
130
     * @return $this
131
     */
132
    public function setPrecise()
133
    {
134
        $this->precise = true;
135
        return $this;
136
    }
137
138
    /**
139
     * @param WordsList $list
140
     * @return $this
141
     */
142 3
    public function useList(WordsList $list)
143
    {
144 3
        $this->list = $list;
145 3
        return $this;
146
    }
147
148
    /**
149
     * @return Cloud
150
     */
151 4
    public function build()
152
    {
153 4
        Assert::notNull($this->font, 'Font not set');
154
155 4
        $cloud = new Cloud();
156
        $cloud
157 4
            ->setFont($this->font)
158 4
            ->setWidth($this->width)
159 4
            ->setHeight($this->height)
160 4
            ->setBackgroundColor($this->backgroundColor)
161
        ;
162
163 4
        if ($this->list) {
164 3
            $this->addWords($cloud, $this->list);
165 3
            $this->mask = $this->placeWords($cloud);
166
        }
167
168 4
        return $cloud;
169
    }
170
171
    /**
172
     * @param Cloud $cloud
173
     * @param WordsList $list
174
     * @return $this
175
     */
176 3
    protected function addWords(Cloud $cloud, WordsList $list)
177
    {
178 3
        $words = $list->getWordsOrdered();
179 3
        $maxCount = $list->getWordsMaxCount();
180
181 3
        if (!$this->sizeGenerator) {
182 3
            $this->sizeGenerator = new LinearFontSizeGenerator();
183
        }
184
185
        /** @var Word $word */
186 3
        foreach ($words as $word) {
187 3
            $cloudWord = new CloudWord();
188
            $cloudWord
189 3
                ->setCloud($cloud)
190 3
                ->setPosition(array(0, 0))
191 3
                ->setSize($this->sizeGenerator->calculateFontSize($word->getCount(), $maxCount, $this->minFontSize, $this->maxFontSize))
192 3
                ->setAngle($word->getOrientation() === Word::DIR_VERTICAL ? 270 : 0)
193 3
                ->setColor($word->getColor())
194 3
                ->setText($word->getText())
195 3
                ->setIsVisible(true)
196
            ;
197 3
            $cloud->addWord($cloudWord);
198
        }
199
200 3
        return $this;
201
    }
202
203
    /**
204
     * @param Cloud $cloud
205
     * @return \SixtyNine\Cloud\Usher\Usher
206
     */
207 3
    protected function placeWords(Cloud $cloud)
208
    {
209 3
        $placer = $this->placerName
210 1
            ? PlacerFactory::getInstance()->getPlacer($this->placerName, $cloud->getWidth(), $cloud->getHeight())
211 3
            : PlacerFactory::getInstance()->getDefaultPlacer($cloud->getWidth(), $cloud->getHeight())
212
        ;
213
214 3
        $metrics = new FontMetrics($this->fontsFactory);
215 3
        $usher = new Usher($cloud->getWidth(), $cloud->getHeight(), $placer, $metrics);
216
217
        /** @var CloudWord $word */
218 3
        foreach ($cloud->getWords() as $word) {
219
220 3
            $place = $usher->getPlace($word->getText(), $cloud->getFont(), $word->getSize(), $word->getAngle(), $this->precise);
221
222 3
            $word->setIsVisible((bool)$place);
223
224 3
            if ($place) {
225 3
                $word->setBox($place);
226 3
                $word->setPosition(array((int)$place->getX(), (int)$place->getY()));
227
            }
228
        }
229
230 3
        return $usher->getMask();
231
    }
232
233
    /**
234
     * @return MaskInterface
235
     */
236
    public function getMask()
237
    {
238
        return $this->mask;
239
    }
240
}
241