Completed
Push — master ( af310e...ca9412 )
by Nikola
05:51
created

DoctrineOrmExtension::getFilters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 1.037

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 11
loc 11
ccs 4
cts 6
cp 0.6667
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
crap 1.037
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RunOpenCode\Bundle\QueryResourcesLoader\Twig;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\ORM\Mapping\ClassMetadataInfo;
9
use Doctrine\ORM\Mapping\MappingException;
10
use Doctrine\Persistence\ManagerRegistry;
11
use Twig\Extension\AbstractExtension;
12
use Twig\TwigFilter;
13
use Twig\TwigFunction;
14
15
final class DoctrineOrmExtension extends AbstractExtension
16
{
17
    private ManagerRegistry $doctrine;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
18
19 4
    public function __construct(ManagerRegistry $doctrine)
20
    {
21 4
        $this->doctrine = $doctrine;
22 4
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 3
    public function getFunctions(): array
28
    {
29
        return [
30
            new TwigFunction('table_name', \Closure::bind(function ($entity) {
31 1
                return $this->getTableName($entity);
32 3
            }, $this)),
33
            new TwigFunction('join_table_name', \Closure::bind(function ($field, $entity) {
34
                return $this->getJoinTableName($field, $entity);
35 3
            }, $this)),
36
            new TwigFunction('column_name', \Closure::bind(function ($field, $entity) {
37 1
                return $this->getColumnName($field, $entity);
38 3
            }, $this)),
39
        ];
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 1
    public function getFilters(): array
46
    {
47
        return [
48
            new TwigFilter('table_name', \Closure::bind(function ($entity) {
49
                return $this->getTableName($entity);
50 1
            }, $this)),
51
            new TwigFilter('join_table_name', \Closure::bind(function ($field, $entity) {
52
                return $this->getJoinTableName($field, $entity);
53 1
            }, $this)),
54
            new TwigFilter('column_name', \Closure::bind(function ($field, $entity) {
55
                return $this->getColumnName($field, $entity);
56 1
            }, $this)),
57
        ];
58
    }
59
60
    /**
61
     * Get table name for entity
62
     *
63
     * @param string $entity Entity
64
     *
65
     * @return string
66
     */
67 1
    private function getTableName(string $entity): string
68
    {
69
        /** @var EntityManagerInterface $entityManager */
70 1
        $entityManager = $this->doctrine->getManagerForClass($entity);
71
72 1
        return $entityManager->getClassMetadata($entity)->getTableName();
73
    }
74
75
    /**
76
     * Get column name for property of the entity
77
     *
78
     * @param string $field  Entity property
79
     * @param string $entity Entity
80
     *
81
     * @return string
82
     */
83 1
    private function getColumnName(string $field, string $entity): string
84
    {
85
        /** @var EntityManagerInterface $entityManager */
86 1
        $entityManager = $this->doctrine->getManagerForClass($entity);
87
88 1
        return $entityManager->getClassMetadata($entity)->getColumnName($field);
89
    }
90
91
    /**
92
     * Get join table name name for property of the entity
93
     *
94
     * @param string $field  Entity property
95
     * @param string $entity Entity
96
     *
97
     * @return string
98
     *
99
     * @throws MappingException
100
     */
101
    private function getJoinTableName(string $field, string $entity): string
102
    {
103
        /** @var EntityManagerInterface $entityManager */
104
        $entityManager = $this->doctrine->getManagerForClass($entity);
105
        /** @var ClassMetadataInfo $metadata */
106
        $metadata = $entityManager->getClassMetadata($entity);
107
        $mapping  = $metadata->getAssociationMapping($field);
108
109
        return $mapping['joinTable']['name'];
110
    }
111
}
112