Passed
Push — 1.x-dev ( b2df86...afef98 )
by Doug
03:17
created

OrientatedItem   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 110
ccs 19
cts 21
cp 0.9048
rs 10
c 0
b 0
f 0
wmc 8

8 Methods

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