Passed
Push — master ( 0692ec...4950c9 )
by Sebastian
02:38 queued 11s
created

SurplusRemover::processColumn()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 26
rs 9.8666
1
<?php
2
/**
3
 * File containing the {@see Mistralys\WidthsCalculator\Calculator\SurplusRemover} class.
4
 *
5
 * @package WidthsCalculator
6
 * @see Mistralys\WidthsCalculator\Calculator\SurplusRemover
7
 */
8
9
declare (strict_types=1);
10
11
namespace Mistralys\WidthsCalculator\Calculator;
12
13
use Mistralys\WidthsCalculator\Calculator;
14
15
/**
16
 * Handles subtracting values above the maximum total.
17
 * This can happen when there are missing values combined
18
 * with a minimum column width.
19
 *
20
 * @package WidthsCalculator
21
 * @author Sebastian Mordziol <[email protected]>
22
 */
23
class SurplusRemover
24
{
25
    /**
26
     * @var Calculator
27
     */
28
    private $calculator;
29
    
30
    /**
31
     * @var Operations
32
     */
33
    private $operations;
34
    
35
   /**
36
    * @var float
37
    */
38
    private $leftover = 0;
39
    
40
   /**
41
    * @var float
42
    */
43
    private $baseTotal = 0;
44
    
45
    public function __construct(Calculator $calculator)
46
    {
47
        $this->calculator = $calculator;
48
        $this->operations = $calculator->getOperations();
49
    }
50
    
51
    public function remove() : void
52
    {
53
        $this->leftover = $this->calculator->getMaxTotal() - $this->operations->calcTotal();
54
        
55
        if($this->leftover >= 0)
56
        {
57
            return;
58
        }
59
        
60
        $this->leftover = $this->leftover * -1; // we want a positive number
61
        $this->baseTotal = $this->operations->calcTotalNotMissing();
62
        $cols = $this->calculator->getColumns();
63
        
64
        foreach($cols as $col)
65
        {
66
            if(!$this->processColumn($col))
67
            {
68
                break;
69
            }
70
        }
71
        
72
        // There is some surplus left after the operation:
73
        // this means there were columns from which the 
74
        // surplus could not be removed because of the min
75
        // column width. 
76
        //
77
        // We simply run the removal again, to remove the 
78
        // surplus from the columns it can be removed from.
79
        if($this->leftover > 0)
80
        {
81
            $this->remove();
82
        }
83
    }
84
    
85
    private function processColumn(Column $col) : bool
86
    {
87
        if($col->isMissing())
88
        {
89
            return true;
90
        }
91
        
92
        if($this->leftover <= 0)
93
        {
94
            return false;
95
        }
96
        
97
        $percent = $col->getValue() * 100 / $this->baseTotal;
98
        $amount = round($this->leftover * $percent / 100);
99
        $val = $col->getValue() - $amount;
100
        
101
        if($val < $this->calculator->getMinWidth())
102
        {
103
            return true;
104
        }
105
        
106
        $this->leftover -= $amount;
107
        
108
        $col->setValue($val);
109
        
110
        return true;
111
    }
112
}
113