Test Failed
Push — master ( 1655fb...18f60e )
by Doug
04:26 queued 02:43
created

OrientatedItem::getSurfaceFootprint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Box packing (3D bin packing, knapsack problem).
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace DVDoug\BoxPacker;
10
11
use function min;
12
13
/**
14
 * An item to be packed.
15
 *
16
 * @author Doug Wright
17
 */
18
class OrientatedItem
19
{
20
    /**
21
     * @var Item
22
     */
23
    protected $item;
24
25
    /**
26
     * @var int
27
     */
28
    protected $width;
29
30
    /**
31
     * @var int
32
     */
33
    protected $length;
34
35
    /**
36
     * @var int
37
     */
38
    protected $depth;
39
40
    /**
41
     * Constructor.
42
     *
43
     * @param Item $item
44
     * @param int  $width
45
     * @param int  $length
46
     * @param int  $depth
47
     */
48 21
    public function __construct(Item $item, int $width, int $length, int $depth)
49
    {
50 21
        $this->item = $item;
51 21
        $this->width = $width;
52 21
        $this->length = $length;
53 21
        $this->depth = $depth;
54 21
    }
55
56
    /**
57
     * Item.
58
     *
59
     * @return Item
60
     */
61 21
    public function getItem(): Item
62
    {
63 21
        return $this->item;
64
    }
65
66
    /**
67
     * Item width in mm in it's packed orientation.
68
     *
69
     * @return int
70
     */
71 21
    public function getWidth(): int
72
    {
73 21
        return $this->width;
74
    }
75
76
    /**
77
     * Item length in mm in it's packed orientation.
78
     *
79
     * @return int
80
     */
81 21
    public function getLength(): int
82
    {
83 21
        return $this->length;
84
    }
85
86
    /**
87
     * Item depth in mm in it's packed orientation.
88
     *
89
     * @return int
90
     */
91 21
    public function getDepth(): int
92
    {
93 21
        return $this->depth;
94
    }
95
96
    /**
97
     * Calculate the surface footprint of the current orientation
98
     *
99
     * @return int
100
     */
101 18
    public function getSurfaceFootprint(): int
102
    {
103 18
        return $this->width * $this->length;
104
    }
105
106
    /**
107
     * Is this item stable (low centre of gravity), calculated as if the tipping point is >15 degrees.
108
     *
109
     * N.B. Assumes equal weight distribution.
110
     *
111
     * @return bool
112
     */
113 21
    public function isStable(): bool
114
    {
115 21
        return atan(min($this->getLength(), $this->getWidth()) / $this->getDepth()) > 0.261; //radians
116
    }
117
}
118