Completed
Pull Request — master (#84)
by Daniel
02:23
created

GridFactoryBuilder::addAnnotationDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 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\Column\TextColumn;
16
use Psi\Component\Grid\Filter\BooleanFilter;
17
use Psi\Component\Grid\Filter\ChoiceFilter;
18
use Psi\Component\Grid\Filter\DateFilter;
19
use Psi\Component\Grid\Filter\NumberFilter;
20
use Psi\Component\Grid\Filter\StringFilter;
21
use Psi\Component\Grid\Form\GridExtension;
22
use Psi\Component\Grid\Metadata\Driver\AnnotationDriver;
23
use Psi\Component\Grid\Metadata\Driver\ArrayDriver;
24
use Psi\Component\ObjectAgent\AgentFinder;
25
use Symfony\Component\EventDispatcher\EventDispatcher;
26
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
27
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
28
use Symfony\Component\Form\FormFactoryBuilderInterface;
29
use Symfony\Component\Form\Forms;
30
use Symfony\Component\Validator\Validation;
31
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
32
33
final class GridFactoryBuilder
34
{
35
    private $agentFinder;
36
    private $eventDispatcher;
37
    private $formFactoryBuilder;
38
39
    private $actions = [];
40
    private $columns = [];
41
    private $filters = [];
42
    private $metadataDrivers = [];
43
44
    public static function create(
45
        AgentFinder $agentFinder,
46
        FormFactoryBuilderInterface $formFactoryBuilder = null,
47
        EventDispatcherInterface $eventDispatcher = null
48
    ) {
49
        $instance = new self();
50
        $instance->agentFinder = $agentFinder;
51
        $instance->formFactoryBuilder = $formFactoryBuilder ?: Forms::createFormFactoryBuilder();
52
        $instance->eventDispatcher = $eventDispatcher ?: new EventDispatcher();
53
54
        return $instance;
55
    }
56
57
    public static function createWithDefaults(AgentFinder $agentFinder, FormFactoryBuilderInterface $formFactoryBuilder = null)
58
    {
59
        return self::create($agentFinder, $formFactoryBuilder)
60
            ->addAction(new DeleteAction(), 'delete')
61
62
            ->addColumn(new PropertyColumn(), 'property')
63
            ->addColumn(new SelectColumn($agentFinder), 'select')
0 ignored issues
show
Unused Code introduced by
The call to SelectColumn::__construct() has too many arguments starting with $agentFinder.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
64
            ->addColumn(new MoneyColumn(), 'money')
65
            ->addColumn(new BooleanColumn(), 'boolean')
66
            ->addColumn(new DateTimeColumn(), 'datetime')
67
            ->addColumn(new TextColumn(), 'text')
68
69
            ->addFilter(new StringFilter(), 'string')
70
            ->addFilter(new BooleanFilter(), 'boolean')
71
            ->addFilter(new NumberFilter(), 'number')
72
            ->addFilter(new DateFilter(), 'date')
73
            ->addFilter(new ChoiceFilter(), 'choice');
74
    }
75
76
    public function addArrayDriver(array $mapping)
77
    {
78
        $this->metadataDrivers[] = new ArrayDriver($mapping);
79
80
        return $this;
81
    }
82
83
    public function addAnnotationDriver()
84
    {
85
        $this->metadataDrivers[] = new AnnotationDriver();
86
87
        return $this;
88
    }
89
90
    public function addAction(ActionInterface $action, $alias = null): self
91
    {
92
        $this->actions[$alias] = $action;
93
94
        return $this;
95
    }
96
97
    public function addFilter(FilterInterface $filter, $alias = null)
98
    {
99
        $this->filters[$alias] = $filter;
100
101
        return $this;
102
    }
103
104
    public function addColumn(ColumnInterface $column, $alias = null)
105
    {
106
        $this->columns[$alias] = $column;
107
108
        return $this;
109
    }
110
111
    public function addSubscriber(EventSubscriberInterface $subscriber)
112
    {
113
        $this->eventDispatcher->addSubscriber($subscriber);
114
        return $this;
115
    }
116
117
    public function createGridFactory()
118
    {
119
        if (empty($this->metadataDrivers)) {
120
            throw new \InvalidArgumentException(
121
                'You must add at least one metadata driver (e.g. ->addArrayDriver, ->addAnnotationDriver, ->addXmlDriver)'
122
            );
123
        }
124
125
        $actionRegistry = new ActionRegistry();
126
        foreach ($this->actions as $alias => $action) {
127
            $actionRegistry->register($action, $alias);
128
        }
129
        $columnRegistry = new ColumnRegistry();
130
        foreach ($this->columns as $alias => $column) {
131
            $columnRegistry->register($column, $alias);
132
        }
133
        $filterRegistry = new FilterRegistry();
134
        foreach ($this->filters as $alias => $filter) {
135
            $filterRegistry->register($filter, $alias);
136
        }
137
138
        $metadataDriver = new DriverChain($this->metadataDrivers);
139
        $columnFactory = new ColumnFactory($columnRegistry);
140
        $metadataFactory = new MetadataFactory($metadataDriver);
141
        $gridMetadataFactory = new EventDispatchingGridMetadataFactory(
142
            new GridMetadataFactory($metadataFactory),
143
            $this->eventDispatcher
144
        );
145
146
        $validator = Validation::createValidator();
147
148
        $formFactory = $this->formFactoryBuilder
149
            ->addExtension(new ValidatorExtension($validator))
150
            ->addExtension(new GridExtension(
151
                $columnRegistry,
152
                $filterRegistry
153
            ))
154
            ->getFormFactory();
155
156
        $filterFactory = new EventDispatchingFilterBarFactory(
157
            new FilterBarFactory($formFactory, $filterRegistry),
158
            $this->eventDispatcher
159
        );
160
161
        $queryFactory = new QueryFactory($metadataFactory);
162
163
        $gridViewFactory = new GridViewFactory(
164
            $columnFactory,
165
            $filterFactory,
166
            $queryFactory
167
        );
168
169
        $actionPerformer = new ActionPerformer($actionRegistry);
170
171
        return new GridFactory(
172
            $this->agentFinder,
173
            $gridMetadataFactory,
174
            $gridViewFactory,
175
            $actionPerformer
176
        );
177
    }
178
}
179