Completed
Pull Request — master (#21)
by Daniel
03:53 queued 01:13
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\ObjectAgent\AgentFinder;
9
10
class GridFactory
11
{
12
    private $agentFinder;
13
    private $metadataFactory;
14
    private $cellFactory;
15
    private $filterFactory;
16
    private $actionPerformer;
17
18
    public function __construct(
19
        AgentFinder $agentFinder,
20
        MetadataFactory $metadataFactory,
21
        GridViewFactory $gridViewFactory,
22
        ActionPerformer $actionPerformer
23
    ) {
24
        $this->agentFinder = $agentFinder;
25
        $this->metadataFactory = $metadataFactory;
26
        $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...
27
        $this->actionPerformer = $actionPerformer;
28
    }
29
30
    public function loadGrid(string $classFqn, array $context): Grid
31
    {
32
        $context = new GridContext($classFqn, $context);
33
34
        try {
35
            return $this->doLoadGrid($context);
36
        } catch (\Exception $exception) {
37
            throw new \InvalidArgumentException(sprintf(
38
                'Could not load grid for class "%s"', $classFqn
39
            ), 0, $exception);
40
        }
41
    }
42
43
    private function doLoadGrid(GridContext $context): Grid
44
    {
45
        if (null === $metadata = $this->metadataFactory->getMetadataForClass($context->getClassFqn())) {
46
            throw new \InvalidArgumentException('Could not locate grid metadata');
47
        }
48
49
        // find the agent and get the grid metadata.
50
        $agent = $this->agentFinder->findFor($context->getClassFqn());
51
        $gridMetadata = $this->resolveGridMetadata($metadata->getGrids(), $context->getVariant());
52
53
        return new Grid(
54
            $this->gridViewFactory,
55
            $this->actionPerformer,
56
            $agent,
57
            $context,
58
            $gridMetadata
59
        );
60
    }
61
62
    private function resolveGridMetadata(array $grids, string $variant = null)
63
    {
64
        if (empty($grids)) {
65
            throw new \InvalidArgumentException('No grid variants are available');
66
        }
67
68
        // if no explicit grid variant is requested, return the first one that
69
        // was defined.
70
        if (null === $variant) {
71
            return reset($grids);
72
        }
73
74
        if (!isset($grids[$variant])) {
75
            throw new \InvalidArgumentException(sprintf(
76
                'Unknown grid variant "%s", available variants: "%s"',
77
                implode('", "', array_keys($grids))
78
            ));
79
        }
80
81
        return $grids[$variant];
82
    }
83
}
84