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

GridViewFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 10
dl 0
loc 46
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B createView() 0 25 1
A getNumberOfRecords() 0 8 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