Completed
Pull Request — master (#57)
by Daniel
04:47 queued 02:18
created

GridFactoryBuilder   C

Complexity

Total Complexity 13

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 27

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 27
dl 0
loc 124
rs 5
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid;
6
7
use Metadata\Driver\DriverChain;
8
use Metadata\MetadataFactory;
9
use Psi\Component\Grid\Action\DeleteAction;
10
use Psi\Component\Grid\Column\BooleanColumn;
11
use Psi\Component\Grid\Column\DateTimeColumn;
12
use Psi\Component\Grid\Column\MoneyColumn;
13
use Psi\Component\Grid\Column\PropertyColumn;
14
use Psi\Component\Grid\Column\SelectColumn;
15
use Psi\Component\Grid\Filter\BooleanFilter;
16
use Psi\Component\Grid\Filter\NumberFilter;
17
use Psi\Component\Grid\Filter\StringFilter;
18
use Psi\Component\Grid\Form\GridExtension;
19
use Psi\Component\Grid\Metadata\Driver\AnnotationDriver;
20
use Psi\Component\Grid\Metadata\Driver\ArrayDriver;
21
use Psi\Component\ObjectAgent\AgentFinder;
22
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
23
use Symfony\Component\Form\FormFactoryBuilderInterface;
24
use Symfony\Component\Form\Forms;
25
use Symfony\Component\Validator\Validation;
26
use Psi\Component\Grid\Column\BooleanColumn;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Cannot use Psi\Component\Grid\Column\BooleanColumn as BooleanColumn because the name is already in use
Loading history...
27
use Psi\Component\Grid\Column\MoneyColumn;
28
29
final class GridFactoryBuilder
30
{
31
    private $agentFinder;
32
    private $formFactoryBuilder;
33
34
    private $actions = [];
35
    private $columns = [];
36
    private $filters = [];
37
    private $metadataDrivers = [];
38
39
    public static function create(
40
        AgentFinder $agentFinder,
41
        FormFactoryBuilderInterface $formFactoryBuilder = null
42
    ) {
43
        $instance = new self();
44
        $instance->agentFinder = $agentFinder;
45
        $instance->formFactoryBuilder = $formFactoryBuilder ?: Forms::createFormFactoryBuilder();
46
47
        return $instance;
48
    }
49
50
    public static function createWithDefaults(AgentFinder $agentFinder, FormFactoryBuilderInterface $formFactoryBuilder = null)
51
    {
52
        return self::create($agentFinder, $formFactoryBuilder)
53
            ->addAction(new DeleteAction(), 'delete')
54
            ->addColumn(new PropertyColumn(), 'property')
55
            ->addColumn(new SelectColumn($agentFinder), 'select')
56
            ->addColumn(new DateTimeColumn(), 'datetime')
57
            ->addColumn(new MoneyColumn(), 'money')
58
            ->addColumn(new BooleanColumn(), 'boolean')
59
            ->addFilter(new StringFilter(), 'string')
60
            ->addFilter(new BooleanFilter(), 'boolean')
61
            ->addFilter(new NumberFilter(), 'number');
62
    }
63
64
    public function addArrayDriver(array $mapping)
65
    {
66
        $this->metadataDrivers[] = new ArrayDriver($mapping);
67
68
        return $this;
69
    }
70
71
    public function addAnnotationDriver()
72
    {
73
        $this->metadataDrivers[] = new AnnotationDriver();
74
75
        return $this;
76
    }
77
78
    public function addAction(ActionInterface $action, $alias = null): self
79
    {
80
        $this->actions[$alias] = $action;
81
82
        return $this;
83
    }
84
85
    public function addFilter(FilterInterface $filter, $alias = null)
86
    {
87
        $this->filters[$alias] = $filter;
88
89
        return $this;
90
    }
91
92
    public function addColumn(ColumnInterface $column, $alias = null)
93
    {
94
        $this->columns[$alias] = $column;
95
96
        return $this;
97
    }
98
99
    public function createGridFactory()
100
    {
101
        if (empty($this->metadataDrivers)) {
102
            throw new \InvalidArgumentException(
103
                'You must add at least one metadata driver (e.g. ->addArrayDriver, ->addAnnotationDriver, ->addXmlDriver)'
104
            );
105
        }
106
107
        $actionRegistry = new ActionRegistry();
108
        foreach ($this->actions as $alias => $action) {
109
            $actionRegistry->register($action, $alias);
110
        }
111
        $columnRegistry = new ColumnRegistry();
112
        foreach ($this->columns as $alias => $column) {
113
            $columnRegistry->register($column, $alias);
114
        }
115
        $filterRegistry = new FilterRegistry();
116
        foreach ($this->filters as $alias => $filter) {
117
            $filterRegistry->register($filter, $alias);
118
        }
119
120
        $metadataDriver = new DriverChain($this->metadataDrivers);
121
        $columnFactory = new ColumnFactory($columnRegistry);
122
        $metadataFactory = new MetadataFactory($metadataDriver);
123
124
        $validator = Validation::createValidator();
125
126
        $formFactory = $this->formFactoryBuilder
127
            ->addExtension(new ValidatorExtension($validator))
128
            ->addExtension(new GridExtension(
129
                $columnRegistry,
130
                $filterRegistry
131
            ))
132
            ->getFormFactory();
133
134
        $filterFactory = new FilterBarFactory($formFactory, $filterRegistry);
135
        $queryFactory = new QueryFactory($metadataFactory);
136
137
        $gridViewFactory = new GridViewFactory(
138
            $columnFactory,
139
            $filterFactory,
140
            $queryFactory
141
        );
142
143
        $actionPerformer = new ActionPerformer($actionRegistry);
144
145
        return new GridFactory(
146
            $this->agentFinder,
147
            $metadataFactory,
148
            $gridViewFactory,
149
            $actionPerformer
150
        );
151
    }
152
}
153