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

GridFactory::getNumberOfRecords()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid;
6
7
use Metadata\MetadataFactory;
8
use Psi\Component\Grid\Metadata\GridMetadata;
9
use Psi\Component\ObjectAgent\AgentFinder;
10
use Psi\Component\ObjectAgent\AgentInterface;
11
use Psi\Component\ObjectAgent\Query\Query;
12
use Psi\Component\Grid\Grid;
13
use Psi\Component\Grid\GridViewFactory;
14
15
class GridFactory
16
{
17
    private $agentFinder;
18
    private $metadataFactory;
19
    private $cellFactory;
20
    private $filterFactory;
21
    private $actionPerformer;
22
23
    public function __construct(
24
        AgentFinder $agentFinder,
25
        MetadataFactory $metadataFactory,
26
        GridViewFactory $gridViewFactory,
27
        ActionPerformer $actionPerformer
28
    ) {
29
        $this->agentFinder = $agentFinder;
30
        $this->metadataFactory = $metadataFactory;
31
        $this->gridViewFactory = $gridViewFactory;
0 ignored issues
show
Bug introduced by
The property gridViewFactory 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...
32
        $this->actionPerformer = $actionPerformer;
33
    }
34
35
    public function loadGrid(string $classFqn, array $context): Grid
36
    {
37
        $context = new GridContext($classFqn, $context);
38
39
        try {
40
            return $this->doLoadGrid($context);
41
        } catch (\Exception $exception) {
42
            throw new \InvalidArgumentException(sprintf(
43
                'Could not load grid for class "%s"', $classFqn
44
            ), 0, $exception);
45
        }
46
    }
47
48
    private function doLoadGrid(GridContext $context): Grid
49
    {
50
        if (null === $metadata = $this->metadataFactory->getMetadataForClass($context->getClassFqn())) {
51
            throw new \InvalidArgumentException('Could not locate grid metadata');
52
        }
53
54
        // find the agent and get the grid metadata.
55
        $agent = $this->agentFinder->findFor($context->getClassFqn());
56
        $gridMetadata = $this->resolveGridMetadata($metadata->getGrids(), $context->getVariant());
57
58
        return new Grid(
59
            $this->gridViewFactory,
60
            $this->actionPerformer,
61
            $agent,
62
            $context,
63
            $gridMetadata
64
        );
65
    }
66
67
    private function resolveGridMetadata(array $grids, string $variant = null)
68
    {
69
        if (empty($grids)) {
70
            throw new \InvalidArgumentException('No grid variants are available');
71
        }
72
73
        // if no explicit grid variant is requested, return the first one that
74
        // was defined.
75
        if (null === $variant) {
76
            return reset($grids);
77
        }
78
79
        if (!isset($grids[$variant])) {
80
            throw new \InvalidArgumentException(sprintf(
81
                'Unknown grid variant "%s", available variants: "%s"',
82
                implode('", "', array_keys($grids))
83
            ));
84
        }
85
86
        return $grids[$variant];
87
    }
88
}
89