Completed
Pull Request — master (#42)
by Daniel
01:54
created

QueryFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 45
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
B createQuery() 0 24 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid;
6
7
use Metadata\MetadataFactory;
8
use Psi\Component\ObjectAgent\Query\Converter\ArrayConverter;
9
10
class QueryFactory
11
{
12
    /**
13
     * @var MetadataFactory
14
     */
15
    private $metadataFactory;
16
17
    /**
18
     * @var ArrayConverter
19
     */
20
    private $converter;
21
22
    public function __construct(
23
        MetadataFactory $metadataFactory,
24
        ArrayConverter $converter = null
25
    ) {
26
        $this->metadataFactory = $metadataFactory;
27
        $this->converter = $converter ?: new ArrayConverter();
28
    }
29
30
    public function createQuery(string $classFqn, string $name)
31
    {
32
        if (null === $metadata = $this->metadataFactory->getMetadataForClass($classFqn)) {
33
            throw new \InvalidArgumentException('Could not locate grid metadata');
34
        }
35
36
        $queries = $metadata->getQueries();
37
38
        if (!isset($queries[$name])) {
39
            throw new \InvalidArgumentException(sprintf(
40
                'Query "%s" for "%s" is not known. Known queries: "%s"',
41
                $name, $classFqn, implode('", "', array_keys($queries))
42
            ));
43
        }
44
45
        $query = $queries[$name];
46
47
        return $this->converter->__invoke([
48
            'from' => $classFqn,
49
            'selects' => $query->getSelects(),
50
            'joins' => $query->getJoins(),
51
            'criteria' => $query->getCriteria() ?: null,
52
        ]);
53
    }
54
}
55