Passed
Push — master ( ede80c...25a1dd )
by Doug
02:13
created

InfalliblePacker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
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 7
    public function __construct()
27
    {
28 7
        $this->unpackedItems = new ItemList();
29 7
        parent::__construct();
30 7
    }
31
32
    /**
33
     * Return the items that couldn't be packed.
34
     *
35
     * @return ItemList
36
     */
37 2
    public function getUnpackedItems(): ItemList
38
    {
39 2
        return $this->unpackedItems;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 7
    public function pack(): PackedBoxList
46
    {
47 7
        $itemList = clone $this->items;
48
49 7
        while(true) {
50
            try {
51 7
                return parent::pack();
52 2
            } catch (ItemTooLargeException $e) {
53 2
                $this->unpackedItems->insert($e->getItem());
54 2
                $itemList->remove($e->getItem());
55 2
                $this->setItems($itemList);
56
            }
57
        }
58
    }
59
}
60