Completed
Pull Request — master (#21)
by Daniel
02:31
created

GridViewFactory::getNumberOfRecords()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace Psi\Component\Grid;
4
5
use Psi\Component\ObjectAgent\AgentInterface;
6
use Psi\Component\Grid\GridContext;
7
use Psi\Component\Grid\Metadata\GridMetadata;
8
use Psi\Component\ObjectAgent\Query\Query;
9
10
class GridViewFactory
11
{
12
    public function __construct(
13
        CellFactory $cellFactory,
14
        FilterBarFactory $filterFactory
15
    )
16
    {
17
        $this->cellFactory = $cellFactory;
0 ignored issues
show
Bug introduced by
The property cellFactory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
18
        $this->filterFactory = $filterFactory;
0 ignored issues
show
Bug introduced by
The property filterFactory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
19
    }
20
21
    public function createView(AgentInterface $agent, GridContext $gridContext, GridMetadata $gridMetadata)
22
    {
23
        // create the filter form based on the metadata and submit any data.
24
        $filterForm = $this->filterFactory->createForm($gridMetadata, $agent->getCapabilities());
25
        $filterForm->submit($gridContext->getFilter());
26
27
        // create the query and get the data collection from the object-agent.
28
        $query = Query::create(
29
            $gridContext->getClassFqn(),
30
            $this->filterFactory->createExpression($gridMetadata, $filterForm->getData()),
31
            $gridContext->getOrderings(),
32
            $gridContext->getPageOffset(),
33
            $gridContext->getPageSize()
34
        );
35
        $collection = new \IteratorIterator($agent->query($query));
36
37
        return new View\Grid(
38
            $gridContext->getClassFqn(),
39
            $gridMetadata->getName(),
40
            new View\Table($this->cellFactory, $gridMetadata, $collection, $gridContext),
41
            new View\Paginator($gridContext, count($collection), $this->getNumberOfRecords($agent, $query)),
42
            new View\FilterBar($filterForm->createView(), $gridContext),
43
            new View\ActionBar($gridMetadata)
44
        );
45
    }
46
47
    private function getNumberOfRecords(AgentInterface $agent, Query $query)
48
    {
49
        if (false === $agent->getCapabilities()->canQueryCount()) {
50
            return;
51
        }
52
53
        return $agent->queryCount($query);
54
    }
55
}
56