Completed
Push — master ( 8413a9...d75c00 )
by Narcotic
10:07
created

TranslatableFieldsCompilerPass   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 13.04 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 93.1%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 9
loc 69
ccs 27
cts 29
cp 0.931
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 10 2
C getTranslatableFields() 9 37 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * build a list of all services that have translatable mappings
4
 *
5
 * this can be used by whoever needs to know where translatables are..
6
 */
7
8
namespace Graviton\DocumentBundle\DependencyInjection\Compiler;
9
10
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\DocumentMap;
11
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\Document;
12
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\EmbedMany;
13
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\EmbedOne;
14
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\Field;
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
18
/**
19
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
20
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
21
 * @link     http://swisscom.ch
22
 */
23
class TranslatableFieldsCompilerPass implements CompilerPassInterface
24
{
25
    /**
26
     * @var DocumentMap
27
     */
28
    private $documentMap;
29
30
    /**
31
     * Make translatable fields map and set it to parameter
32
     *
33
     * @param ContainerBuilder $container container builder
34
     * @return void
35
     */
36 4
    public function process(ContainerBuilder $container)
37
    {
38 4
        $this->documentMap = $container->get('graviton.document.map');
39
40 4
        $map = [];
41 4
        foreach ($this->documentMap->getDocuments() as $document) {
42 4
            $map[$document->getClass()] = $this->getTranslatableFields($document);
43
        }
44 4
        $container->setParameter('graviton.document.type.translatable.fields', $map);
45 4
    }
46
47
    /**
48
     * Get document fields
49
     *
50
     * @param Document $document Document
51
     * @param string   $prefix   Field prefix
52
     * @return array
53
     */
54 4
    private function getTranslatableFields(Document $document, $prefix = '')
55
    {
56 4
        $reflection = new \ReflectionClass($document->getClass());
57 4
        if ($reflection->implementsInterface('Graviton\I18nBundle\Document\TranslatableDocumentInterface')) {
58 4
            $instance = $reflection->newInstanceWithoutConstructor();
59 4
            $translatableFields = $instance->getTranslatableFields();
60
        } else {
61 2
            $translatableFields = [];
62
        }
63
64 4
        $result = [];
65 4
        foreach ($document->getFields() as $field) {
66 4
            if ($field instanceof Field) {
67 4
                if (in_array($field->getFieldName(), $translatableFields, true)) {
68 4
                    $result[] = $prefix.$field->getExposedName();
69
                }
70
            } elseif ($field instanceof EmbedOne) {
71 4
                $result = array_merge(
72
                    $result,
73 4
                    $this->getTranslatableFields(
74 4
                        $field->getDocument(),
75 4
                        $prefix.$field->getExposedName().'.'
76
                    )
77
                );
78 View Code Duplication
            } elseif ($field instanceof EmbedMany) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
79 4
                $result = array_merge(
80
                    $result,
81 4
                    $this->getTranslatableFields(
82 4
                        $field->getDocument(),
83 4
                        $prefix.$field->getExposedName().'.0.'
84
                    )
85
                );
86
            }
87
        }
88
89 4
        return $result;
90
    }
91
}
92