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