Completed
Push — master ( 406296...0a0b96 )
by Daniel
07:00 queued 04:29
created

GridViewFactory::createView()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 17
nc 1
nop 3
1
<?php
2
3
namespace Psi\Component\Grid;
4
5
use Psi\Component\Grid\Metadata\GridMetadata;
6
use Psi\Component\ObjectAgent\AgentInterface;
7
use Psi\Component\ObjectAgent\Query\Query;
8
9
class GridViewFactory
10
{
11
    private $cellFactory;
12
    private $filterFactory;
13
14
    public function __construct(
15
        CellFactory $cellFactory,
16
        FilterBarFactory $filterFactory
17
    ) {
18
        $this->cellFactory = $cellFactory;
19
        $this->filterFactory = $filterFactory;
20
    }
21
22
    public function createView(AgentInterface $agent, GridContext $gridContext, GridMetadata $gridMetadata)
23
    {
24
        // create the filter form based on the metadata and submit any data.
25
        $filterForm = $this->filterFactory->createForm($gridMetadata, $agent->getCapabilities());
26
        $filterForm->submit($gridContext->getFilter());
27
28
        // create the query and get the data collection from the object-agent.
29
        $query = Query::create(
30
            $gridContext->getClassFqn(),
31
            $this->filterFactory->createExpression($gridMetadata, $filterForm->getData()),
32
            $gridContext->getOrderings(),
33
            $gridContext->getPageOffset(),
34
            $gridContext->getPageSize()
35
        );
36
        $collection = new \IteratorIterator($agent->query($query));
37
38
        return new View\Grid(
39
            $gridContext->getClassFqn(),
40
            $gridMetadata->getName(),
41
            new View\Table($this->cellFactory, $gridMetadata, $collection, $gridContext),
42
            new View\Paginator($gridContext, count($collection), $this->getNumberOfRecords($agent, $query)),
43
            new View\FilterBar($filterForm->createView(), $gridContext),
44
            new View\ActionBar($gridMetadata)
45
        );
46
    }
47
48
    private function getNumberOfRecords(AgentInterface $agent, Query $query)
49
    {
50
        if (false === $agent->getCapabilities()->canQueryCount()) {
51
            return;
52
        }
53
54
        return $agent->queryCount($query);
55
    }
56
}
57