Completed
Pull Request — master (#5)
by Daniel
08:11 queued 06:07
created

GridFactory::doLoadGrid()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 23
nc 3
nop 3
1
<?php
2
3
namespace Psi\Component\Grid;
4
5
use Metadata\MetadataFactory;
6
use Psi\Component\ObjectAgent\AgentFinder;
7
use Psi\Component\ObjectAgent\Query\Query;
8
use Psi\Component\View\ViewFactory;
9
use Psi\Component\Grid\Form\FilterType;
10
use Psi\Component\Grid\FilterFormFactory;
11
12
class GridFactory
13
{
14
    private $agentFinder;
15
    private $metadataFactory;
16
    private $viewFactory;
17
    private $filterFactory;
18
19
    public function __construct(
20
        AgentFinder $agentFinder,
21
        MetadataFactory $metadataFactory,
22
        ViewFactory $viewFactory,
23
        FilterFormFactory $filterFactory
24
    ) {
25
        $this->agentFinder = $agentFinder;
26
        $this->metadataFactory = $metadataFactory;
27
        $this->viewFactory = $viewFactory;
28
        $this->filterFactory = $filterFactory;
29
    }
30
31
    public function loadGrid(string $classFqn, array $options, array $filterData = []): Grid
32
    {
33
        $options = new GridOptions($options);
34
35
        try {
36
            return $this->doLoadGrid($classFqn, $options, $filterData);
37
        } catch (\Exception $exception) {
38
            throw new \InvalidArgumentException(sprintf(
39
                'Could not load grid for class "%s"', $classFqn
40
            ), null, $exception);
41
        }
42
    }
43
44
    private function doLoadGrid(string $classFqn, GridOptions $options, array $filterData): Grid
45
    {
46
        if (null === $metadata = $this->metadataFactory->getMetadataForClass($classFqn)) {
47
            throw new \InvalidArgumentException('Could not locate grid metadata');
48
        }
49
50
        $gridMetadata = $this->resolveGridMetadata($metadata->getGrids(), $options->getVariant());
51
        $agent = $this->agentFinder->findAgentFor($classFqn);
52
        $expression = null;
53
54
        $form = $this->filterFactory->createForm($gridMetadata, $agent->getCapabilities(), $filterData);
0 ignored issues
show
Unused Code introduced by
The call to FilterFormFactory::createForm() has too many arguments starting with $filterData.

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...
55
        $form->submit($filterData);
56
57
        if ($filterData && $form->isValid()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filterData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
58
            $expression = $this->filterFactory->createExpression($gridMetadata, $form->getData());
59
        }
60
61
        $query = Query::create(
62
            $classFqn,
63
            $expression,
64
            $options->getOrderings(),
65
            $options->getPageOffset(),
66
            $options->getPageSize()
67
        );
68
        $collection = $agent->query($query);
69
70
        return new Grid(
71
            new Table($this->viewFactory, $gridMetadata, $collection),
72
            new Paginator(
73
                $options->getPageSize(),
74
                $options->getCurrentPage()
75
            ),
76
            new FilterForm($form->createView())
77
        );
78
    }
79
80
    private function resolveGridMetadata(array $grids, string $variant = null)
81
    {
82
        if (empty($grids)) {
83
            throw new \InvalidArgumentException('No grid variants are available');
84
        }
85
86
        if (null === $variant) {
87
            return reset($grids);
88
        }
89
90
        if (!isset($grids[$variant])) {
91
            throw new \InvalidArgumentException(sprintf(
92
                'Unknown grid variant "%s", available variants: "%s"',
93
                implode('", "', array_keys($grids))
94
            ));
95
        }
96
97
        return $grids[$variant];
98
    }
99
}
100