MissingFiller   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 48
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fill() 0 10 2
A calcPerColumn() 0 10 2
A applyToColumns() 0 9 3
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
    private Calculator $calculator;
24
    private Operations $operations;
25
    private int $missing;
26
    
27
    public function __construct(Calculator $calculator)
28
    {
29
        $this->calculator = $calculator;
30
        $this->operations = $calculator->getOperations();
31
        $this->missing = $this->operations->countMissing();
32
    }
33
    
34
    public function fill() : void
35
    {
36
        if($this->missing === 0)
37
        {
38
            return;
39
        }
40
        
41
        $perColumn = $this->calcPerColumn();
42
        
43
        $this->applyToColumns($perColumn);
44
    }
45
    
46
    private function applyToColumns(float $perColumn) : void
47
    {
48
        $cols = $this->calculator->getColumns();
49
        
50
        foreach($cols as $col)
51
        {
52
            if($col->isMissing())
53
            {
54
                $col->setValue($perColumn);
55
            }
56
        }
57
    }
58
    
59
    private function calcPerColumn() : float
60
    {
61
        $toDistribute = $this->calculator->getMaxTotal() - $this->operations->calcTotal();
62
        
63
        if($toDistribute <= 0)
64
        {
65
            $toDistribute = $this->calculator->getMinWidth() * $this->missing;
66
        }
67
        
68
        return $toDistribute / $this->missing;
69
    }
70
}
71