Packed   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 4
dl 0
loc 111
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 34 5
B handleErrors() 0 27 5
A getBins() 0 4 1
A yieldBins() 0 6 2
A count() 0 4 1
1
<?php namespace BinPacking3d\Entity;
2
3
use BinPacking3d\Exception\CriticalException;
4
use BinPacking3d\Exception\WarningException;
5
6
/**
7
 * Class Packed
8
 * @package BinPacking3d\Entity
9
 */
10
class Packed
11
{
12
13
    /**
14
     * @var array
15
     */
16
    private $bins = array();
17
18
    /**
19
     * @param $response
20
     * @param Request $request
21
     * @throws \Exception
22
     */
23
    public function __construct(Response $response, Request $request)
24
    {
25
        // Parse response
26
        $response = $response->get()->response;
27
28
        // Handle errors when thrown
29
        if ($response->status !== 1 || !empty($response->errors)) {
30
            $this->handleErrors($response->errors);
31
        }
32
33
        // Loop over packed bins
34
        foreach ($response->bins_packed as $packedBin) {
35
            // Find original bin
36
            $originalBin = $request->getBin($packedBin->bin_data->id);
37
38
            // Loop over items
39
            foreach ($packedBin->items as $item) {
40
                $originalItem = $request->getItem($item->id);
41
                $originalBin->addItem($originalItem);
42
            }
43
44
            // Add used space
45
            $originalBin->setUsedSpace($packedBin->bin_data->used_space);
46
47
            // Add used weight
48
            $originalBin->setUsedWeight($packedBin->bin_data->used_weight);
49
50
            // Set image result
51
            $originalBin->setImage($packedBin->image_complete);
52
53
            // Add bin
54
            $this->bins[] = $originalBin;
55
        }
56
    }
57
58
    /**
59
     * Throw most severe error as exception
60
     *
61
     * @param $errors
62
     * @throws CriticalException
63
     * @throws WarningException
64
     * @throws \Exception
65
     */
66
    public function handleErrors($errors)
67
    {
68
        // Sort based on severity
69
        $order = ['critical', 'warning'];
70
        usort(
71
            $errors, function ($firstError, $secondError) use ($order) {
72
            if (array_search($firstError->level, $order) > array_search($secondError->level, $order)) {
73
                return -1;
74
            } elseif (array_search($firstError->level, $order) < array_search($secondError->level, $order)) {
75
                return 1;
76
            }
77
78
            return 0;
79
        }
80
        );
81
82
        // Throw first one as exception
83
        $error = array_shift($errors);
84
        switch ($error->level) {
85
            case 'critical':
86
                throw new CriticalException($error->message);
87
            case 'warning':
88
                throw new WarningException($error->message);
89
            default:
90
                throw new \Exception($error->message);
91
        }
92
    }
93
94
    /**
95
     * @return array
96
     */
97
    public function getBins()
98
    {
99
        return $this->bins;
100
    }
101
102
    /**
103
     * @return \Iterator
104
     */
105
    public function yieldBins()
106
    {
107
        foreach ($this->bins as $bin) {
108
            yield $bin;
109
        }
110
    }
111
112
    /**
113
     * @return int
114
     */
115
    public function count()
116
    {
117
        return count($this->bins);
118
    }
119
120
}
121