Completed
Push — master ( d1e33a...1dc55d )
by Nikola
05:12
created

DoctrineOrmExtension::getFunctions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 11
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
crap 1
1
<?php
2
/*
3
 * This file is part of the QueryResourcesLoaderBundle, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Bundle\QueryResourcesLoader\Twig;
11
12
use Symfony\Bridge\Doctrine\RegistryInterface;
13
14
class DoctrineOrmExtension extends \Twig_Extension
15
{
16
    /**
17
     * @var RegistryInterface
18
     */
19
    private $doctrine;
20
21 5
    public function __construct(RegistryInterface $doctrine)
22
    {
23 5
        $this->doctrine = $doctrine;
24 5
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 1
    public function getName()
30
    {
31 1
        return 'runopencode_query_resources_loader';
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 View Code Duplication
    public function getFunctions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        return array(
40 3
            new \Twig_SimpleFunction('table_name', \Closure::bind(function($entity) {
41 1
                return $this->getTableName($entity);
42 3
            }, $this)),
43 3
            new \Twig_SimpleFunction('column_name', \Closure::bind(function($field, $entity) {
44 1
                return $this->getColumnName($field, $entity);
45 3
            }, $this))
46
        );
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 View Code Duplication
    public function getFilters()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        return array(
55 1
            new \Twig_SimpleFilter('table_name', \Closure::bind(function($entity) {
56
                return $this->getTableName($entity);
57 1
            }, $this)),
58 1
            new \Twig_SimpleFilter('column_name', \Closure::bind(function($field, $entity) {
59
                return $this->getColumnName($field, $entity);
60 1
            }, $this))
61
        );
62
    }
63
64
    /**
65
     * Get table name for entity
66
     *
67
     * @param string|object $entity Entity
68
     * @return string
69
     */
70 1 View Code Duplication
    protected function getTableName($entity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72 1
        if (is_object($entity)) {
73
            $entity = get_class($entity);
74
        }
75
76 1
        return $this->doctrine->getManagerForClass($entity)->getClassMetadata($entity)->getTableName();
77
    }
78
79
    /**
80
     * Get column name for property of the entity
81
     *
82
     * @param string $field Entity property
83
     * @param string|object $entity
84
     * @return string
85
     */
86 1 View Code Duplication
    protected function getColumnName($field, $entity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88 1
        if (is_object($entity)) {
89
            $entity = get_class($entity);
90
        }
91
92 1
        return $this->doctrine->getManagerForClass($entity)->getClassMetadata($entity)->getColumnName($field);
93
    }
94
}
95