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 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