Completed
Push — master ( 39a8ae...07946a )
by Dan
02:41
created

Usher   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 97.06%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
c 3
b 0
f 0
lcom 1
cbo 5
dl 0
loc 98
ccs 33
cts 34
cp 0.9706
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A getPlace() 0 13 2
C searchPlace() 0 32 7
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
        $placeFound = false;
80 4
        $current = $this->placer->getFirstPlaceToTry();
81 4
        $curTry = 1;
82
83 4
        while (!$placeFound) {
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
            $placeFound = !$this->mask->overlaps($currentBox);
95
96 4
            $placeFound = $placeFound &&  $currentBox->inside($bounds);
97
98 4
            if ($placeFound) {
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