Completed
Pull Request — master (#29)
by Daniel
02:17
created

ColumnFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 37
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A createCell() 0 7 1
A createHeader() 0 7 1
A resolveOptions() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid;
6
7
use Psi\Component\Grid\View\Header;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
10
class ColumnFactory
11
{
12
    private $registry;
13
14
    public function __construct(
15
        ColumnRegistry $registry
16
17
    ) {
18
        $this->registry = $registry;
19
    }
20
21
    public function createCell(string $columnName, string $typeName, RowData $data, array $options): CellInterface
22
    {
23
        $column = $this->registry->get($typeName);
24
        $options = $this->resolveOptions($columnName, $column, $options);
25
26
        return $column->createCell($data, $options);
27
    }
28
29
    public function createHeader(GridContext $gridContext, string $columnName, string $typeName, array $options): Header
30
    {
31
        $column = $this->registry->get($typeName);
32
        $options = $this->resolveOptions($columnName, $column, $options);
33
34
        return $column->createHeader($gridContext, $options);
35
    }
36
37
    private function resolveOptions($columnName, ColumnInterface $column, array $options)
38
    {
39
        $resolver = new OptionsResolver();
40
        $resolver->setDefault('column_name', $columnName);
41
        $column->configureOptions($resolver);
42
        $options = $resolver->resolve($options);
43
44
        return $options;
45
    }
46
}
47