Completed
Push — master ( 875476...1855ce )
by Dan
02:56
created

Usher::getPlace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 8
nc 2
nop 4
crap 2
1
<?php
2
3
namespace SixtyNine\Cloud;
4
5
use Imagine\Image\PointInterface;
6
use SixtyNine\Cloud\Model\Box;
7
use SixtyNine\Cloud\Placer\PlacerInterface;
8
9
/**
10
 * Responsible to find a place for the word in the cloud
11
 */
12
13
class Usher
14
{
15
    const DEFAULT_MAX_TRIES = 100000;
16
17
    /** @var int */
18
    protected $maxTries;
19
20
    /** @var \SixtyNine\Cloud\SimpleMask */
21
    protected $mask;
22
23
    /** @var \SixtyNine\Cloud\Placer\PlacerInterface */
24
    protected $placer;
25
26
    /** @var \SixtyNine\Cloud\FontMetrics */
27
    protected $metrics;
28
29
    /**
30
     * @param int $imgWidth
31
     * @param int $imgHeight
32
     * @param PlacerInterface $placer
33
     * @param FontMetrics $metrics
34
     * @param int $maxTries
35
     */
36 4
    public function __construct(
37
        $imgWidth,
38
        $imgHeight,
39
        PlacerInterface $placer,
40
        FontMetrics $metrics,
41
        $maxTries = self::DEFAULT_MAX_TRIES
42
    ) {
43 4
        $this->mask = new SimpleMask();
44 4
        $this->metrics = $metrics;
45 4
        $this->imgHeight = $imgHeight;
0 ignored issues
show
Bug introduced by
The property imgHeight does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
46 4
        $this->imgWidth = $imgWidth;
0 ignored issues
show
Bug introduced by
The property imgWidth does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
47 4
        $this->maxTries = $maxTries;
48 4
        $this->placer = $placer;
49 4
    }
50
51
    /**
52
     * @param string $word
53
     * @param string $font
54
     * @param int $size
55
     * @param int $angle
56
     * @return bool|Box
57
     */
58 4
    public function getPlace($word, $font, $size, $angle)
59
    {
60 4
        $bounds = new Box(0, 0, $this->imgWidth, $this->imgHeight);
61 4
        $box = $this->metrics->calculateSize($word, $font, $size, $angle);
62 4
        $place = $this->searchPlace($bounds, $box);
63
64 4
        if ($place) {
65 4
            $this->mask->add($place->getPosition(), $box);
66 4
            return $place;
67
        }
68
69 1
        return false;
70
    }
71
72
    /**
73
     * Search a free place for a new box.
74
     * @param \SixtyNine\Cloud\Model\Box $bounds
75
     * @param \SixtyNine\Cloud\Model\Box $box
76
     * @return bool|Box
77
     */
78 4
    protected function searchPlace(Box $bounds, Box $box)
79
    {
80 4
        $place_found = false;
81 4
        $current = $this->placer->getFirstPlaceToTry();
82 4
        $curTry = 1;
83
84 4
        while (!$place_found) {
85
86 4
            if (!$current) {
87
                return false;
88
            }
89
90 4
            if ($curTry > $this->maxTries) {
91
                return false;
92
            }
93
94 4
            $currentBox = $box->move($current->getX(), $current->getY());
95 4
            $place_found = !$this->mask->overlaps($currentBox);
96
97 4
            if ($place_found) {
98 4
                break;
99
            }
100
101 4
            $current = $this->placer->getNextPlaceToTry($current);
102 4
            $curTry++;
103
        }
104
105 4
        $currentBox = $box->move($current->getX(), $current->getY());
106 4
        return $currentBox->inside($bounds) ? $currentBox : false;
107
    }
108
}
109