MoneyColumn   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 34
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configureOptions() 0 6 1
A getParent() 0 4 1
A buildCell() 0 19 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid\Column;
6
7
use Psi\Component\Grid\View\Cell;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
10
class MoneyColumn extends AbstractColumn
11
{
12
    public function buildCell(Cell $cell, array $options)
13
    {
14
        $value = $cell->value;
15
16
        if (null === $value) {
17
            return;
18
        }
19
20
        if (false === is_int($value)) {
21
            throw new \InvalidArgumentException(sprintf(
22
                'Money column requires an integer value, got "%s"',
23
                gettype($value)
24
            ));
25
        }
26
27
        $cell->value = $cell->value / $options['divisor'];
28
        $cell->value = number_format($cell->value, $options['scale']);
29
        $cell->parameters['currency'] = $options['currency'];
30
    }
31
32
    public function configureOptions(OptionsResolver $resolver)
33
    {
34
        $resolver->setDefault('currency', 'EUR');
35
        $resolver->setDefault('divisor', 1);
36
        $resolver->setDefault('scale', 2);
37
    }
38
39
    public function getParent()
40
    {
41
        return PropertyColumn::class;
42
    }
43
}
44