Test Failed
Push — master ( a0e7d0...b649da )
by Dan
03:02
created

Usher   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 91.18%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
lcom 1
cbo 7
dl 0
loc 119
ccs 31
cts 34
cp 0.9118
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A getPlace() 0 17 3
A addWordToMask() 0 16 3
C searchPlace() 0 32 7
1
<?php
2
3
namespace SixtyNine\Cloud\Usher;
4
5
use Imagine\Image\Point;
6
use SixtyNine\Cloud\FontMetrics;
7
use SixtyNine\Cloud\Model\Box;
8
use SixtyNine\Cloud\Placer\PlacerInterface;
9
10
/**
11
 * Responsible to find a place for the word in the cloud
12
 */
13
14
class Usher
15
{
16
    const DEFAULT_MAX_TRIES = 100000;
17
18
    /** @var int */
19
    protected $maxTries;
20
21
    /** @var \SixtyNine\Cloud\Usher\MaskInterface */
22
    protected $mask;
23
24
    /** @var \SixtyNine\Cloud\Placer\PlacerInterface */
25
    protected $placer;
26
27
    /** @var \SixtyNine\Cloud\FontMetrics */
28
    protected $metrics;
29
30
    /**
31
     * @param int $imgWidth
32
     * @param int $imgHeight
33
     * @param PlacerInterface $placer
34
     * @param FontMetrics $metrics
35
     * @param int $maxTries
36 4
     */
37
    public function __construct(
38
        $imgWidth,
39
        $imgHeight,
40
        PlacerInterface $placer,
41
        FontMetrics $metrics,
42
        $maxTries = self::DEFAULT_MAX_TRIES
43
    ) {
44 4
        $this->mask = new QuadTreeMask($imgWidth, $imgHeight);
45 4
        $this->metrics = $metrics;
46 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...
47 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...
48 4
        $this->maxTries = $maxTries;
49 4
        $this->placer = $placer;
50 4
    }
51
52
    /**
53
     * @param string $word
54
     * @param string $font
55
     * @param int $size
56
     * @param int $angle
57
     * @return bool|Box
58
     */
59 4
    public function getPlace($word, $font, $size, $angle, $precise = true)
60
    {
61 4
        $bounds = new Box(0, 0, $this->imgWidth, $this->imgHeight);
62 4
        $box = $this->metrics->calculateSize($word, $font, $size, $angle);
63 4
        $place = $this->searchPlace($bounds, $box);
64
65 4
        if ($place) {
66 4
            if ($precise) {
67 4
                $this->addWordToMask($word, $place, $font, $size, $angle);
68
            } else {
69
                $this->mask->add($place->getPosition(), $box);
70
            }
71
            return $place;
72
        }
73
74
        return false;
75
    }
76
77
    public function addWordToMask($word, Box $place, $font, $size, $angle)
78
    {
79 4
        $base = $this->metrics->calculateSize($word, $font, $size, $angle);
80
        foreach (str_split($word) as $letter) {
81 4
            $box = $this->metrics->calculateSize($letter, $font, $size, $angle);
82 4
            $newPos = new Point($place->getX(), $place->getY() + ($base->getHeight() - $box->getHeight()));
83 4
            $this->mask->add($newPos, $box);
84
85 4
            if ($angle === 0) {
86
                $place = $place->move($box->getWidth(), 0);
87 4
            } else {
88
                // FIXME: this is wrong
89
                $place = $place->move(0, $box->getHeight());
90
            }
91 4
        }
92
    }
93
94
    /**
95 4
     * Search a free place for a new box.
96 4
     * @param \SixtyNine\Cloud\Model\Box $bounds
97
     * @param \SixtyNine\Cloud\Model\Box $box
98 4
     * @return bool|Box
99
     */
100 4
    protected function searchPlace(Box $bounds, Box $box)
101 4
    {
102
        $placeFound = false;
103
        $current = $this->placer->getFirstPlaceToTry();
104 4
        $curTry = 1;
105 4
106
        while (!$placeFound) {
107
108 4
            if (!$current) {
109 4
                return false;
110
            }
111
112
            if ($curTry > $this->maxTries) {
113
                return false;
114
            }
115
116
            $currentBox = $box->move($current->getX(), $current->getY());
117
            $placeFound = !$this->mask->overlaps($currentBox);
118
119
            $placeFound = $placeFound &&  $currentBox->inside($bounds);
120
121
            if ($placeFound) {
122
                break;
123
            }
124
125
            $current = $this->placer->getNextPlaceToTry($current);
126
            $curTry++;
127
        }
128
129
        $currentBox = $box->move($current->getX(), $current->getY());
130
        return $currentBox->inside($bounds) ? $currentBox : false;
131
    }
132
}
133