Completed
Push — master ( 664783...554f9d )
by
unknown
15:59
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 100%

Importance

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

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 2
            } 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 2 View Code Duplication
            } elseif ($field instanceof EmbedMany) {
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