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

MissingFiller::calcPerColumn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 10
rs 10
1
<?php
2
/**
3
 * File containing the {@see Mistralys\WidthsCalculator\Calculator\MissingFiller} class.
4
 *
5
 * @package WidthsCalculator
6
 * @see Mistralys\WidthsCalculator\Calculator\MissingFiller
7
 */
8
9
declare (strict_types=1);
10
11
namespace Mistralys\WidthsCalculator\Calculator;
12
13
use Mistralys\WidthsCalculator\Calculator;
14
15
/**
16
 * Handles filling the missing column values with meaningful values.
17
 *
18
 * @package WidthsCalculator
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class MissingFiller
22
{
23
    /**
24
     * @var Calculator
25
     */
26
    private $calculator;
27
    
28
    /**
29
     * @var Operations
30
     */
31
    private $operations;
32
    
33
   /**
34
    * @var integer
35
    */
36
    private $missing = 0;
37
    
38
    public function __construct(Calculator $calculator)
39
    {
40
        $this->calculator = $calculator;
41
        $this->operations = $calculator->getOperations();
42
        $this->missing = $this->operations->countMissing();
43
    }
44
    
45
    public function fill() : void
46
    {
47
        if($this->missing === 0)
48
        {
49
            return;
50
        }
51
        
52
        $perColumn = $this->calcPerColumn();
53
        
54
        $this->applyToColumns($perColumn);
55
    }
56
    
57
    private function applyToColumns(float $perColumn) : void
58
    {
59
        $cols = $this->calculator->getColumns();
60
        
61
        foreach($cols as $col)
62
        {
63
            if($col->isMissing())
64
            {
65
                $col->setValue($perColumn);
66
            }
67
        }
68
    }
69
    
70
    private function calcPerColumn() : float
71
    {
72
        $toDistribute = $this->calculator->getMaxTotal() - $this->operations->calcTotal();
73
        
74
        if($toDistribute <= 0)
75
        {
76
            $toDistribute = $this->calculator->getMinWidth() * $this->missing;
77
        }
78
        
79
        return $toDistribute / $this->missing;
80
    }
81
}
82