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

OverflowFixer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * File containing the {@see Mistralys\WidthsCalculator\Calculator\OverflowFixer} class.
4
 *
5
 * @package WidthsCalculator
6
 * @see Mistralys\WidthsCalculator\Calculator\OverflowFixer
7
 */
8
9
declare (strict_types=1);
10
11
namespace Mistralys\WidthsCalculator\Calculator;
12
13
use Mistralys\WidthsCalculator\Calculator;
14
15
/**
16
 * Handles converting values that are out of bounds to
17
 * values that the calculator can work with.
18
 *
19
 * @package WidthsCalculator
20
 * @author Sebastian Mordziol <[email protected]>
21
 */
22
class  OverflowFixer
23
{
24
   /**
25
    * @var Calculator
26
    */
27
    private $calculator;
28
    
29
   /**
30
    * @var Operations
31
    */
32
    private $operations;
33
    
34
    public function __construct(Calculator $calculator)
35
    {
36
        $this->calculator = $calculator;
37
        $this->operations = $calculator->getOperations();
38
    }
39
    
40
    public function fix() : void
41
    {
42
        $total = $this->operations->calcTotal();
43
        
44
        // to allow space for the missing columns, we base the
45
        // total target percentage on the amount of columns that
46
        // are not missing.
47
        $maxTotal = $this->calculator->getMaxTotal() / ($this->operations->countColumns() - $this->operations->countMissing());
48
        
49
        $cols = $this->calculator->getColumns();
50
        
51
        foreach($cols as $col)
52
        {
53
            // no change for missing columns, they get filled later
54
            if($col->isMissing())
55
            {
56
                continue;
57
            }
58
            
59
            $percentage = $col->getValue() * 100 / $total;
60
            $adjusted = floor($maxTotal * $percentage / 100);
61
            
62
            $col->setValue((int)$adjusted);
63
        }
64
    }
65
}
66