Passed
Push — master ( 5f5e3f...bc1f8b )
by Dan
02:17
created

Mask::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
3
namespace SixtyNine\Cloud;
4
5
6
use Imagine\Image\BoxInterface;
7
use Imagine\Image\PointInterface;
8
9
class Mask
10
{
11
    private $drawnBoxes = array();
12
13
    /** {@inheritdoc} */
14 3
    public function add(PointInterface $pos, BoxInterface $box)
15
    {
16 3
        $this->drawnBoxes[] = array(
17 3
            'pos' => $pos,
18 3
            'box' => $box
19
        );
20 3
    }
21
22
    public function reset()
23
    {
24
        $this->drawnBoxes = array();
25
    }
26
27 3
    public function overlaps(PointInterface $pos, BoxInterface $box)
28
    {
29 3
        foreach ($this->drawnBoxes as $dBox) {
30 3
            if ($this->intersects($pos, $box, $dBox['pos'], $dBox['box'])) {
31 3
                return true;
32
            }
33
        }
34 3
        return false;
35
    }
36
37 3
    protected function intersects(
38
        PointInterface $pos1, BoxInterface $box1,
39
        PointInterface $pos2, BoxInterface $box2
40
    ) {
41 3
        if ($pos1->getY() + $box1->getHeight() < $pos2->getY()) {
42 2
            return false;
43
        }
44 3
        if ($pos1->getY() > $pos2->getY() + $box2->getHeight()) {
45 2
            return false;
46
        }
47 3
        if ($pos1->getX() > $pos2->getX() + $box2->getWidth()) {
48 1
            return false;
49
        }
50 3
        if ($pos1->getX() + $box1->getWidth() < $pos2->getX()) {
51 1
            return false;
52
        }
53
54 3
        return true;
55
    }
56
}
57