Passed
Push — 3.x ( e58d28...ec426a )
by Doug
01:41
created

InfalliblePacker   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 38
ccs 0
cts 14
cp 0
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getUnpackedItems() 0 3 1
A __construct() 0 4 1
A pack() 0 11 3
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
/**
12
 * A version of the packer that swallows internal exceptions.
13
 *
14
 * @author Doug Wright
15
 */
16
class InfalliblePacker extends Packer
17
{
18
    /**
19
     * @var ItemList
20
     */
21
    protected $unpackedItems;
22
23
    /**
24
     * InfalliblePacker constructor.
25
     */
26
    public function __construct()
27
    {
28
        $this->unpackedItems = new ItemList();
29
        parent::__construct();
30
    }
31
32
    /**
33
     * Return the items that couldn't be packed.
34
     */
35
    public function getUnpackedItems(): ItemList
36
    {
37
        return $this->unpackedItems;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function pack(): PackedBoxList
44
    {
45
        $itemList = clone $this->items;
46
47
        while (true) {
48
            try {
49
                return parent::pack();
50
            } catch (ItemTooLargeException $e) {
51
                $this->unpackedItems->insert($e->getItem());
52
                $itemList->remove($e->getItem());
53
                $this->setItems($itemList);
54
            }
55
        }
56
    }
57
}
58