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

CloudBuilder::placeWords()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 13
cts 13
cp 1
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 12
nc 6
nop 1
crap 4
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\Usher;
17
use Webmozart\Assert\Assert;
18
19
class CloudBuilder
20
{
21
    /** @var WordsList */
22
    protected $list;
23
    /** @var int */
24
    protected $width = 800;
25
    /** @var int */
26
    protected $height = 600;
27
    /** @var string */
28
    protected $font;
29
    /** @var string */
30
    protected $backgroundColor = '#ffffff';
31
    /** @var FontSizeGeneratorInterface */
32
    protected $sizeGenerator;
33
    /** @var string */
34
    protected $placerName;
35
    /** @var int */
36
    protected $minFontSize = 10;
37
    /** @var int */
38
    protected $maxFontSize = 60;
39
    /** @var FontsFactory */
40
    protected $fontsFactory;
41
    /** @var bool */
42
    protected $precise = false;
43
44 4
    protected function __construct(FontsFactory $fontsFactory)
45
    {
46 4
        $this->fontsFactory = $fontsFactory;
47 4
    }
48
49
    /**
50
     * @return CloudBuilder
51
     */
52 4
    public static function create(FontsFactory $fontsFactory)
53
    {
54 4
        return new self($fontsFactory);
55
    }
56
57
    /**
58
     * @param int $width
59
     * @param int $height
60
     * @return CloudBuilder
61
     */
62 2
    public function setDimension($width, $height)
63
    {
64 2
        $this->width = $width;
65 2
        $this->height = $height;
66 2
        return $this;
67
    }
68
69 2
    public function setBackgroundColor($color)
70
    {
71 2
        $this->backgroundColor = $color;
72 2
        return $this;
73
    }
74
75 4
    public function setFont($name)
76
    {
77 4
        $this->font = $name;
78 4
        return $this;
79
    }
80
81
    public function setSizeGenerator(FontSizeGeneratorInterface $generator)
82
    {
83
        $this->sizeGenerator = $generator;
84
        return $this;
85
    }
86
87
    public function setFontSizes($minSize, $maxSize)
88
    {
89
        $this->minFontSize = $minSize;
90
        $this->maxFontSize = $maxSize;
91
        return $this;
92
    }
93
94 1
    public function setPlacer($name)
95
    {
96 1
        Assert::oneOf($name, PlacerFactory::getInstance()->getPlacersNames(), 'Placer not found: ' . $name);
97 1
        $this->placerName = $name;
98 1
        return $this;
99
    }
100
101
    public function setPrecise()
102
    {
103
        $this->precise = true;
104
    }
105
106 3
    public function useList(WordsList $list)
107
    {
108 3
        $this->list = $list;
109 3
        return $this;
110
    }
111
112 4
    public function build()
113
    {
114 4
        Assert::notNull($this->font, 'Font not set');
115
116 4
        $cloud = new Cloud();
117
        $cloud
118 4
            ->setFont($this->font)
119 4
            ->setWidth($this->width)
120 4
            ->setHeight($this->height)
121 4
            ->setBackgroundColor($this->backgroundColor)
122
        ;
123
124 4
        if ($this->list) {
125 3
            $this->addWords($cloud, $this->list);
126 3
            $this->placeWords($cloud);
127
        }
128
129 4
        return $cloud;
130
    }
131
132 3
    protected function addWords(Cloud $cloud, WordsList $list)
133
    {
134 3
        $words = $list->getWordsOrdered();
135 3
        $maxCount = $list->getWordsMaxCount();
136
137 3
        if (!$this->sizeGenerator) {
138 3
            $this->sizeGenerator = new LinearFontSizeGenerator();
139
        }
140
141
        /** @var Word $word */
142 3
        foreach ($words as $word) {
143 3
            $cloudWord = new CloudWord();
144
            $cloudWord
145 3
                ->setCloud($cloud)
146 3
                ->setPosition(array(0, 0))
147 3
                ->setSize($this->sizeGenerator->calculateFontSize($word->getCount(), $maxCount, $this->minFontSize, $this->maxFontSize))
148 3
                ->setAngle($word->getOrientation() === Word::DIR_VERTICAL ? 270 : 0)
149 3
                ->setColor($word->getColor())
150 3
                ->setText($word->getText())
151 3
                ->setIsVisible(true)
152
            ;
153 3
            $cloud->addWord($cloudWord);
154
        }
155
156 3
        return $this;
157
    }
158
159 3
    protected function placeWords(Cloud $cloud)
160
    {
161 3
        $placer = $this->placerName
162 1
            ? PlacerFactory::getInstance()->getPlacer($this->placerName, $cloud->getWidth(), $cloud->getHeight())
163 3
            : PlacerFactory::getInstance()->getDefaultPlacer($cloud->getWidth(), $cloud->getHeight())
164
        ;
165
166 3
        $metrics = new FontMetrics($this->fontsFactory);
167 3
        $usher = new Usher($cloud->getWidth(), $cloud->getHeight(), $placer, $metrics);
168
169
        /** @var CloudWord $word */
170 3
        foreach ($cloud->getWords() as $word) {
171
172 3
            $place = $usher->getPlace($word->getText(), $cloud->getFont(), $word->getSize(), $word->getAngle(), $this->precise);
173
174 3
            $word->setIsVisible((bool)$place);
175
176 3
            if ($place) {
177 3
                $word->setBox($place);
178 3
                $word->setPosition(array((int)$place->getX(), (int)$place->getY()));
179
            }
180
        }
181 3
    }
182
183
}
184