Completed
Push — master ( fe7bcf...9fd0f0 )
by Dan
02:31
created

Usher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 12
nc 1
nop 5
crap 1
1
<?php
2
3
namespace SixtyNine\Cloud;
4
5
use SixtyNine\Cloud\Model\Box;
6
use SixtyNine\Cloud\Placer\PlacerInterface;
7
8
/**
9
 * Responsible to find a place for the word in the cloud
10
 */
11
12
class Usher
13
{
14
    const DEFAULT_MAX_TRIES = 100000;
15
16
    /** @var int */
17
    protected $maxTries;
18
19
    /** @var \SixtyNine\Cloud\SimpleMask */
20
    protected $mask;
21
22
    /** @var \SixtyNine\Cloud\Placer\PlacerInterface */
23
    protected $placer;
24
25
    /** @var \SixtyNine\Cloud\FontMetrics */
26
    protected $metrics;
27
28
    /**
29
     * @param int $imgWidth
30
     * @param int $imgHeight
31
     * @param PlacerInterface $placer
32
     * @param FontMetrics $metrics
33
     * @param int $maxTries
34
     */
35 4
    public function __construct(
36
        $imgWidth,
37
        $imgHeight,
38
        PlacerInterface $placer,
39
        FontMetrics $metrics,
40
        $maxTries = self::DEFAULT_MAX_TRIES
41
    ) {
42 4
        $this->mask = new SimpleMask();
43 4
        $this->metrics = $metrics;
44 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...
45 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...
46 4
        $this->maxTries = $maxTries;
47 4
        $this->placer = $placer;
48 4
    }
49
50
    /**
51
     * @param string $word
52
     * @param string $font
53
     * @param int $size
54
     * @param int $angle
55
     * @return bool|Box
56
     */
57 4
    public function getPlace($word, $font, $size, $angle)
58
    {
59 4
        $bounds = new Box(0, 0, $this->imgWidth, $this->imgHeight);
60 4
        $box = $this->metrics->calculateSize($word, $font, $size, $angle);
61 4
        $place = $this->searchPlace($bounds, $box);
62
63 4
        if ($place) {
64 4
            $this->mask->add($place->getPosition(), $box);
65 4
            return $place;
66
        }
67
68 1
        return false;
69
    }
70
71
    /**
72
     * Search a free place for a new box.
73
     * @param \SixtyNine\Cloud\Model\Box $bounds
74
     * @param \SixtyNine\Cloud\Model\Box $box
75
     * @return bool|Box
76
     */
77 4
    protected function searchPlace(Box $bounds, Box $box)
78
    {
79 4
        $place_found = false;
80 4
        $current = $this->placer->getFirstPlaceToTry();
81 4
        $curTry = 1;
82
83 4
        while (!$place_found) {
84
85 4
            if (!$current) {
86 1
                return false;
87
            }
88
89 4
            if ($curTry > $this->maxTries) {
90
                return false;
91
            }
92
93 4
            $currentBox = $box->move($current->getX(), $current->getY());
94 4
            $place_found = !$this->mask->overlaps($currentBox);
95
96 4
            $place_found = $place_found &&  $currentBox->inside($bounds);
97
98 4
            if ($place_found) {
99 4
                break;
100
            }
101
102 4
            $current = $this->placer->getNextPlaceToTry($current);
103 4
            $curTry++;
104
        }
105
106 4
        $currentBox = $box->move($current->getX(), $current->getY());
107 4
        return $currentBox->inside($bounds) ? $currentBox : false;
108
    }
109
}
110