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

ColumnFactory::createCell()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 4
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